Skip to content

Instantly share code, notes, and snippets.

@NanoDano
Last active May 15, 2018 00:35
Show Gist options
  • Save NanoDano/05612865cd1f7fe6de0f87a5db2da540 to your computer and use it in GitHub Desktop.
Save NanoDano/05612865cd1f7fe6de0f87a5db2da540 to your computer and use it in GitHub Desktop.
Recursive Delete Directory in Node.js
/**
* 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