Skip to content

Commit

Permalink
fix(core): combine serial and parallel execution + forward sigint to …
Browse files Browse the repository at this point in the history
…child process (#13885)

Co-authored-by: AgentEnder <craigorycoppola@gmail.com>
  • Loading branch information
caioaao and AgentEnder committed Mar 17, 2023
1 parent 616f0f0 commit fd11334
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
4 changes: 2 additions & 2 deletions e2e/nx-misc/src/extras.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
}
});
Expand Down
30 changes: 17 additions & 13 deletions packages/nx/src/executors/run-commands/run-commands.impl.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down

1 comment on commit fd11334

@vercel
Copy link

@vercel vercel bot commented on fd11334 Mar 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app
nx.dev

Please sign in to comment.