feat: Add deleteFile function to helpers module
This commit is contained in:
parent
01a39e3348
commit
1befdb1c6b
@ -1,4 +1,4 @@
|
||||
const { existsSync, mkdirSync, writeFileSync } = require('fs');
|
||||
const { existsSync, mkdirSync, writeFileSync, unlink } = require('fs');
|
||||
const { join } = require('path');
|
||||
|
||||
const validateDir = (dir) => {
|
||||
@ -45,6 +45,29 @@ const writeToFile = ({ dir, filename, content, isRequired, mode = '0644' }) => {
|
||||
}
|
||||
};
|
||||
|
||||
const deleteFile = ({ dir, filename, isRequired }) => {
|
||||
validateDir(dir);
|
||||
const filePath = join(dir, filename);
|
||||
|
||||
if (existsSync(filePath)) {
|
||||
const message = `⚠️ [FILE] ${filePath} Required file exist.`;
|
||||
handleError(message, isRequired);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
console.log(`[FILE] Deleting ${filePath} file ...`);
|
||||
unlink(filePath, (error) => {
|
||||
if (error) {
|
||||
throw new Error(error);
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
const message = `⚠️[FILE] Deleting file error. filePath: ${filePath}, message: ${error.message}`;
|
||||
handleError(message, isRequired);
|
||||
}
|
||||
};
|
||||
|
||||
const validateRequiredInputs = (inputs) => {
|
||||
const inputKeys = Object.keys(inputs);
|
||||
const validInputs = inputKeys.filter((inputKey) => {
|
||||
@ -66,6 +89,7 @@ const snakeToCamel = (str) => str.replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.t
|
||||
|
||||
module.exports = {
|
||||
writeToFile,
|
||||
deleteFile,
|
||||
validateRequiredInputs,
|
||||
snakeToCamel
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user