Skip to content

Instantly share code, notes, and snippets.

@Drecu
Created February 17, 2021 10:05
Show Gist options
  • Save Drecu/4848ac433db380fe2180118919fc4b81 to your computer and use it in GitHub Desktop.
Save Drecu/4848ac433db380fe2180118919fc4b81 to your computer and use it in GitHub Desktop.
Node script to delete folders and files recursively from the cli (usage: node clean.js path1 path2)
/* eslint-disable no-undef */
/* eslint-disable @typescript-eslint/no-var-requires */
var fs = require('fs');
function deleteFolderRecursive(path) {
if(!fs.existsSync(path)) return console.log(`${path} does not exist!`)
if (fs.existsSync(path) && fs.lstatSync(path).isDirectory()) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
fs.readdirSync(path).forEach(function(file, _index){
var curPath = path + "/" + file;
if (fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
console.log(`Deleting directory "${path}"...`);
fs.rmdirSync(path);
}
};
var pathArgs = process.argv.slice(2);
if(pathArgs.length < 1) return console.warn("Please enter paths as arguments, for the folders you want to delete recursively. \nExample script invokation: node scripts/clean.js ./release")
console.log("Cleaning working tree...");
pathArgs.forEach(pathToDelete => deleteFolderRecursive(pathToDelete));
console.log("Successfully cleaned working tree!");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment