Files
create-react-app/tasks/screencast-start.js
Mario Nebl 70ef42c3f3 Add screencast task (#3816)
* Automate screencast recordings
* **screencast.js**: Automate screencast.sh, asciinema, svg-term-cli. Removes progress-bar, npm tree data from cast
* **screencast.sh**: Simulate user input, trigger demoed commands
* **screencast-start.js**: Start a shell command and end the process log patterns have been observed
2018-01-16 19:07:45 +00:00

59 lines
1.2 KiB
JavaScript

#!/usr/bin/env node
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
const execa = require('execa');
const meow = require('meow');
const multimatch = require('multimatch');
main(meow());
function main(cli) {
let count = 0;
const start = Date.now();
const duration = parseInt(cli.flags.timeout, 10) * 1000;
const cp = execa.shell(cli.flags.command);
const target = parseInt(cli.flags.patternCount || '1', 10);
cp.stdout.on('data', data => {
process.stdout.write(data);
const matches = multimatch([String(data)], cli.flags.pattern);
const errMatches = multimatch([String(data)], cli.flags.errorPattern);
if (matches.length > 0) {
count++;
}
if (errMatches.length > 0) {
process.exit(1);
}
if (count >= target) {
setTimeout(() => {
process.exit(0);
}, duration);
}
});
cp.on('exit', e => {
const elapsed = Date.now() - start;
if (elapsed >= duration) {
return;
}
setTimeout(() => {
process.exit(e.code);
}, duration - elapsed);
});
}