ap-o
ap-o

Reputation: 170

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);
});

Upvotes: 0

Views: 158

Answers (2)

ap-o
ap-o

Reputation: 170

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'));

Upvotes: 0

ap-o
ap-o

Reputation: 170

Not sure if it qualifies as an answer, but i found way around with node-dotenv. Just without gulp.

First i have a config file that looks like

// only set default for env
configExport.env = process.env.NODE_ENV || 'development';
// and a lot of other variables

And then both in my server and cli tool i load the .env only if its needed.

import dotenv from 'dotenv'
import config from '../server/config';
if(config.env === 'development'){
  dotenv.load();
}

dotenv will not fail if .env file is not found, but the reason for the extra if check, is i'm worried a .env might get deployed by accident.

We will also need to make sure .env is not deployed with all appropriate .ignores (.gitignore, modulusignore, .dockerignore) and that should do the job.

Upvotes: 0

Related Questions