Skip to content

Instantly share code, notes, and snippets.

@azaslavsky
Forked from tkihira/gist:2367067
Last active August 31, 2018 11:05
Show Gist options
  • Save azaslavsky/661020d437fa199e95ab to your computer and use it in GitHub Desktop.
Save azaslavsky/661020d437fa199e95ab to your computer and use it in GitHub Desktop.
var fs = require("fs");
var path = require("path");
var rmdir = function(dir) {
var empty = true, list = fs.readdirSync(dir);
for(var i = list.length - 1; i >= 0; i--) {
var filename = path.join(dir, list[i]);
var stat = fs.statSync(filename);
if(filename.indexOf('.') > -1) {
//There are files in the directory - we can't empty it!
empty = false;
list.splice(i, 1);
}
}
//Cycle through the list of sub-directories, cleaning each as we go
for(var i = list.length - 1; i >= 0; i--) {
filename = path.join(dir, list[i]);
if (rmdir(filename)) {
list.splice(i, 1);
}
}
//Check if the directory was truly empty
if (!list.length && empty) {
console.log('delete!');
fs.rmdirSync(dir);
return true;
}
return false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment