From 339d049956a4cf669dc9ef12b1efb342ee89061b Mon Sep 17 00:00:00 2001 From: Jarid Margolin Date: Thu, 16 Mar 2017 15:13:58 -0400 Subject: [PATCH] Add `runCommand` helper method to Neutrino. --- packages/neutrino/src/neutrino.js | 27 +++++++++------------------ packages/neutrino/test/api_test.js | 11 +++++++++++ 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/packages/neutrino/src/neutrino.js b/packages/neutrino/src/neutrino.js index 0f0a7d3..2c5d4d0 100644 --- a/packages/neutrino/src/neutrino.js +++ b/packages/neutrino/src/neutrino.js @@ -68,31 +68,22 @@ class Neutrino extends EventEmitter { } build(args) { - return this - .emitForAll('prebuild', args) - .then(() => this.builder()) - .then(() => this.emitForAll('build', args)); + return this.runCommand('build', args, () => this.builder()); } start(args) { - return this - .emitForAll('prestart', args) - .then(() => { - const config = this.getWebpackOptions(); - - if (config.devServer) { - return this.devServer(); - } - - return this.watcher(); - }) - .then(() => this.emitForAll('start', args)); + return this.runCommand('start', args, () => (this.getWebpackOptions().devServer ? this.devServer() : this.watcher())); } test(args) { + return this.runCommand('test', args); + } + + runCommand(command, args = {}, fn) { return this - .emitForAll('pretest', args) - .then(() => this.emitForAll('test', args)); + .emitForAll(`pre${command}`, args) + .then(fn) + .then(() => this.emitForAll(command, args)); } devServer() { diff --git a/packages/neutrino/test/api_test.js b/packages/neutrino/test/api_test.js index cabdae5..9ef0ab6 100644 --- a/packages/neutrino/test/api_test.js +++ b/packages/neutrino/test/api_test.js @@ -94,6 +94,17 @@ test('import middleware for use', t => { t.notDeepEqual(api.getWebpackOptions(), {}); }); +test('command emits events around execution', async (t) => { + const api = new Neutrino(); + const events = []; + + api.on('prebuild', () => events.push('alpha')); + api.on('build', () => events.push('gamma')); + + await api.runCommand('build', {}, () => events.push('beta')); + t.deepEqual(events, ['alpha', 'beta', 'gamma']); +}); + test('creates a Webpack config', t => { const api = new Neutrino();