Skip to content

Commit

Permalink
feat: Add deleteFile function to helpers module
Browse files Browse the repository at this point in the history
  • Loading branch information
Armadillidiid committed Mar 17, 2024
1 parent 01a39e3 commit 1befdb1
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { existsSync, mkdirSync, writeFileSync } = require('fs');
const { existsSync, mkdirSync, writeFileSync, unlink } = require('fs');
const { join } = require('path');

const validateDir = (dir) => {
Expand Down Expand Up @@ -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) => {
Expand All @@ -66,6 +89,7 @@ const snakeToCamel = (str) => str.replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.t

module.exports = {
writeToFile,
deleteFile,
validateRequiredInputs,
snakeToCamel
};

0 comments on commit 1befdb1

Please sign in to comment.