From ac987164c761eb1110fd098558e7ca5379b39100 Mon Sep 17 00:00:00 2001 From: Denis Olsem Date: Mon, 7 Sep 2020 21:08:36 -0700 Subject: [PATCH] feat(watch): add on-change cli option --- lib/cli.js | 7 ++++++- lib/watcher.js | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/cli.js b/lib/cli.js index ef93cf5a76..8471deae38 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -75,6 +75,10 @@ const FLAGS = { coerce: coerceLastValue, description: 'Re-run tests when files change', type: 'boolean' + }, + 'on-change': { + description: 'Extra command to run when files change', + type: 'string' } }; @@ -457,7 +461,8 @@ exports.run = async () => { // eslint-disable-line complexity globs, projectDir, providers, - reporter + reporter, + onChange: combined['on-change'] }); watcher.observeStdin(process.stdin); } else { diff --git a/lib/watcher.js b/lib/watcher.js index 6c6511f1df..0ce58e1e41 100644 --- a/lib/watcher.js +++ b/lib/watcher.js @@ -78,7 +78,7 @@ class TestDependency { } class Watcher { - constructor({api, filter = [], globs, projectDir, providers, reporter}) { + constructor({api, filter = [], globs, projectDir, providers, reporter, onChange}) { this.debouncer = new Debouncer(this); this.clearLogOnNextRun = true; @@ -147,6 +147,18 @@ class Watcher { .catch(rethrowAsync); }; + this.onChange = onChange && ((event) => { + try { + const output = require('child_process').execSync(onChange, { + env: { AVA_WATCH_EVENT: event, ...process.env }, + }); + if (output) reporter.lineWriter.writeLine(output.toString()); + } catch (err) { + reporter.endRun(); + process.exit(1); + } + }); + this.testDependencies = []; this.trackTestDependencies(api); @@ -172,6 +184,7 @@ class Watcher { }).on('all', (event, path) => { if (event === 'add' || event === 'change' || event === 'unlink') { debug('Detected %s of %s', event, path); + if (this.onChange) this.onChange(event); this.dirtyStates[nodePath.join(this.globs.cwd, path)] = event; this.debouncer.debounce(); }