diff --git a/e2e/nx-misc/src/extras.test.ts b/e2e/nx-misc/src/extras.test.ts index 92810f53a785a..654e93d6cb5a0 100644 --- a/e2e/nx-misc/src/extras.test.ts +++ b/e2e/nx-misc/src/extras.test.ts @@ -200,8 +200,8 @@ describe('Extra Nx Misc Tests', () => { runCLI(`run ${mylib}:error`); fail('Should error if process errors'); } catch (e) { - expect(e.stdout.toString()).toContain( - 'Something went wrong in run-commands - Command failed: exit 1' + expect(e.stderr.toString()).toContain( + 'command "exit 1" exited with non-zero status code' ); } }); diff --git a/packages/nx/src/executors/run-commands/run-commands.impl.ts b/packages/nx/src/executors/run-commands/run-commands.impl.ts index aed60308944be..d0bfdcfdbf2d2 100644 --- a/packages/nx/src/executors/run-commands/run-commands.impl.ts +++ b/packages/nx/src/executors/run-commands/run-commands.impl.ts @@ -1,4 +1,4 @@ -import { exec, execSync } from 'child_process'; +import { exec } from 'child_process'; import * as path from 'path'; import * as yargsParser from 'yargs-parser'; import { env as appendLocalEnv } from 'npm-run-path'; @@ -176,12 +176,20 @@ async function runSerially( context: ExecutorContext ) { for (const c of options.commands) { - createSyncProcess( - c.command, + const success = await createProcess( + c, + undefined, options.color, calculateCwd(options.cwd, context) ); + if (!success) { + process.stderr.write( + `Warning: run-commands command "${c.command}" exited with non-zero status code` + ); + return false; + } } + return true; } @@ -205,9 +213,14 @@ function createProcess( /** * Ensure the child process is killed when the parent exits */ - const processExitListener = () => childProcess.kill(); + const processExitListener = (signal?: number | NodeJS.Signals) => () => + childProcess.kill(signal); + process.on('exit', processExitListener); process.on('SIGTERM', processExitListener); + process.on('SIGINT', processExitListener); + process.on('SIGQUIT', processExitListener); + childProcess.stdout.on('data', (data) => { process.stdout.write(addColorAndPrefix(data, commandConfig)); if (readyWhen && data.toString().indexOf(readyWhen) > -1) { @@ -253,15 +266,6 @@ function addColorAndPrefix( return out; } -function createSyncProcess(command: string, color: boolean, cwd: string) { - execSync(command, { - env: processEnv(color), - stdio: ['inherit', 'inherit', 'inherit'], - maxBuffer: LARGE_BUFFER, - cwd, - }); -} - function calculateCwd( cwd: string | undefined, context: ExecutorContext