👌 IMPROVE: backend - Hotfix suppression fichiers

This commit is contained in:
Divlo
2020-04-15 13:55:07 +02:00
parent 3d0488a605
commit 43d2180209
4 changed files with 29 additions and 30 deletions

View File

@ -1,19 +1,18 @@
const fs = require("fs");
const path = require("path");
function deleteFilesNameStartWith(pattern, dirPath = __dirname) {
// Get all file names in directory
fs.readdir(path.resolve(dirPath), (err, fileNames) => {
if (err) throw err;
console.log(dirPath);
// Iterate through the found file names
function deleteFilesNameStartWith(pattern, dirPath, callback) {
fs.readdir(path.resolve(dirPath), (_error, fileNames) => {
for (const name of fileNames) {
// If file name matches the pattern
if (name.startsWith(pattern) && name !== 'default.png') {
console.log(name)
fs.unlinkSync(path.join(dirPath, name));
const splitedName = name.split('.');
if (splitedName.length === 2) {
const fileName = splitedName[0];
if (fileName === pattern && name !== 'default.png') {
return fs.unlink(path.join(dirPath, name), callback);
}
}
}
return callback();
});
}