Skip to content

Commit

Permalink
feat(watch): add on-change cli option
Browse files Browse the repository at this point in the history
  • Loading branch information
dolsem committed Sep 8, 2020
1 parent db5d2c3 commit ac98716
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
};

Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 14 additions & 1 deletion lib/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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();
}
Expand Down

0 comments on commit ac98716

Please sign in to comment.