Skip to content

Commit 2968525

Browse files
committed
RushXCommandLine now uses executeLifecycleCommand() instead of executeLifecycleCommandAsync().
NOTE: We will come back in a later PR and straighten out the API for launching processes
1 parent c9121b7 commit 2968525

File tree

3 files changed

+30
-28
lines changed

3 files changed

+30
-28
lines changed

apps/rush-lib/src/cli/RushXCommandLine.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4-
import * as child_process from 'child_process';
54
import * as colors from 'colors';
65
import * as os from 'os';
76
import * as path from 'path';
@@ -71,20 +70,19 @@ export class RushXCommandLine {
7170

7271
const packageFolder: string = path.dirname(packageJsonFilePath);
7372

74-
const result: child_process.ChildProcess = Utilities.executeLifecycleCommandAsync(
75-
scriptBody,
76-
packageFolder,
77-
packageFolder
73+
const exitCode: number = Utilities.executeLifecycleCommand(scriptBody, {
74+
workingDirectory: packageFolder,
75+
initCwd: packageFolder,
76+
handleOutput: false
77+
}
7878
);
7979

80-
result.on('close', (code) => {
81-
if (code) {
82-
console.log(colors.red(`The script failed with exit code ${code}`));
83-
}
80+
if (exitCode > 0) {
81+
console.log(colors.red(`The script failed with exit code ${exitCode}`));
82+
}
83+
84+
process.exitCode = exitCode;
8485

85-
// Pass along the exit code of the child process
86-
process.exitCode = code || 0;
87-
});
8886
} catch (error) {
8987
console.log(colors.red('Error: ' + error.message));
9088
}

apps/rush-lib/src/logic/EventHooksManager.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ export class EventHooksManager {
2828
console.log(os.EOL + colors.green(`Executing event hooks for ${Event[event]}`));
2929
scripts.forEach((script) => {
3030
try {
31-
Utilities.executeLifecycleCommand(script,
32-
process.cwd(),
33-
this._commonTempFolder,
34-
true
31+
Utilities.executeLifecycleCommand(script, {
32+
workingDirectory: process.cwd(),
33+
initCwd: this._commonTempFolder,
34+
handleOutput: true
35+
}
3536
);
3637
} catch (error) {
3738
console.error(`${os.EOL} Event hook "${script}" failed. Run "rush" with --debug` +

apps/rush-lib/src/utilities/Utilities.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -303,15 +303,16 @@ export class Utilities {
303303
* @param workingDirectory - working directory for running this command
304304
* @param initCwd = the folder containing a local .npmrc, which will be used
305305
* for the INIT_CWD environment variable
306-
* @param captureOutput - if true, map stdio to 'pipe' instead of the parent process's streams
307-
* @beta
306+
* @param handleOutput - if true, hide the process's output, but if there is a nonzero exit code
307+
* then show the stderr
308308
*/
309309
public static executeLifecycleCommand(
310-
command: string,
311-
workingDirectory: string,
312-
initCwd: string,
313-
captureOutput: boolean = false
314-
): child_process.SpawnSyncReturns<Buffer> {
310+
command: string, options: {
311+
workingDirectory: string,
312+
initCwd: string,
313+
handleOutput: boolean
314+
}
315+
): number {
315316
let shellCommand: string = process.env.comspec || 'cmd';
316317
let commandFlags: string = '/d /s /c';
317318
let useShell: boolean = true;
@@ -321,20 +322,22 @@ export class Utilities {
321322
useShell = false;
322323
}
323324

324-
const environment: IEnvironment = Utilities._createEnvironmentForRushCommand(initCwd);
325+
const environment: IEnvironment = Utilities._createEnvironmentForRushCommand(options.initCwd);
325326

326327
const result: child_process.SpawnSyncReturns<Buffer> = child_process.spawnSync(
327328
shellCommand,
328329
[commandFlags, command],
329330
{
330-
cwd: workingDirectory,
331+
cwd: options.workingDirectory,
331332
shell: useShell,
332333
env: environment,
333-
stdio: captureOutput ? ['pipe', 'pipe', 'pipe'] : [0, 1, 2]
334+
stdio: options.handleOutput ? ['pipe', 'pipe', 'pipe'] : [0, 1, 2]
334335
});
335336

336-
Utilities._processResult(result);
337-
return result;
337+
if (options.handleOutput) {
338+
Utilities._processResult(result);
339+
}
340+
return result.status;
338341
}
339342

340343
/**

0 commit comments

Comments
 (0)