mafintosh/pump

Multiple pumps inside a single task

janrembold opened this issue · 4 comments

Is there any best-practice-way of using multiple pumps inside a single task and return the callback after all of them finished?

Maybe a simplified demo code clearifies the question:

gulp.task('compress', function (cb) {
  const folders = ['folder1', 'folder2'];

  folders.forEach((folder) => {
    pump([
        gulp.src(`lib/${folder}/*.js`),
        uglify(),
        gulp.dest(`${folder}/dist/`)
      ],
      cb // <= this is wrong of course
    );
  });  

  // cb(); // calling it here might be as wrong as well
});

This is, of course, not a real life code snippet. But it shows the need to run multiple pumps inside a single task. In my real case the source and destination folders are a little bit more complex.

Is there any way to solve that with pump?

We solved this by adding Promises around each pump and call the callback function after all promises got resolved. See https://github.com/biotope/biotope-build/blob/master/tasks/uglify.js#L25

@janrembold Hi, how to make the code work in the above example? I face the same problem and it throws the error: task completion callback called too many times. My codes:

gulp.task('script', function(cb) {
	pump([
		gulp.src('file.js'),
		sourcemaps.init(),
		uglifyJs({}),
		concat('scripts.common.min.js'),
		sourcemaps.write('.'),
		gulp.dest(scriptBuiltPath)
	], cb);
	pump([
		gulp.src('another-file.js'),
		sourcemaps.init(),
		uglifyJs({}),
		rename({ extname: '.min.js' }),
		sourcemaps.write('.'),
		gulp.dest(scriptBuiltPath)
	], cb);
})

@zhuchuji Please see the link above for a working example using Promises. You should only call cb() once

@janrembold Got it. Thank you.