Browse Source

Set environment variable, NODE_ENV, inside of `runCommand` (also adds --env option to CLI).

v6-dev
Jarid Margolin 8 years ago
parent
commit
27b8011be1
  1. 8
      packages/neutrino/bin/neutrino
  2. 7
      packages/neutrino/src/neutrino.js
  3. 16
      packages/neutrino/test/api_test.js

8
packages/neutrino/bin/neutrino

@ -26,6 +26,11 @@ const args = yargs
default: [],
global: true
})
.option('env', {
description: 'The value for the environment variable, NODE_ENV',
string: true,
global: true
})
.command('start', 'Build a project in development mode')
.command('build', 'Compile the source directory to a bundled build')
.command('test [files..]', 'Run all suites from the test directory or provided files', {
@ -48,7 +53,6 @@ const args = yargs
.argv;
function run(command, args) {
const environments = { build: 'production', start: 'development', test: 'test' };
const pkg = optional(join(process.cwd(), 'package.json')) || {};
const pkgMiddleware = pathOr([], ['neutrino', 'use'], pkg);
const middleware = [...new Set(pkgMiddleware.concat(args.use))];
@ -56,8 +60,6 @@ function run(command, args) {
const config = pathOr({}, ['neutrino', 'config'], pkg);
const api = new Neutrino(Object.assign(options, { config }));
process.env.NODE_ENV = environments[command];
// Grab all middleware and merge them into a single webpack-chain config instance
api.import(middleware);

7
packages/neutrino/src/neutrino.js

@ -5,6 +5,7 @@ const webpack = require('webpack');
const Config = require('webpack-chain');
const ora = require('ora');
const merge = require('deepmerge');
const { defaultTo } = require('ramda');
const requireMiddleware = require('./requireMiddleware');
const normalizePath = (path, root) => (isAbsolute(path) ? path : join(root, path));
@ -80,6 +81,12 @@ class Neutrino extends EventEmitter {
}
runCommand(command, args = {}, fn) {
process.env.NODE_ENV = defaultTo({
build: 'production',
start: 'development',
test: 'test'
}[command], args.env);
return this
.emitForAll(`pre${command}`, args)
.then(fn)

16
packages/neutrino/test/api_test.js

@ -94,6 +94,22 @@ test('import middleware for use', t => {
t.notDeepEqual(api.getWebpackOptions(), {});
});
test('command sets correct NODE_ENV', t => {
const api = new Neutrino();
api.runCommand('build');
t.is(process.env.NODE_ENV, 'production');
api.runCommand('start');
t.is(process.env.NODE_ENV, 'development');
api.runCommand('test');
t.is(process.env.NODE_ENV, 'test');
api.runCommand('build', { env: 'development' });
t.is(process.env.NODE_ENV, 'development');
});
test('command emits events around execution', async (t) => {
const api = new Neutrino();
const events = [];

Loading…
Cancel
Save