Search code examples
node.jsgulpgulp-shellnode-commander

Commander-plus with gulp


I'm writing a node cli tool with commander-plus.

import program from 'commander-plus';

const prompts = ['a', 'b', 'c'];
program.choose(prompts, (index) => {
  // never returns;
});

And want to run it with a gulp task, mostly because its convenient and we're loading .env variables, but on development only.

import env from 'gulp-env';   
gulp.task('env', () => {
  env();
});

At first i've been trying with gulp-shell. I'm actually using a similar script to kick-off nodemon, which works fine. The cli script runs just fine but commander-plus won't listen to the keyboard input.

import shell from 'gulp-shell';
import gulp from 'gulp';

gulp.task('cli', ['env'], shell.task([
  'babel-node src/cli',
]))

Later i found that, either this is how its supposed to work or perhaps its now fixed. https://github.com/sun-zheng-an/gulp-shell/issues/10

But also that gulp-shell is blacklisted, and thought to try with gulp-exec or child_process.exec instead.

import { exec } from 'child_process';
gulp.task('cli', ['env'], done => {
  exec('babel-node src/server/cli', done);
});

Solution

  • Apparently gulp-bg is a working option. With that we can still continue running developer tasks with gulp and avoid dotenv entirely on production.

    import bg from 'gulp-bg';
    import gulp from 'gulp';
    
    gulp.task('cli', ['env'], bg('node', './src/cli'));