-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
run-executor.ts
116 lines (108 loc) · 3.1 KB
/
run-executor.ts
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
import { appendFileSync, openSync, writeFileSync } from 'fs';
import { run } from '../src/command-line/run';
import { addCommandPrefixIfNeeded } from '../src/utils/add-command-prefix';
if (process.env.NX_TERMINAL_OUTPUT_PATH) {
setUpOutputWatching(
process.env.NX_TERMINAL_CAPTURE_STDERR === 'true',
process.env.NX_STREAM_OUTPUT === 'true'
);
}
if (!process.env.NX_WORKSPACE_ROOT) {
console.error('Invalid Nx command invocation');
process.exit(1);
}
let projectName;
requireCli();
function requireCli() {
process.env.NX_CLI_SET = 'true';
try {
const args = JSON.parse(process.argv[2]);
projectName = args.targetDescription.project;
run(
process.cwd(),
process.env.NX_WORKSPACE_ROOT,
args.targetDescription,
args.overrides,
args.isVerbose,
false
)
.then((statusCode) => {
process.exit(statusCode);
})
.catch((e) => {
console.error(`Unexpected error`);
console.error(e);
});
} catch (e) {
console.error(`Could not find 'nx' module in this workspace.`, e);
process.exit(1);
}
}
/**
* We need to collect all stdout and stderr and store it, so the caching mechanism
* could store it.
*
* Writing stdout and stderr into different streams is too risky when using TTY.
*
* So we are simply monkey-patching the Javascript object. In this case the actual output will always be correct.
* And the cached output should be correct unless the CLI bypasses process.stdout or console.log and uses some
* C-binary to write to stdout.
*/
function setUpOutputWatching(captureStderr: boolean, streamOutput: boolean) {
const stdoutWrite = process.stdout._write;
const stderrWrite = process.stderr._write;
// The terminal output file gets out and err
const outputPath = process.env.NX_TERMINAL_OUTPUT_PATH;
const stdoutAndStderrLogFileHandle = openSync(outputPath, 'w');
const onlyStdout = [] as string[];
process.stdout._write = (
chunk: any,
encoding: string,
callback: Function
) => {
onlyStdout.push(chunk);
appendFileSync(stdoutAndStderrLogFileHandle, chunk);
if (streamOutput) {
const updatedChunk = addCommandPrefixIfNeeded(
projectName,
chunk,
encoding
);
stdoutWrite.apply(process.stdout, [
updatedChunk.content,
updatedChunk.encoding,
callback,
]);
} else {
callback();
}
};
process.stderr._write = (
chunk: any,
encoding: string,
callback: Function
) => {
appendFileSync(stdoutAndStderrLogFileHandle, chunk);
if (streamOutput) {
const updatedChunk = addCommandPrefixIfNeeded(
projectName,
chunk,
encoding
);
stderrWrite.apply(process.stderr, [
updatedChunk.content,
updatedChunk.encoding,
callback,
]);
} else {
callback();
}
};
process.on('exit', (code) => {
// when the process exits successfully, and we are not asked to capture stderr
// override the file with only stdout
if (code === 0 && !captureStderr) {
writeFileSync(outputPath, onlyStdout.join(''));
}
});
}