最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Gulp task does not finish even when the process terminates with code 0 - Stack Overflow

programmeradmin4浏览0评论

I have added a gulp task to remove directories in the given paths. The paths are read from an array. My gulp task runs properly and does the required job. The Task Runner explorer give the message of starting the task as well as the process terminating successfully with code 0. The problem is that it doesn't state that the task has finished. Due to this, my other tasks that are dependent on this task, cannot execute during build process automation.

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);
    });
});

Inside Task Runner Explorer, the task starts but doesn't finish, though it terminates with code 0, as shown below:

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.

I have added a gulp task to remove directories in the given paths. The paths are read from an array. My gulp task runs properly and does the required job. The Task Runner explorer give the message of starting the task as well as the process terminating successfully with code 0. The problem is that it doesn't state that the task has finished. Due to this, my other tasks that are dependent on this task, cannot execute during build process automation.

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);
    });
});

Inside Task Runner Explorer, the task starts but doesn't finish, though it terminates with code 0, as shown below:

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.
Share Improve this question edited Jan 7, 2021 at 10:16 Srishti Gupta asked Jan 11, 2018 at 12:33 Srishti GuptaSrishti Gupta 1,2731 gold badge17 silver badges33 bronze badges 5
  • How about moving the return statement to : return rmr.sync(folder.Path); – Mark Commented Jan 11, 2018 at 21:02
  • Tried that too. Makes no difference! – Srishti Gupta Commented Jan 12, 2018 at 10:55
  • I would say what you want is forEach() rather than map(). map() returns an array which you are not interested in, you just want to run a function for each array item. [You don't really need each item in the array to be an object, they could just be the strings themselves unless you have other things in those objects besides Path.] – Mark Commented Jan 12, 2018 at 15:04
  • Using forEach() in place of map() also makes no difference. The process still terminates without calling finish. – Srishti Gupta Commented Jan 18, 2018 at 6:58
  • You're using the visual-studio-2015 tag, but it's not clear how your issue is related. Does this gulp issue only happen when run with the vs vode debugger? – Sergeon Commented Jan 18, 2018 at 7:15
Add a ment  | 

2 Answers 2

Reset to default 5

The correct way is to return a promise.

Given an iterable (array or string), or a promise of an iterable, promise.all iterates over all the values in the iterable into an array and returns a promise that is fulfilled when all the item(s) in the array is(are) fulfilled.

Following is the code snippet of the solution:

gulp.task('cleanFolders', function () {
    return Promise.all([
        removeFoldersArr.map(function (folder) {
            rmr.sync(folder.Path);
        })
    ]);
});

Now, the process first finishes and then gets terminated.

Following is the output as shown in 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.

After having this issue on VS2017, we looked to see that the issue we were having was related to version numbers 15.8.X, in which they shipped a new version of Node.js, version 10.6, and that basically broke gulp. It was terminating the process early with a code 0, but few if any of the processes were even allowed to finish.

Rearranging the web package management list to put $(PATH) before $(VSInstalledExternalTools) and having LTS version of Node on your path is what fixed it for us.

See: https://developermunity.visualstudio./content/problem/311553/task-runner-cant-load-gruntfilejs.html

发布评论

评论列表(0)

  1. 暂无评论