官方文档:https://github.com/sindresorhus/del
使用全局列表删除文件和目录。与rimraf类似,但具有承诺的API,并支持多个文件和全局匹配。它还可以防止您删除当前工作目录和以上目录。
安装:
$ npm install del
使用:
const del = require('del');
(async () => {
const deletedFilePaths = await del(['temp/*.js', '!temp/unicorn.js']);
const deletedDirectoryPaths = await del(['temp', 'public']);
console.log('Deleted files:\n', deletedFilePaths.join('\n'));
console.log('\n\n');
console.log('Deleted directories:\n', deletedDirectoryPaths.join('\n));
})();
注意:
通配符模式**匹配所有子元素和父元素。所以下面的代码不会执行:
del.sync(['public/assets/**', '!public/assets/goat.png']);
您也必须显式地忽略父目录:
del.sync(['public/assets/**', '!public/assets', '!public/assets/goat.png']);
要删除public/中的所有子目录,你可以做:
del.sync(['public/*/']);
注意,通配符模式只能包含前斜杠,而不能包含后斜杠。Windows文件路径可以使用反斜杠,只要路径不包含任何类似全局的字符,否则使用path.posix.join()代替path.join()
API:
del(patterns, options?)
返回Promise<string[]>和删除的路径
del.sync(patterns, options?)
返回包含已删除路径的字符串[]
patterns默认是:字符串或[str,str1,str2]
options参数:对象
{
force: false, //允许删除当前工作目录和外部
dryRun: false //看看什么会被删除
}
例子:
const del = require('del');
(async () => {
const deletedPaths = await del(['temp/*.js'], {dryRun: true});
console.log('Files and directories that would be deleted:\n', deletedPaths.join('\n'));
})();
项目中使用:
var del = require("del");
// 删除
gulp.task("del", function() {
del("./webapp_publish"); // 删除整个dist文件夹
del(SRC + "/static/page_concat_resource/js/*");
// del(SRC + "/static/page_concat_resource/css/*");//后续修改完成concat.json 之后放开
});