/
start.action.ts
187 lines (175 loc) · 5.43 KB
/
start.action.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import * as chalk from 'chalk';
import { spawn } from 'child_process';
import * as fs from 'fs';
import { join } from 'path';
import * as killProcess from 'tree-kill';
import { treeKillSync as killProcessSync } from '../lib/utils/tree-kill';
import { Input } from '../commands';
import { getValueOrDefault } from '../lib/compiler/helpers/get-value-or-default';
import {
defaultConfiguration,
defaultOutDir,
} from '../lib/configuration/defaults';
import { ERROR_PREFIX } from '../lib/ui';
import { BuildAction } from './build.action';
export class StartAction extends BuildAction {
public async handle(inputs: Input[], options: Input[]) {
try {
const configFileName = options.find((option) => option.name === 'config')!
.value as string;
const configuration = await this.loader.load(configFileName);
const appName = inputs.find((input) => input.name === 'app')!
.value as string;
const pathToTsconfig = getValueOrDefault<string>(
configuration,
'compilerOptions.tsConfigPath',
appName,
'path',
options,
);
const binaryToRunOption = options.find(
(option) => option.name === 'exec',
);
const debugModeOption = options.find((option) => option.name === 'debug');
const watchModeOption = options.find((option) => option.name === 'watch');
const isWatchEnabled = !!(watchModeOption && watchModeOption.value);
const watchAssetsModeOption = options.find(
(option) => option.name === 'watchAssets',
);
const isWatchAssetsEnabled = !!(
watchAssetsModeOption && watchAssetsModeOption.value
);
const debugFlag = debugModeOption && debugModeOption.value;
const binaryToRun =
binaryToRunOption && (binaryToRunOption.value as string | undefined);
const { options: tsOptions } =
this.tsConfigProvider.getByConfigFilename(pathToTsconfig);
const outDir = tsOptions.outDir || defaultOutDir;
const entryFile =
(options.find((option) => option.name === 'entryFile')
?.value as string) ||
getValueOrDefault(
configuration,
'entryFile',
appName,
undefined,
undefined,
defaultConfiguration.entryFile,
);
const sourceRoot =
(options.find((option) => option.name === 'sourceRoot')
?.value as string) ||
getValueOrDefault(
configuration,
'sourceRoot',
appName,
undefined,
undefined,
defaultConfiguration.sourceRoot,
);
const onSuccess = this.createOnSuccessHook(
entryFile,
sourceRoot,
debugFlag,
outDir,
binaryToRun,
);
await this.runBuild(
inputs,
options,
isWatchEnabled,
isWatchAssetsEnabled,
!!debugFlag,
onSuccess,
);
} catch (err) {
if (err instanceof Error) {
console.log(`\n${ERROR_PREFIX} ${err.message}\n`);
} else {
console.error(`\n${chalk.red(err)}\n`);
}
}
}
public createOnSuccessHook(
entryFile: string,
sourceRoot: string,
debugFlag: boolean | string | undefined,
outDirName: string,
binaryToRun = 'node',
) {
let childProcessRef: any;
process.on(
'exit',
() => childProcessRef && killProcessSync(childProcessRef.pid),
);
return () => {
if (childProcessRef) {
childProcessRef.removeAllListeners('exit');
childProcessRef.on('exit', () => {
childProcessRef = this.spawnChildProcess(
entryFile,
sourceRoot,
debugFlag,
outDirName,
binaryToRun,
);
childProcessRef.on('exit', () => (childProcessRef = undefined));
});
childProcessRef.stdin && childProcessRef.stdin.pause();
killProcess(childProcessRef.pid);
} else {
childProcessRef = this.spawnChildProcess(
entryFile,
sourceRoot,
debugFlag,
outDirName,
binaryToRun,
);
childProcessRef.on('exit', (code: number) => {
process.exitCode = code;
childProcessRef = undefined;
});
}
};
}
private spawnChildProcess(
entryFile: string,
sourceRoot: string,
debug: boolean | string | undefined,
outDirName: string,
binaryToRun: string,
) {
let outputFilePath = join(outDirName, sourceRoot, entryFile);
if (!fs.existsSync(outputFilePath + '.js')) {
outputFilePath = join(outDirName, entryFile);
}
let childProcessArgs: string[] = [];
const argsStartIndex = process.argv.indexOf('--');
if (argsStartIndex >= 0) {
childProcessArgs = process.argv.slice(argsStartIndex + 1);
}
outputFilePath =
outputFilePath.indexOf(' ') >= 0 ? `"${outputFilePath}"` : outputFilePath;
const processArgs = [outputFilePath, ...childProcessArgs];
if (debug) {
const inspectFlag =
typeof debug === 'string' ? `--inspect=${debug}` : '--inspect';
processArgs.unshift(inspectFlag);
}
if (this.isSourceMapSupportPkgAvailable()) {
processArgs.unshift('-r source-map-support/register');
}
return spawn(binaryToRun, processArgs, {
stdio: 'inherit',
shell: true,
});
}
private isSourceMapSupportPkgAvailable() {
try {
require.resolve('source-map-support');
return true;
} catch {
return false;
}
}
}