commit 5c3bc7f31e102027f594ff297964da9b864b6989
parent 2704e0ebff2e1cc64f9dfb9a99eec4382b12dce0
Author: Harald Rudell <harald@therudells.com>
Date: Thu, 14 Jul 2016 20:46:51 -0700
Detect server exit while cb is pending, proper status code on cli-run failure (#742)
* Detect server exit while cb is pending, proper status code on cli-run failure
* Complying with linting from the 70s
* Allow callers of run to get resolution value
Diffstat:
2 files changed, 13 insertions(+), 2 deletions(-)
diff --git a/tools/run.js b/tools/run.js
@@ -17,19 +17,20 @@ function run(fn, options) {
console.log(
`[${format(start)}] Starting '${task.name}${options ? `(${options})` : ''}'...`
);
- return task(options).then(() => {
+ return task(options).then(resolution => {
const end = new Date();
const time = end.getTime() - start.getTime();
console.log(
`[${format(end)}] Finished '${task.name}${options ? `(${options})` : ''}' after ${time} ms`
);
+ return resolution;
});
}
if (require.main === module && process.argv.length > 2) {
delete require.cache[__filename]; // eslint-disable-line no-underscore-dangle
const module = require(`./${process.argv[2]}.js`).default;
- run(module).catch(err => console.error(err.stack));
+ run(module).catch(err => { console.error(err.stack); process.exit(1); });
}
export default run;
diff --git a/tools/runServer.js b/tools/runServer.js
@@ -20,6 +20,8 @@ const serverPath = path.join(output.path, output.filename);
// Launch or restart the Node.js server
function runServer(cb) {
+ let cbIsPending = !!cb;
+
function onStdOut(data) {
const time = new Date().toTimeString();
const match = data.toString('utf8').match(RUNNING_REGEXP);
@@ -31,6 +33,7 @@ function runServer(cb) {
server.stdout.removeListener('data', onStdOut);
server.stdout.on('data', x => process.stdout.write(x));
if (cb) {
+ cbIsPending = false;
cb(null, match[1]);
}
}
@@ -44,6 +47,13 @@ function runServer(cb) {
env: Object.assign({ NODE_ENV: 'development' }, process.env),
silent: false,
});
+ if (cbIsPending) {
+ server.once('exit', (code, signal) => {
+ if (cbIsPending) {
+ throw new Error(`Server terminated unexpectedly with code: ${code} signal: ${signal}`);
+ }
+ });
+ }
server.stdout.on('data', onStdOut);
server.stderr.on('data', x => process.stderr.write(x));