let fs = require('fs');
function deleteFolder(path) {
var files = [];
if (fs.existsSync(path)) {
if (fs.statSync(path).isDirectory()) {
files = fs.readdirSync(path);
files.forEach(function (file, index) {
var curPath = path + "/" + file;
if (fs.statSync(curPath).isDirectory()) {
deleteFolder(curPath);
} else {
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
} else {
fs.unlinkSync(path);
}
}
}
function copyFolder(from, to) { // 复制文件夹到指定目录
let files = [];
if (fs.existsSync(to)) { // 文件是否存在 如果不存在则创建
files = fs.readdirSync(from);
files.forEach(function (file, index) {
var targetPath = from + "/" + file;
var toPath = to + '/' + file;
if (fs.statSync(targetPath).isDirectory()) { // 复制文件夹
copyFolder(targetPath, toPath);
} else { // 拷贝文件
fs.copyFileSync(targetPath, toPath);
}
});
} else {
fs.mkdirSync(to);
copyFolder(from, to);
}
}
使用:
deleteFolder('./resource');
copyFolder('./resource', './other/resource');