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 | return __awaiter(this, void 0, void 0, function* () {
|
26 | try {
|
27 | const configFileName = options.find((option) => option.name === 'config')
|
28 | .value;
|
29 | const configuration = yield this.loader.load(configFileName);
|
30 | const appName = inputs.find((input) => input.name === 'app')
|
31 | .value;
|
32 | const pathToTsconfig = (0, get_value_or_default_1.getValueOrDefault)(configuration, 'compilerOptions.tsConfigPath', appName, 'path', options);
|
33 | const debugModeOption = options.find((option) => option.name === 'debug');
|
34 | const watchModeOption = options.find((option) => option.name === 'watch');
|
35 | const isWatchEnabled = !!(watchModeOption && watchModeOption.value);
|
36 | const watchAssetsModeOption = options.find((option) => option.name === 'watchAssets');
|
37 | const isWatchAssetsEnabled = !!(watchAssetsModeOption && watchAssetsModeOption.value);
|
38 | const debugFlag = debugModeOption && debugModeOption.value;
|
39 | const binaryToRun = (0, get_value_or_default_1.getValueOrDefault)(configuration, 'exec', appName, 'exec', options, defaults_1.defaultConfiguration.exec);
|
40 | const { options: tsOptions } = this.tsConfigProvider.getByConfigFilename(pathToTsconfig);
|
41 | const outDir = tsOptions.outDir || defaults_1.defaultOutDir;
|
42 | const entryFile = (0, get_value_or_default_1.getValueOrDefault)(configuration, 'entryFile', appName, 'entryFile', options, defaults_1.defaultConfiguration.entryFile);
|
43 | const sourceRoot = (0, get_value_or_default_1.getValueOrDefault)(configuration, 'sourceRoot', appName, 'sourceRoot', options, defaults_1.defaultConfiguration.sourceRoot);
|
44 | const onSuccess = this.createOnSuccessHook(entryFile, sourceRoot, debugFlag, outDir, binaryToRun);
|
45 | yield this.runBuild(inputs, options, isWatchEnabled, isWatchAssetsEnabled, !!debugFlag, onSuccess);
|
46 | }
|
47 | catch (err) {
|
48 | if (err instanceof Error) {
|
49 | console.log(`\n${ui_1.ERROR_PREFIX} ${err.message}\n`);
|
50 | }
|
51 | else {
|
52 | console.error(`\n${chalk.red(err)}\n`);
|
53 | }
|
54 | }
|
55 | });
|
56 | }
|
57 | createOnSuccessHook(entryFile, sourceRoot, debugFlag, outDirName, binaryToRun) {
|
58 | let childProcessRef;
|
59 | process.on('exit', () => childProcessRef && (0, tree_kill_1.treeKillSync)(childProcessRef.pid));
|
60 | return () => {
|
61 | if (childProcessRef) {
|
62 | childProcessRef.removeAllListeners('exit');
|
63 | childProcessRef.on('exit', () => {
|
64 | childProcessRef = this.spawnChildProcess(entryFile, sourceRoot, debugFlag, outDirName, binaryToRun);
|
65 | childProcessRef.on('exit', () => (childProcessRef = undefined));
|
66 | });
|
67 | childProcessRef.stdin && childProcessRef.stdin.pause();
|
68 | killProcess(childProcessRef.pid);
|
69 | }
|
70 | else {
|
71 | childProcessRef = this.spawnChildProcess(entryFile, sourceRoot, debugFlag, outDirName, binaryToRun);
|
72 | childProcessRef.on('exit', (code) => {
|
73 | process.exitCode = code;
|
74 | childProcessRef = undefined;
|
75 | });
|
76 | }
|
77 | };
|
78 | }
|
79 | spawnChildProcess(entryFile, sourceRoot, debug, outDirName, binaryToRun) {
|
80 | let outputFilePath = (0, path_1.join)(outDirName, sourceRoot, entryFile);
|
81 | if (!fs.existsSync(outputFilePath + '.js')) {
|
82 | outputFilePath = (0, path_1.join)(outDirName, entryFile);
|
83 | }
|
84 | let childProcessArgs = [];
|
85 | const argsStartIndex = process.argv.indexOf('--');
|
86 | if (argsStartIndex >= 0) {
|
87 | childProcessArgs = process.argv.slice(argsStartIndex + 1);
|
88 | }
|
89 | outputFilePath =
|
90 | outputFilePath.indexOf(' ') >= 0 ? `"${outputFilePath}"` : outputFilePath;
|
91 | const processArgs = [outputFilePath, ...childProcessArgs];
|
92 | if (debug) {
|
93 | const inspectFlag = typeof debug === 'string' ? `--inspect=${debug}` : '--inspect';
|
94 | processArgs.unshift(inspectFlag);
|
95 | }
|
96 | if (this.isSourceMapSupportPkgAvailable()) {
|
97 | processArgs.unshift('-r source-map-support/register');
|
98 | }
|
99 | return (0, child_process_1.spawn)(binaryToRun, processArgs, {
|
100 | stdio: 'inherit',
|
101 | shell: true,
|
102 | });
|
103 | }
|
104 | isSourceMapSupportPkgAvailable() {
|
105 | try {
|
106 | require.resolve('source-map-support');
|
107 | return true;
|
108 | }
|
109 | catch (_a) {
|
110 | return false;
|
111 | }
|
112 | }
|
113 | }
|
114 | exports.StartAction = StartAction;
|