Last active
May 15, 2018 00:35
-
-
Save NanoDano/05612865cd1f7fe6de0f87a5db2da540 to your computer and use it in GitHub Desktop.
Recursive Delete Directory in Node.js
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
/** | |
* Clean out the build/ directory. | |
* | |
* npm run clean | |
*/ | |
const fs = require('fs'); | |
const path = require('path'); | |
let buildDir = "build"; | |
let rmDirRecursive = (dirpath) => { | |
let files = fs.readdirSync(dirpath); | |
files.forEach(file => { | |
let filepath = path.join(dirpath, file); | |
let stats = fs.statSync(filepath); | |
if (stats.isFile(filepath) || stats.isSymbolicLink(filepath)) { | |
fs.unlinkSync(filepath); | |
} else if (stats.isDirectory(filepath)) { | |
rmDirRecursive(filepath); | |
fs.rmdirSync(filepath); | |
} | |
}); | |
}; | |
console.log("[*] Cleaning the build directory..."); | |
rmDirRecursive(buildDir); | |
console.log("[*] Done cleaning."); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment