Last active
December 1, 2016 16:34
-
-
Save geedew/48a4888e30271b930d33 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 rmdirAsync = function(path, callback) { | |
fs.readdir(path, function(err, files) { | |
if(err) { | |
// Pass the error on to callback | |
callback(err, []); | |
return; | |
} | |
var wait = files.length, | |
count = 0, | |
folderDone = function(err) { | |
count++; | |
// If we cleaned out all the files, continue | |
if( count >= wait || err) { | |
fs.rmdir(path,callback); | |
} | |
}; | |
// Empty directory to bail early | |
if(!wait) { | |
folderDone(); | |
return; | |
} | |
// Remove one or more trailing slash to keep from doubling up | |
path = path.replace(/\/+$/,""); | |
files.forEach(function(file) { | |
var curPath = path + "/" + file; | |
fs.lstat(curPath, function(err, stats) { | |
if( err ) { | |
callback(err, []); | |
return; | |
} | |
if( stats.isDirectory() ) { | |
rmdirAsync(curPath, folderDone); | |
} else { | |
fs.unlink(curPath, folderDone); | |
} | |
}); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment