1
- const { existsSync, mkdirSync, writeFileSync } = require ( 'fs' ) ;
1
+ const { existsSync, mkdirSync, writeFileSync, unlink } = require ( 'fs' ) ;
2
2
const { join } = require ( 'path' ) ;
3
3
4
4
const validateDir = ( dir ) => {
@@ -45,6 +45,29 @@ const writeToFile = ({ dir, filename, content, isRequired, mode = '0644' }) => {
45
45
}
46
46
} ;
47
47
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
+
48
71
const validateRequiredInputs = ( inputs ) => {
49
72
const inputKeys = Object . keys ( inputs ) ;
50
73
const validInputs = inputKeys . filter ( ( inputKey ) => {
@@ -66,6 +89,7 @@ const snakeToCamel = (str) => str.replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.t
66
89
67
90
module . exports = {
68
91
writeToFile,
92
+ deleteFile,
69
93
validateRequiredInputs,
70
94
snakeToCamel
71
95
} ;
0 commit comments