Created
July 1, 2020 22:18
-
-
Save MoatazAbdAlmageed/6deffb750abf0f0744e7be98f0e3bee4 to your computer and use it in GitHub Desktop.
nodeJs helper functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require("fs"); | |
const mkdir = (dir) => { | |
if (!fs.existsSync(dir)) { | |
fs.mkdir(dir, (err) => { | |
if (err) throw err; | |
console.log("Folder has been created successfully"); | |
}); | |
} else { | |
console.log("Directory Exists"); | |
} | |
}; | |
const rmdir = (dir) => { | |
if (fs.existsSync(dir)) { | |
fs.rmdir(dir, (err) => { | |
if (err) throw err; | |
console.log("Folder has been removed successfully"); | |
}); | |
} else { | |
console.log("Directory not found"); | |
} | |
}; | |
// files | |
const touch = (file, data) => { | |
if (!fs.existsSync(file)) { | |
fs.writeFile(file, data, (err) => { | |
if (err) throw err; | |
console.log("File has been created successfully"); | |
}); | |
} else { | |
console.log("File already exits"); | |
} | |
}; | |
const rm = (file) => { | |
if (fs.existsSync(file)) { | |
fs.unlink(file, (err) => { | |
if (err) throw err; | |
console.log("File has been removed successfully"); | |
}); | |
} else { | |
console.log("File not found"); | |
} | |
}; | |
module.exports = { mkdir, rmdir, rm, touch }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment