1- const { existsSync, mkdirSync, writeFileSync } = require ( 'fs' ) ;
1+ const { existsSync, mkdirSync, writeFileSync, unlink } = require ( 'fs' ) ;
22const { join } = require ( 'path' ) ;
33
44const 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+
4871const 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
6790module . exports = {
6891 writeToFile,
92+ deleteFile,
6993 validateRequiredInputs,
7094 snakeToCamel
7195} ;
0 commit comments