Search code examples
gulpgulp-inject

Gulp streams firing end events when in parallel


Here's the interesting gulpfile:

gulp.task 'index', ['coffee:build', 'templates:build', 'bower', 'sass:build', 'frontend:assets'], ->
  bowerCss = gulp.src ["bower_components/**/*.css"], cwd: 'frontend'
  appJs = gulp.src ["scripts/**/*.js"], cwd: 'frontend'
  appCss = gulp.src ["**/*.css", '!bower_components/**'], cwd: 'frontend'
  return gulp.src 'web/index.html'
  .pipe plugins.inject appJs.pipe(plugins.angularFilesort())
  .pipe plugins.inject appCss
  .pipe plugins.debug()
  .pipe plugins.inject bowerCss, {name: 'bower'}
  .pipe plugins.debug()
  .pipe gulp.dest 'frontend'

Calling it ends up injecting some filenames into my index.html. It works fine.

When I call it as part of a bigger batch:

gulp.task 'develop', [
  'backend:coffee'
  'backend:copy'
  'backend:serve'
  'frontend:serve' # index is required by frontend:serve
  'coffee:watch'
  'sass:watch'
  'templates:watch']

It just sort of fizzles. Instead of getting reasonable-looking outputs from gulp-inject and the gulp-debug, I get the following from gulp-debug:

[23:06:59] Starting 'index'...
[23:06:59] Starting 'templates:watch'...
[23:06:59] gulp-debug: end event fired (2014-08-03 06:06:59 UTC)
[23:06:59] gulp-debug: end event fired (2014-08-03 06:06:59 UTC)
[23:06:59] Finished 'index' after 188 ms

It's worth noting that I can do gulp frontend:serve and it works fine. This only errors out when I'm running my "just launch everything" command. I imagine I'm missing some gotcha with parallelizing or a race condition, but it's weird that the end event just fires off willy-nilly like that.

Right now with this set up, I can do gulp frontend:serve, gulp backend:serve on two different terminals and it's fine. But if I do gulp frontend:serve backend:serve or gulp debug (which is effectively the same, it doesn't rebuild my index file.

Any pointers? Thanks.


Solution

  • Aha, got it. It was actually gulp-nodemon, which was setting the process-wide working directory when I set it up with:

    gulp.task 'backend:serve', ['backend:build', 'backend:copy'],  ->
      plugins.nodemon
        cwd: config.paths.backendBuild
        script: "server.js"
    

    I should have done

    gulp.task 'backend:serve', ['backend:build', 'backend:copy'],  ->
      plugins.nodemon
        watch: config.paths.backendBuild
        script: "backend/server.js"
    

    instead. I'm not sure if setting process.cwd() in nodemon is a great idea, but it makes sense given that this behavior probably comes from nodemon, which probably doesn't expect to be running as part of a bigger project.