/
run-cli.ts
74 lines (66 loc) · 1.79 KB
/
run-cli.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
import * as path from 'path';
import * as fs from 'fs';
import { findWorkspaceRoot } from './find-workspace-root';
const workspace = findWorkspaceRoot(process.cwd());
if (process.env.NX_TERMINAL_OUTPUT_PATH) {
setUpOutputWatching(process.env.NX_TERMINAL_CAPTURE_STDERR === 'true');
}
requireCli();
function requireCli() {
if (workspace.type === 'nx') {
require(path.join(
workspace.dir,
'node_modules',
'@nrwl',
'tao',
'index.js'
));
} else {
require(path.join(
workspace.dir,
'node_modules',
'@angular',
'cli',
'lib',
'init.js'
));
}
}
/**
* We need to collect all stdout and stderr and store it, so the caching mechanism
* could store it.
*
* Writing stdout and stderr into different stream 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) {
const stdoutWrite = process.stdout._write;
const stderrWrite = process.stderr._write;
let out = [];
process.stdout._write = (
chunk: any,
encoding: string,
callback: Function
) => {
out.push(chunk.toString());
stdoutWrite.apply(process.stdout, [chunk, encoding, callback]);
};
process.stderr._write = (
chunk: any,
encoding: string,
callback: Function
) => {
if (captureStderr) {
out.push(chunk.toString());
}
stderrWrite.apply(process.stderr, [chunk, encoding, callback]);
};
process.on('exit', code => {
if (code === 0) {
fs.writeFileSync(process.env.NX_TERMINAL_OUTPUT_PATH, out.join(''));
}
});
}