1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
12 | exports.StartAction = void 0;
|
13 | const chalk = require("chalk");
|
14 | const child_process_1 = require("child_process");
|
15 | const fs = require("fs");
|
16 | const path_1 = require("path");
|
17 | const killProcess = require("tree-kill");
|
18 | const tree_kill_1 = require("../lib/utils/tree-kill");
|
19 | const get_value_or_default_1 = require("../lib/compiler/helpers/get-value-or-default");
|
20 | const defaults_1 = require("../lib/configuration/defaults");
|
21 | const ui_1 = require("../lib/ui");
|
22 | const build_action_1 = require("./build.action");
|
23 | class StartAction extends build_action_1.BuildAction {
|
24 | handle(inputs, options) {
|
25 | var _a, _b;
|
26 | return __awaiter(this, void 0, void 0, function* () {
|
27 | try {
|
28 | const configFileName = options.find((option) => option.name === 'config')
|
29 | .value;
|
30 | const configuration = yield this.loader.load(configFileName);
|
31 | const appName = inputs.find((input) => input.name === 'app')
|
32 | .value;
|
33 | const pathToTsconfig = (0, get_value_or_default_1.getValueOrDefault)(configuration, 'compilerOptions.tsConfigPath', appName, 'path', options);
|
34 | const binaryToRunOption = options.find((option) => option.name === 'exec');
|
35 | const debugModeOption = options.find((option) => option.name === 'debug');
|
36 | const watchModeOption = options.find((option) => option.name === 'watch');
|
37 | const isWatchEnabled = !!(watchModeOption && watchModeOption.value);
|
38 | const watchAssetsModeOption = options.find((option) => option.name === 'watchAssets');
|
39 | const isWatchAssetsEnabled = !!(watchAssetsModeOption && watchAssetsModeOption.value);
|
40 | const debugFlag = debugModeOption && debugModeOption.value;
|
41 | const binaryToRun = binaryToRunOption && binaryToRunOption.value;
|
42 | const { options: tsOptions } = this.tsConfigProvider.getByConfigFilename(pathToTsconfig);
|
43 | const outDir = tsOptions.outDir || defaults_1.defaultOutDir;
|
44 | const entryFile = ((_a = options.find((option) => option.name === 'entryFile')) === null || _a === void 0 ? void 0 : _a.value) ||
|
45 | (0, get_value_or_default_1.getValueOrDefault)(configuration, 'entryFile', appName, undefined, undefined, defaults_1.defaultConfiguration.entryFile);
|
46 | const sourceRoot = ((_b = options.find((option) => option.name === 'sourceRoot')) === null || _b === void 0 ? void 0 : _b.value) ||
|
47 | (0, get_value_or_default_1.getValueOrDefault)(configuration, 'sourceRoot', appName, undefined, undefined, defaults_1.defaultConfiguration.sourceRoot);
|
48 | const onSuccess = this.createOnSuccessHook(entryFile, sourceRoot, debugFlag, outDir, binaryToRun);
|
49 | yield this.runBuild(inputs, options, isWatchEnabled, isWatchAssetsEnabled, !!debugFlag, onSuccess);
|
50 | }
|
51 | catch (err) {
|
52 | if (err instanceof Error) {
|
53 | console.log(`\n${ui_1.ERROR_PREFIX} ${err.message}\n`);
|
54 | }
|
55 | else {
|
56 | console.error(`\n${chalk.red(err)}\n`);
|
57 | }
|
58 | }
|
59 | });
|
60 | }
|
61 | createOnSuccessHook(entryFile, sourceRoot, debugFlag, outDirName, binaryToRun = 'node') {
|
62 | let childProcessRef;
|
63 | process.on('exit', () => childProcessRef && (0, tree_kill_1.treeKillSync)(childProcessRef.pid));
|
64 | return () => {
|
65 | if (childProcessRef) {
|
66 | childProcessRef.removeAllListeners('exit');
|
67 | childProcessRef.on('exit', () => {
|
68 | childProcessRef = this.spawnChildProcess(entryFile, sourceRoot, debugFlag, outDirName, binaryToRun);
|
69 | childProcessRef.on('exit', () => (childProcessRef = undefined));
|
70 | });
|
71 | childProcessRef.stdin && childProcessRef.stdin.pause();
|
72 | killProcess(childProcessRef.pid);
|
73 | }
|
74 | else {
|
75 | childProcessRef = this.spawnChildProcess(entryFile, sourceRoot, debugFlag, outDirName, binaryToRun);
|
76 | childProcessRef.on('exit', (code) => {
|
77 | process.exitCode = code;
|
78 | childProcessRef = undefined;
|
79 | });
|
80 | }
|
81 | };
|
82 | }
|
83 | spawnChildProcess(entryFile, sourceRoot, debug, outDirName, binaryToRun) {
|
84 | let outputFilePath = (0, path_1.join)(outDirName, sourceRoot, entryFile);
|
85 | if (!fs.existsSync(outputFilePath + '.js')) {
|
86 | outputFilePath = (0, path_1.join)(outDirName, entryFile);
|
87 | }
|
88 | let childProcessArgs = [];
|
89 | const argsStartIndex = process.argv.indexOf('--');
|
90 | if (argsStartIndex >= 0) {
|
91 | childProcessArgs = process.argv.slice(argsStartIndex + 1);
|
92 | }
|
93 | outputFilePath =
|
94 | outputFilePath.indexOf(' ') >= 0 ? `"${outputFilePath}"` : outputFilePath;
|
95 | const processArgs = [outputFilePath, ...childProcessArgs];
|
96 | if (debug) {
|
97 | const inspectFlag = typeof debug === 'string' ? `--inspect=${debug}` : '--inspect';
|
98 | processArgs.unshift(inspectFlag);
|
99 | }
|
100 | if (this.isSourceMapSupportPkgAvailable()) {
|
101 | processArgs.unshift('-r source-map-support/register');
|
102 | }
|
103 | return (0, child_process_1.spawn)(binaryToRun, processArgs, {
|
104 | stdio: 'inherit',
|
105 | shell: true,
|
106 | });
|
107 | }
|
108 | isSourceMapSupportPkgAvailable() {
|
109 | try {
|
110 | require.resolve('source-map-support');
|
111 | return true;
|
112 | }
|
113 | catch (_a) {
|
114 | return false;
|
115 | }
|
116 | }
|
117 | }
|
118 | exports.StartAction = StartAction;
|