-
-
Save azaslavsky/661020d437fa199e95ab to your computer and use it in GitHub Desktop.
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
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