Created
February 17, 2021 10:05
-
-
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)
This file contains 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
/* 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