我添加了一个 gulp 任务来删除给定路径中的目录。路径是从数组中读取的。 我的 gulp 任务正常运行并完成所需的工作。 Task Runner 资源管理器给出启动任务的消息以及进程以代码 0 成功终止。问题是它没有说明任务已完成。因此,依赖于此任务的我的其他任务无法在构建过程自动化期间执行。
const rmr = require('rmr');
// array containing the list of all paths of the folders to be deleted
const removeFoldersArr = [
{
Path: "wwww/scripts"
},
{
Path: "www/styles"
}
];
// Gulp Task to remove all files in a folder
gulp.task('cleanFolders', function () {
return removeFoldersArr.map(function (folder) {
rmr.sync(folder.Path);
});
});
在 Task Runner Explorer 中,任务开始但没有完成,尽管它以代码 0 终止,如下所示:
cmd.exe /c gulp -b "D:\My Projects\Solution1" --color --gulpfile "D:\My Projects\Solution1\Gulpfile.js" cleanFolders
[18:04:23] Using gulpfile D:\My Projects\Solution1\Gulpfile.js
[18:04:23] Starting 'cleanFolders'...
Process terminated with code 0.
最佳答案
正确的方法是返回一个promise。
给定一个可迭代对象(数组或字符串),或一个可迭代对象的 promise ,promise.all 将迭代对象中的所有值迭代到一个数组中,并返回一个 promise ,当数组中的所有项目都满足时is(are) fulfilled.
以下是解决方案的代码片段:
gulp.task('cleanFolders', function () {
return Promise.all([
removeFoldersArr.map(function (folder) {
rmr.sync(folder.Path);
})
]);
});
现在,进程首先完成,然后终止。
以下是 Task Runner Explorer 中显示的输出:
cmd.exe /c gulp -b "D:\My Projects\Solution1" --color --gulpfile "D:\My Projects\Solution1\Gulpfile.js" cleanFolders
[12:25:01] Using gulpfile D:\My Projects\Solution1\Gulpfile.js
[12:45:01] Starting 'cleanFolders'...
[12:45:01] Finished 'cleanFolders' after 3.18 ms
Process terminated with code 0.
关于javascript - 即使进程以代码 0 终止,Gulp 任务也不会完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48207194/