forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCmdRunner.ts
More file actions
139 lines (119 loc) · 3.75 KB
/
CmdRunner.ts
File metadata and controls
139 lines (119 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as childProcess from 'child_process';
import * as path from 'path';
import {
IPackageJson,
FileSystem,
Terminal
} from '@rushstack/node-core-library';
import { StandardBuildFolders } from './StandardBuildFolders';
/**
* Options for a CmdTask.
* @beta
*/
export interface IBaseTaskOptions {
/**
* The name of the package to resolve.
*/
packagePath: string;
/**
*
*/
packageJson: IPackageJson;
/**
* The path to the binary to invoke inside the package.
*/
packageBinPath: string;
}
/**
* @beta
*/
export interface IRunCmdOptions {
args: string[];
onData?: (data: Buffer) => void;
onError?: (data: Buffer) => void;
onClose?: (code: number, hasErrors: boolean, resolve: () => void, reject: (error: Error) => void) => void;
}
/**
* This base task provides support for finding and then executing a binary in a node package.
*
* @beta
*/
export class CmdRunner {
private static readonly _nodePath: string = process.execPath;
private _standardBuildFolders: StandardBuildFolders;
private _terminal: Terminal;
private _options: IBaseTaskOptions;
private _errorHasBeenLogged: boolean;
public constructor(
constants: StandardBuildFolders,
terminal: Terminal,
options: IBaseTaskOptions
) {
this._standardBuildFolders = constants;
this._terminal = terminal;
this._options = options;
}
public runCmd(options: IRunCmdOptions): Promise<void> {
const {
args,
onData = this._onData.bind(this),
onError = this._onError.bind(this),
onClose = this._onClose.bind(this)
}: IRunCmdOptions = options;
const packageJson: IPackageJson | undefined = this._options.packageJson;
if (!packageJson) {
return Promise.reject(new Error(`Unable to find the package.json file for ${this._options}.`));
}
// Print the version
this._terminal.writeLine(`${packageJson.name} version: ${packageJson.version}`);
const binaryPath: string = path.resolve(this._options.packagePath, this._options.packageBinPath);
if (!FileSystem.exists(binaryPath)) {
return Promise.reject(new Error(
`The binary is missing. This indicates that ${this._options.packageBinPath} is not ` +
'installed correctly.'
));
}
return new Promise((resolve: () => void, reject: (error: Error) => void) => {
const nodePath: string | undefined = CmdRunner._nodePath;
if (!nodePath) {
reject(new Error('Unable to find node executable'));
return;
}
// Invoke the tool and watch for log messages
const spawnResult: childProcess.ChildProcess = childProcess.spawn(
nodePath,
[binaryPath, ...args],
{
cwd: this._standardBuildFolders.projectFolderPath,
env: process.env,
stdio: 'pipe'
}
);
if (spawnResult.stdout !== null) {
spawnResult.stdout.on('data', onData);
}
if (spawnResult.stderr !== null) {
spawnResult.stderr.on('data', (data: Buffer) => {
this._errorHasBeenLogged = true;
onError(data);
});
}
spawnResult.on('close', (code) => onClose(code, this._errorHasBeenLogged, resolve, reject));
});
}
protected _onData(data: Buffer): void {
this._terminal.writeLine(data.toString().trim());
}
protected _onError(data: Buffer): void {
this._terminal.writeError(data.toString().trim());
}
protected _onClose(code: number, hasErrors: boolean, resolve: () => void, reject: (error: Error) => void): void {
if (code !== 0 || hasErrors) {
reject(new Error(`exited with code ${code}`));
} else {
resolve();
}
}
}