0

I'm trying clone npm packages to a client-side location, so I have created multiple streams to accomplish this and merge them to return from gulp.task(). However, it is my understanding that I will need to pause the streams in order for the task to receive the proper exit.

The following code runs without error, but if I comment the return line. One of the packages is still copied, when I would expect none of the streams to reach their gulp.dest(). Why aren't all the streams paused?

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.
});
roydukkey
  • 3,149
  • 2
  • 27
  • 43

1 Answers1

0

There maybe a bug in event-stream as the following code only works with 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.
});

As stated above event-stream doesn't close the task properly. The output is as follows:

[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.
roydukkey
  • 3,149
  • 2
  • 27
  • 43
  • can also try `return merge(streams).addListener('end', done)` and pass done to the task function `gulp.task('init:client-packages', function(done) {` – 1800 INFORMATION Sep 16 '19 at 03:10