diff --git a/helpers/functions/repoHistory.js b/helpers/functions/repoHistory.js new file mode 100644 index 0000000..e9bfc55 --- /dev/null +++ b/helpers/functions/repoHistory.js @@ -0,0 +1,45 @@ +import { promises as fs } from 'fs'; +import path from 'path'; + +export default async function repoHistory(data) { + try { + const repoHistoryDir = path.join(process.cwd(), '/config/versions'); + const maxBackupCount = parseInt(process.env.MAX_REPO_BACKUP_COUNT) || 8; + const timestamp = new Date().toISOString(); + const backupDate = timestamp.split('T')[0]; + + //Create the directory if it does not exist + await fs.mkdir(repoHistoryDir, { recursive: true }); + + const existingBackups = await fs.readdir(repoHistoryDir); + + if (existingBackups.length >= maxBackupCount) { + existingBackups.sort(); + const backupsToDelete = existingBackups.slice( + 0, + existingBackups.length - maxBackupCount + 1 + ); + for (const backupToDelete of backupsToDelete) { + const backupFilePathToDelete = path.join( + repoHistoryDir, + backupToDelete + ); + await fs.unlink(backupFilePathToDelete); + } + } + + const backupFileName = `${backupDate}.log`; + const backupFilePath = path.join(repoHistoryDir, backupFileName); + const jsonData = JSON.stringify(data, null, 2); + + const logData = `\n>>>> History of file repo.json at "${timestamp}" <<<<\n${jsonData}\n`; + + // Écrire ou réécrire le fichier avec le contenu mis à jour + await fs.appendFile(backupFilePath, logData); + } catch (error) { + console.error( + 'An error occurred while saving the repo history :', + error.message + ); + } +} diff --git a/pages/api/repo/add.js b/pages/api/repo/add.js index 9963a60..0288817 100644 --- a/pages/api/repo/add.js +++ b/pages/api/repo/add.js @@ -2,6 +2,7 @@ import { promises as fs } from 'fs'; import path from 'path'; import { authOptions } from '../../../pages/api/auth/[...nextauth]'; import { getServerSession } from 'next-auth/next'; +import repoHistory from '../../../helpers/functions/repoHistory'; const util = require('node:util'); const exec = util.promisify(require('node:child_process').exec); @@ -74,6 +75,9 @@ export default async function handler(req, res) { //Create the new repoList with the new repo let newRepoList = [newRepo, ...repoList]; + //History the new repoList + await repoHistory(newRepoList); + //Stringify the newRepoList to write it into the json file. newRepoList = JSON.stringify(newRepoList); diff --git a/pages/api/repo/id/[slug]/delete.js b/pages/api/repo/id/[slug]/delete.js index 1e574f9..02a1c32 100644 --- a/pages/api/repo/id/[slug]/delete.js +++ b/pages/api/repo/id/[slug]/delete.js @@ -2,6 +2,7 @@ import { promises as fs } from 'fs'; import path from 'path'; import { authOptions } from '../../../auth/[...nextauth]'; import { getServerSession } from 'next-auth/next'; +import repoHistory from '../../../../../helpers/functions/repoHistory'; const util = require('node:util'); const exec = util.promisify(require('node:child_process').exec); @@ -69,7 +70,8 @@ export default async function handler(req, res) { }); return; } - + //History the repoList + await repoHistory(repoList); //Stringify the repoList to write it into the json file. repoList = JSON.stringify(repoList); //Write the new json @@ -80,6 +82,7 @@ export default async function handler(req, res) { if (err) console.log(err); } ); + res.status(200).json({ message: 'Envoi API réussi' }); } catch (error) { //Log for backend diff --git a/pages/api/repo/id/[slug]/edit.js b/pages/api/repo/id/[slug]/edit.js index 0476461..c724ef6 100644 --- a/pages/api/repo/id/[slug]/edit.js +++ b/pages/api/repo/id/[slug]/edit.js @@ -2,6 +2,7 @@ import { promises as fs } from 'fs'; import path from 'path'; import { authOptions } from '../../../auth/[...nextauth]'; import { getServerSession } from 'next-auth/next'; +import repoHistory from '../../../../../helpers/functions/repoHistory'; const util = require('node:util'); const exec = util.promisify(require('node:child_process').exec); @@ -65,6 +66,8 @@ export default async function handler(req, res) { } : repo ); + //History the new repoList + await repoHistory(newRepoList); //Stringify the newRepoList to write it into the json file. newRepoList = JSON.stringify(newRepoList); //Write the new json @@ -75,6 +78,7 @@ export default async function handler(req, res) { if (err) console.log(err); } ); + res.status(200).json({ message: 'Envoi API réussi' }); } catch (error) { //Log for backend