-
-
Save heruputra/7894384 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') | |
, path = require('path') | |
, _ = require('underscore'); | |
var rootPath = "/path/to/remove"; | |
removeDirForce(rootPath); | |
// path should have trailing slash | |
function removeDirForce(dirPath) { | |
fs.readdir(dirPath, function(err, files) { | |
if (err) { | |
console.log(JSON.stringify(err)); | |
} else { | |
if (files.length === 0) { | |
fs.rmdir(dirPath, function(err) { | |
if (err) { | |
console.log(JSON.stringify(err)); | |
} else { | |
var parentPath = path.normalize(dirPath + '/..') + '/'; | |
if (parentPath != path.normalize(rootPath)) { | |
removeDirForce(parentPath); | |
} | |
} | |
}); | |
} else { | |
_.each(files, function(file) { | |
var filePath = dirPath + file; | |
fs.stat(filePath, function(err, stats) { | |
if (err) { | |
console.log(JSON.stringify(err)); | |
} else { | |
if (stats.isFile()) { | |
fs.unlink(filePath, function(err) { | |
if (err) { | |
console.log(JSON.stringify(err)); | |
} | |
}); | |
} | |
if (stats.isDirectory()) { | |
removeDirForce(filePath + '/'); | |
} | |
} | |
}); | |
}); | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was used to cheat a test. Random curiosity :)