feat: history in a log the repo list changes

This commit is contained in:
Ravinou 2023-10-08 16:38:06 +02:00
parent 5720ff906c
commit 3e63f187e8
No known key found for this signature in database
GPG key ID: EEEE670C40F6A4D7
4 changed files with 57 additions and 1 deletions

View file

@ -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
);
}
}

View file

@ -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);

View file

@ -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

View file

@ -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