-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
33 lines (27 loc) · 1.06 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const fs = require('fs');
const path = require('path');
function deleteFilesOlderThanDays(dirPath, durationInDays) {
const now = Date.now();
const msInDay = 86400000; // number of milliseconds in a day
const files = fs.readdirSync(dirPath);
files.forEach((file) => {
const filePath = path.join(dirPath, file);
const fileAgeInMs = now - fs.statSync(filePath).mtimeMs;
const fileAgeInDays = fileAgeInMs / msInDay;
if (fileAgeInDays > durationInDays) {
fs.unlinkSync(filePath);
console.log(`Deleted ${filePath}`);
}
});
}
// Define the log file path
const logFilePath = path.join(__dirname, 'backup.log');
// Set up a writable stream to the log file
const logStream = fs.createWriteStream(logFilePath, { flags: 'a' });
function log(message, isError = false) {
const timestamp = new Date().toLocaleString('fr-FR', { timeZone: 'Europe/Paris' });
const prefix = `${timestamp} - ${isError ? 'ERROR: ' : ''}`;
const logMessage = `${prefix}${message}`;
logStream.write(logMessage + '\n');
}
module.exports = { deleteFilesOlderThanDays, log };