UNPKG

5.58 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 get_value_or_default_1 = require("../lib/compiler/helpers/get-value-or-default");
19const defaults_1 = require("../lib/configuration/defaults");
20const ui_1 = require("../lib/ui");
21const build_action_1 = require("./build.action");
22class StartAction extends build_action_1.BuildAction {
23 handle(inputs, options) {
24 return __awaiter(this, void 0, void 0, function* () {
25 try {
26 const configFileName = options.find((option) => option.name === 'config')
27 .value;
28 const configuration = yield this.loader.load(configFileName);
29 const appName = inputs.find((input) => input.name === 'app')
30 .value;
31 const pathToTsconfig = get_value_or_default_1.getValueOrDefault(configuration, 'compilerOptions.tsConfigPath', appName, 'path', options);
32 const binaryToRunOption = options.find((option) => option.name === 'exec');
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 = binaryToRunOption && binaryToRunOption.value;
40 const { options: tsOptions } = this.tsConfigProvider.getByConfigFilename(pathToTsconfig);
41 const outDir = tsOptions.outDir || defaults_1.defaultOutDir;
42 const onSuccess = this.createOnSuccessHook(configuration, appName, debugFlag, outDir, binaryToRun);
43 yield this.runBuild(inputs, options, isWatchEnabled, isWatchAssetsEnabled, !!debugFlag, onSuccess);
44 }
45 catch (err) {
46 if (err instanceof Error) {
47 console.log(`\n${ui_1.ERROR_PREFIX} ${err.message}\n`);
48 }
49 else {
50 console.error(`\n${chalk.red(err)}\n`);
51 }
52 }
53 });
54 }
55 createOnSuccessHook(configuration, appName, debugFlag, outDirName, binaryToRun = 'node') {
56 const sourceRoot = get_value_or_default_1.getValueOrDefault(configuration, 'sourceRoot', appName);
57 const entryFile = get_value_or_default_1.getValueOrDefault(configuration, 'entryFile', appName);
58 let childProcessRef;
59 process.on('exit', () => childProcessRef && killProcess(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 = path_1.join(outDirName, sourceRoot, entryFile);
81 if (!fs.existsSync(outputFilePath + '.js')) {
82 outputFilePath = 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 return child_process_1.spawn(binaryToRun, processArgs, {
97 stdio: 'inherit',
98 shell: true,
99 });
100 }
101}
102exports.StartAction = StartAction;