Skip to content

Commit

Permalink
Add capability to execute command on successful interactive builds
Browse files Browse the repository at this point in the history
  • Loading branch information
mstade committed Mar 1, 2018
1 parent 4e72767 commit 7cbbb0c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ $ ez-build --help
--no-copy disable copying of non-code files to lib
--no-debug disable source map generation
--log <normal|json> 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 <flags> toggle build flags
@<path> read options from the file at <path> (relative to cwd)
Expand Down Expand Up @@ -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`.
Expand Down
2 changes: 1 addition & 1 deletion src/cli/opts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <normal|json>', `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 <spec|false>', `define target browser environments: [${defaults.targetBrowsers}]`, concatFlags, [])
.option('--target-node <current|number|false>', `define target node environment: [${defaults.targetNode}]`, defaults.targetNode)
Expand Down
26 changes: 23 additions & 3 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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...')
Expand Down Expand Up @@ -235,4 +255,4 @@ Comments on the detected babel configuration:
}

return !!babelrc
}
}

0 comments on commit 7cbbb0c

Please sign in to comment.