-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoveOut.js
More file actions
33 lines (31 loc) · 1.02 KB
/
Copy pathremoveOut.js
File metadata and controls
33 lines (31 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* @caption 删除本地文件夹/文件
* @param {String} directoryPath 例: d://x/x/x.json 或者 d://x/x/x
* *@param {Function} callback 例:回调
*/
let _deleteDir = (directoryPath, callback) => {
const fs = require('fs').promises;
async function rmdirAsync (directoryPath) {
try {
let stat = await fs.stat(directoryPath)
if (stat.isFile()) {
await fs.unlink(directoryPath)
} else {
let dirs = await fs.readdir(directoryPath)
// 递归删除文件夹内容(文件/文件夹)
dirs = dirs.map(dir => rmdirAsync(require("path").join(directoryPath, dir)))
await Promise.all(dirs)
await fs.rmdir(directoryPath)
}
} catch (e) {
console.error(e);
}
}
require('fs').existsSync(directoryPath) && rmdirAsync(directoryPath).then(() => {
// 确保文件/文件夹均已删除 => 回调
callback && callback();
})
}
_deleteDir("dist/");
// _deleteDir("build/");
_deleteDir("lib/");