2019-11-15 23:20:34 +08:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const u = require('universalify').fromCallback
|
|
|
|
const fs = require('graceful-fs')
|
|
|
|
const path = require('path')
|
|
|
|
const mkdir = require('../mkdirs')
|
|
|
|
const remove = require('../remove')
|
|
|
|
|
|
|
|
const emptyDir = u(function emptyDir (dir, callback) {
|
|
|
|
callback = callback || function () {}
|
|
|
|
fs.readdir(dir, (err, items) => {
|
|
|
|
if (err) return mkdir.mkdirs(dir, callback)
|
|
|
|
|
|
|
|
items = items.map(item => path.join(dir, item))
|
|
|
|
|
|
|
|
deleteItem()
|
|
|
|
|
|
|
|
function deleteItem () {
|
|
|
|
const item = items.pop()
|
|
|
|
if (!item) return callback()
|
|
|
|
remove.remove(item, err => {
|
|
|
|
if (err) return callback(err)
|
|
|
|
deleteItem()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
function emptyDirSync (dir) {
|
|
|
|
let items
|
|
|
|
try {
|
|
|
|
items = fs.readdirSync(dir)
|
2020-03-23 17:00:06 +08:00
|
|
|
} catch {
|
2019-11-15 23:20:34 +08:00
|
|
|
return mkdir.mkdirsSync(dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
items.forEach(item => {
|
|
|
|
item = path.join(dir, item)
|
|
|
|
remove.removeSync(item)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
emptyDirSync,
|
|
|
|
emptydirSync: emptyDirSync,
|
|
|
|
emptyDir,
|
|
|
|
emptydir: emptyDir
|
|
|
|
}
|