From 7cbbb0c2b9ee6fa2e1126f1e34db1e19b1725728 Mon Sep 17 00:00:00 2001 From: Marcus Stade Date: Mon, 29 May 2017 18:49:54 +0200 Subject: [PATCH] Add capability to execute command on successful interactive builds --- README.md | 6 ++++-- src/cli/opts.js | 2 +- src/main.js | 26 +++++++++++++++++++++++--- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ed18492..a215856 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ $ ez-build --help --no-copy disable copying of non-code files to lib --no-debug disable source map generation --log log output format [normal] - --interactive watch for and recompile on changes (implies -O 0) + --interactive [cmd] watch for and recompile on changes, optionally executing cmd on successful builds (implies -O 0) --production enable production options (implies -O 1) --flags toggle build flags @ read options from the file at (relative to cwd) @@ -126,10 +126,12 @@ By default ez-build will generate source maps and other debugging information fo Determines the output format of ez-build's log. This is generally not used, but setting it to `json` provides additional detail and can sometimes help in debugging issues. -### `--interactive` +### `--interactive [cmd]` Runs ez-build in interactive mode, meaning it will run continuously and watch for changes to input files and directories. This is very useful for rapid development, since it's much faster to rebuild only what changed than the entire project. Setting this flag implies `-O 0` which disables all optimizations. +Optionally, this mode can execute a command on successful builds. For example, this might be used to run a test suite whenever a build passes. Commands that include spaces *must* be properly quoted with double quotes. + This flag is ignored entirely if combined with `--production`. If `NODE_ENV=production` is set when invoking ez-build with `--interactive`, it will still enter interactive mode, but will *not* change the value of `NODE_ENV`. diff --git a/src/cli/opts.js b/src/cli/opts.js index 776e70f..3b31a26 100644 --- a/src/cli/opts.js +++ b/src/cli/opts.js @@ -47,7 +47,7 @@ export default async function parse(pkg, argv) { .option('--no-copy', `disable copying of non-code files to ${defaults.lib}`, Boolean, !defaults.copy) .option('--no-debug', 'disable source map generation', Boolean, !defaults.debug) .option('--log ', `log output format [${defaults.log}]`, /^(json|normal)$/i, defaults.log) - .option('--interactive', `watch for and recompile on changes (implies -O 0)`) + .option('--interactive [cmd]', `watch for and recompile on changes (implies -O 0)`) .option('--production', `enable production options (implies -O 1)`) .option('--target-browsers ', `define target browser environments: [${defaults.targetBrowsers}]`, concatFlags, []) .option('--target-node ', `define target node environment: [${defaults.targetNode}]`, defaults.targetNode) diff --git a/src/main.js b/src/main.js index 0f75bb6..59d4f13 100644 --- a/src/main.js +++ b/src/main.js @@ -12,6 +12,7 @@ import { watch } from 'chokidar' import { timed } from './util/performance' import './util/cli' import { default as parseOpts } from './cli/opts' +import { execSync } from 'child_process' const keys = Object.keys const all = Promise.all.bind(Promise) @@ -76,9 +77,28 @@ async function main() { .on('add', build) .on('change', build) + let cmd = typeof opts.interactive === 'string'? opts.interactive : false + async function build(file) { - let result = await execute(type, pipeline[type], file) - await status(type, ...result) + let results = await execute(type, pipeline[type], file) + await status(type, ...results) + + if (cmd) { + let success = true + + for (let result of results) { + let { input, messages, files, error } = await result + + if (error) { + success = false + break + } + } + + if (success) { + execSync(cmd, { stdio: 'inherit' }) + } + } } }) console.info('Watching source files for changes...') @@ -235,4 +255,4 @@ Comments on the detected babel configuration: } return !!babelrc -} \ No newline at end of file +}