Skip to content

Instantly share code, notes, and snippets.

@yuqianma
Created January 13, 2017 15:45
Show Gist options
  • Save yuqianma/3b28074255f1d8798ca998cf62980874 to your computer and use it in GitHub Desktop.
Save yuqianma/3b28074255f1d8798ca998cf62980874 to your computer and use it in GitHub Desktop.
const fs = require('fs')
function rmPath(path, cb) {
fs.lstat(path, (err, stats) => {
//console.log('retrieve:', path)
if (stats.isDirectory()) {
fs.readdir(path, (err, files) => {
let len = files.length + 1
let checkDir = () => {
//console.log(path + ' left:', len-1)
if (!--len) {
//console.log('remove dir:', path)
fs.rmdir(path, cb)
}
}
checkDir()
files.map(f => rmPath(path + '/' + f, checkDir))
})
return
}
//console.log('remove file:', path)
fs.unlink(path, cb)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment