From 371084d30c559043679c6338ed5ca8a87a5106e4 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Wed, 18 Mar 2020 15:55:05 +0100 Subject: [PATCH] cli: add helper to wait for child to exit + use to fix watch-deps --- packages/cli/src/commands/watch-deps/child.ts | 1 + packages/cli/src/commands/watch-deps/index.ts | 3 +- packages/cli/src/helpers/run.ts | 39 ++++++++++++------- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/packages/cli/src/commands/watch-deps/child.ts b/packages/cli/src/commands/watch-deps/child.ts index a5fcae0401..a18f384538 100644 --- a/packages/cli/src/commands/watch-deps/child.ts +++ b/packages/cli/src/commands/watch-deps/child.ts @@ -34,4 +34,5 @@ export function startChild(args: string[]) { child.stderr!.on('data', data => { log.err(data.toString('utf8')); }); + return child; } diff --git a/packages/cli/src/commands/watch-deps/index.ts b/packages/cli/src/commands/watch-deps/index.ts index d2c14709c0..6ea1fae23b 100644 --- a/packages/cli/src/commands/watch-deps/index.ts +++ b/packages/cli/src/commands/watch-deps/index.ts @@ -22,6 +22,7 @@ import { getPackageDeps } from './packages'; import { startWatcher, startPackageWatcher } from './watcher'; import { startCompiler } from './compiler'; import { startChild } from './child'; +import { waitForExit } from '../../helpers/run'; const PACKAGE_BLACKLIST = [ // We never want to watch for changes in the cli, but all packages will depend on it. @@ -67,6 +68,6 @@ export default async (_command: any, args: string[]) => { }); if (args?.length) { - startChild(args); + await waitForExit(startChild(args)); } }; diff --git a/packages/cli/src/helpers/run.ts b/packages/cli/src/helpers/run.ts index 46ace5733e..52be518459 100644 --- a/packages/cli/src/helpers/run.ts +++ b/packages/cli/src/helpers/run.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { SpawnSyncOptions, spawn } from 'child_process'; +import { SpawnSyncOptions, spawn, ChildProcess } from 'child_process'; import { ExitCodeError } from './errors'; // Runs a child command, returning a promise that is only resolved if the child exits with code 0. @@ -23,20 +23,33 @@ export async function run( args: string[] = [], options: SpawnSyncOptions = {}, ) { - return new Promise((resolve, reject) => { - const env: NodeJS.ProcessEnv = { - ...process.env, - FORCE_COLOR: 'true', - ...(options.env ?? {}), - }; + const env: NodeJS.ProcessEnv = { + ...process.env, + FORCE_COLOR: 'true', + ...(options.env ?? {}), + }; - const child = spawn(name, args, { - stdio: 'inherit', - shell: true, - ...options, - env, - }); + const child = spawn(name, args, { + stdio: 'inherit', + shell: true, + ...options, + env, + }); + await waitForExit(child); +} + +export async function waitForExit( + child: ChildProcess & { exitCode?: number }, +): Promise { + if (typeof child.exitCode === 'number') { + if (child.exitCode) { + throw new ExitCodeError(child.exitCode, name); + } + return; + } + + await new Promise((resolve, reject) => { child.once('error', error => reject(error)); child.once('exit', code => { if (code) {