简体   繁体   English

Gulp任务无法同步运行

[英]Gulp tasks not running synchronously

I am trying to install packages through bower, and once the installation is finished, I want the files to be moved from dist folder to its parent, and then delete the dist folder. 我试图通过Bower安装软件包,安装完成后,我希望将文件从dist文件夹移至其父文件夹,然后删除dist文件夹。 But somehow, its not working properly. 但是不知何故,它无法正常工作。

var gulp = require('gulp'),
    bower = require('gulp-bower'),
    del = require('del'),
    fs = require('fs'),
    path = require('path'),
    merge = require('merge-stream');
    vinylPaths = require('vinyl-paths');
    runSequence = require('run-sequence');

var appPath="./www";
var folders = getFolders(appPath);

/* Function to get folders present in www */
function getFolders(dir) {
    return fs.readdirSync(dir)
      .filter(function(file) {
        return fs.statSync(path.join(dir, file)).isDirectory();
      });
}

gulp.task('clean', function (){
    var tasks = folders.map(function(folder) {
       return gulp.src(path.join(appPath, folder))
            .pipe(vinylPaths(del))
    });
});

/* Gulp task to run bower install */
gulp.task('bower', ['clean'], function() {
    return bower({cmd: "install"});

});

/* Gulp task to copy files from dist folder to their parent folders */
gulp.task('moveFiles', ['bower'], function (){
    var tasks = folders.map(function(folder) {
        console.log("FOLDER", folder);
        return gulp.src(path.join(appPath, folder, '/dist/**/*'))
            .pipe(gulp.dest(path.join(appPath, folder )));

    });
    return tasks;
});

/* Gulp task to clean the dist folder after files have been copied  */
gulp.task('delete-dist-folder', ['moveFiles'], function () {
    var tasks = folders.map(function(folder) {
           return gulp.src(path.join(appPath, folder, '/dist'))
                .pipe(vinylPaths(del))
    });
    return tasks;
});

gulp.task('default', ['delete-dist-folder']);

This is what I wrote till now. 这是我到目前为止写的。 After bower, the rest two tasks don't run. 凉亭之后,其余两个任务将不运行。 But if I run them individually, they work perfectly fine. 但是,如果我单独运行它们,它们可以很好地工作。

I am unable to figure out what is going wrong here. 我无法弄清楚这里出了什么问题。 Any help would be greatly appreciated. 任何帮助将不胜感激。

The problem is that you return arrays of event streams. 问题是您返回事件流的数组 Look at your delete-dist-folder task. 查看您的delete-dist-folder任务。 You return tasks and tasks is created from calling .map on an array. return tasks并且通过在数组上调用.map创建tasks So tasks is an array (of event streams). 因此, tasks是(事件流的)数组。

Gulp does not know what to do with your arrays and assumes that your tasks are synchronous. Gulp不知道如何处理数组,并假设您的任务是同步的。 There are only 3 ways to mark a task as asynchronous: 1) return an even stream; 只有三种方法可以将任务标记为异步:1)返回偶数流; Gulp will wait for the stream to end, 2) return a promise; Gulp将等待流结束,2)返回承诺; Gulp will wait for the promise to be resolved or rejected, or 3) declare your task callback to take a single parameter which you'll call when your task is done; Gulp将等待诺言被解决或拒绝,或者3)声明您的任务回调以采用单个参数,该参数将在任务完成时调用; Gulp will wait for the callback to be called. Gulp将等待回调被调用。 An array of event streams does not correspond to any of these options. 事件流数组与任何这些选项都不对应。

The Gulp wiki suggest using merge-stream to return more than one stream from a task. Gulp Wiki 建议使用merge-stream stream从任务返回多个流。 Judging by the code of merge-stream , and supposing you imported merge-stream as merge , you could do return merge.apply(undefined, tasks) . 根据merge-stream的代码判断,并假设您将merge-stream导入为merge ,则可以return merge.apply(undefined, tasks)

Side node: in your clean task you forgot to return a value. 侧节点:在clean任务中,您忘记了返回值。

I had a similar issue a while back when nesting task(requiring one task in another). 嵌套任务时(前一个任务需要另一个任务),我有类似的问题。 So to solve this issue and run task in sequence I used run-sequence . 因此,为了解决此问题并按顺序运行任务,我使用了run-sequence

You seem to have installed run-sequence . 您似乎已经安装了run-sequence Use it this way: 使用这种方式:

runSequence = require('run-sequence');

    gulp.task('default', function(callback) {
      runSequence('clean', 'bower', 'moveFiles', 'delete-dist-folder',
                 callback);
    });

Hope this might help you. 希望这对您有帮助。 You can refer this for any help. 您可以参考以获取任何帮助。 This is a temporary solution untill gulp 4.0 , which will include option to configure your task run in parallel or sequentially. gulp 4.0 ,这是一个临时解决方案,它将包括用于配置任务并行或顺序运行的选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM