From 1befdb1c6bf3282aa34e6caa431cb2da23d2b17d Mon Sep 17 00:00:00 2001 From: Emmanuel Isenah Date: Sun, 17 Mar 2024 22:19:34 +0100 Subject: [PATCH] feat: Add deleteFile function to helpers module --- src/helpers.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/helpers.js b/src/helpers.js index 1c705df..78379e1 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -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 };