UNPKG

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