Skip to content

Commit 1befdb1

Browse files
committed
feat: Add deleteFile function to helpers module
1 parent 01a39e3 commit 1befdb1

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/helpers.js

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { existsSync, mkdirSync, writeFileSync } = require('fs');
1+
const { existsSync, mkdirSync, writeFileSync, unlink } = require('fs');
22
const { join } = require('path');
33

44
const validateDir = (dir) => {
@@ -45,6 +45,29 @@ const writeToFile = ({ dir, filename, content, isRequired, mode = '0644' }) => {
4545
}
4646
};
4747

48+
const deleteFile = ({ dir, filename, isRequired }) => {
49+
validateDir(dir);
50+
const filePath = join(dir, filename);
51+
52+
if (existsSync(filePath)) {
53+
const message = `⚠️ [FILE] ${filePath} Required file exist.`;
54+
handleError(message, isRequired);
55+
return;
56+
}
57+
58+
try {
59+
console.log(`[FILE] Deleting ${filePath} file ...`);
60+
unlink(filePath, (error) => {
61+
if (error) {
62+
throw new Error(error);
63+
}
64+
});
65+
} catch (error) {
66+
const message = `⚠️[FILE] Deleting file error. filePath: ${filePath}, message: ${error.message}`;
67+
handleError(message, isRequired);
68+
}
69+
};
70+
4871
const validateRequiredInputs = (inputs) => {
4972
const inputKeys = Object.keys(inputs);
5073
const validInputs = inputKeys.filter((inputKey) => {
@@ -66,6 +89,7 @@ const snakeToCamel = (str) => str.replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.t
6689

6790
module.exports = {
6891
writeToFile,
92+
deleteFile,
6993
validateRequiredInputs,
7094
snakeToCamel
7195
};

0 commit comments

Comments
 (0)