m19c / gulp-run

Pipe to shell commands in gulp
ISC License
151 stars 25 forks source link

Running commands in sequence? #46

Closed miparnisari closed 7 years ago

miparnisari commented 7 years ago

I want to run two commands, namely redis-cli flushall and a custom script node gen.js in sequence. This is my current task:

gulp.task('gen', function (done) {
  run('redis-cli flushall').exec('', function () {
    console.log('calling done after redis')
    run('node gen.js').exec('', function () {
      console.log('calling done after gen.js')
      done();
    })
  });
});

However, I see two issues with that code:

1) If I want to add more commands, this quickly becomes callback hell. I would like something cleaner as in with Promises, with then. 2) The result I get is this:

λ gulp gen
[18:22:28] Using gulpfile ~\Documents\app\gulpfile.js
[18:22:28] Starting 'gen'...
calling done after redis
$ redis-cli flushall
OK
calling done after gen.js
[18:22:51] Finished 'gen' after 22 s
$ node gen.js
DB dropped.

...but I expected this:

λ gulp gen
[18:22:28] Using gulpfile ~\Documents\app\gulpfile.js
[18:22:28] Starting 'gen'...
$ redis-cli flushall
OK
calling done after redis
$ node gen.js
DB dropped.
calling done after gen.js
[18:22:51] Finished 'gen' after 22 s
m19c commented 7 years ago

Good idea. Would love to see a pull request.

miparnisari commented 7 years ago

Oh, I thought this was already possible and I just wasn't seeing it haha.

I'm not sure how to go about this. I'm not a Node expert but it seems that if the node scripts returned a stream that can then be piped into another command it should work?

m19c commented 7 years ago

Jep. This works.

command()
  .pipe(...)
  .pipe(...)
  .pipe(...)
;
miparnisari commented 7 years ago

Hmm.. I changed the gulp task to this:

gulp.task('gen', function () {
   return gulp.src('.')
    .pipe(prompt.confirm({
        message: 'Clear Redis and regenerate SQL DB?',
        default: false
    }))
    .pipe(run('redis-cli flushall'))
    .pipe(run('node config/gen.js'))
    .pipe(run('node config/fill_users.js'))
});

And this is what I got. Notice how the order of execution is wrong for the node scripts:

λ gulp gen
[23:09:18] Using gulpfile ~\Documents\app\gulpfile.js
[23:09:18] Starting 'gen'...
? Clear Redis and regenerate SQL DB? Yes
$ redis-cli flushall
OK
$ node config/fill_users.js
finished creating users
$ node config/gen.js
DB dropped.
[23:09:47] Finished 'gen' after 29 s

Not sure if this might be a problem or not, but this is the gen.js script. When run on its own it takes some time.

var sequelize = require('sequelize');
return sequelize
  .query('SET FOREIGN_KEY_CHECKS = 0')
  .then(function () {
    return models.sequelize.sync({ force: true })
  })
  .then(function () {
    return models.sequelize.query('SET FOREIGN_KEY_CHECKS = 1')
  })
  .then(function () {
    console.log('DB dropped.');
    process.exit(0);
  })
  .catch(function (err) {
    console.error(err);
    process.exit(-1);
  })