0

我正在尝试将 npm 包克隆到客户端位置,因此我创建了多个流来完成此操作并将它们合并以从gulp.task(). 但是,据我了解,我需要暂停流,以便任务接收正确的退出。

以下代码运行没有错误,但如果我评论该return行。其中一个包仍然被复制,当我希望没有任何流到达它们的gulp.dest(). 为什么不是所有的流都暂停了?

var gulp = require('gulp')
    , eventStream = require('event-stream')
    ;

const projects = {
    src: {
        dependencies: {
            codemirror: './src/lib/codemirror',
            acorn: './src/lib/acorn'
        }
    }
};

gulp.task('init:client-packages', function () {
    let streams = []
        , ps = eventStream.pause();

    // Load project client-side dependencies
    for (let prj in projects) {
        for (let pkg in projects[prj].dependencies) {
            streams.push(
                gulp.src('./node_modules/' + pkg + '/**')
                    .pipe(ps)
                    .pipe(gulp.dest(projects[prj].dependencies[pkg]))
            );
        }
    }

    // Merge source streams
    return eventStream.merge(streams).pipe(ps.resume());
    // ^^ Commenting this line doesn't block gulp.dest() from completing.
});
4

1 回答 1

0

可能存在错误,event-stream因为以下代码仅适用于merge-stream.

var merge = require('merge-stream')
    , eventStream = require('event-stream') 
    ;

gulp.task('init:client-packages', function() {
    let streams = [];

    // Load project client-side dependencies
    for (let prj in projects) {
        for (let pkg in projects[prj].dependencies) {
            streams.push(
                gulp.src('./node_modules/' + pkg + '/**')
                    .pipe(gulp.dest(projects[prj].dependencies[pkg]))
            );
        }
    }

    // Merge source streams
    return merge(streams);
    // --OR--
    return eventStream.merge(streams); // Results in message below.
});

如上所述event-stream,不能正确关闭任务。输出如下:

[23:36:46] Starting 'init:client-packages'...
[23:36:47] The following tasks did not complete: init:client-packages
[23:36:47] Did you forget to signal async completion?
Process terminated with code 1.
于 2018-07-10T03:35:49.587 回答