UNPKG

5.46 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 });
12const chalk = require("chalk");
13const child_process_1 = require("child_process");
14const fs = require("fs");
15const path_1 = require("path");
16const killProcess = require("tree-kill");
17const get_value_or_default_1 = require("../lib/compiler/helpers/get-value-or-default");
18const defaults_1 = require("../lib/configuration/defaults");
19const ui_1 = require("../lib/ui");
20const build_action_1 = require("./build.action");
21class StartAction extends build_action_1.BuildAction {
22 handle(inputs, options) {
23 return __awaiter(this, void 0, void 0, function* () {
24 try {
25 const configFileName = options.find(option => option.name === 'config')
26 .value;
27 const configuration = yield this.loader.load(configFileName);
28 const appName = inputs.find(input => input.name === 'app')
29 .value;
30 const pathToTsconfig = get_value_or_default_1.getValueOrDefault(configuration, 'compilerOptions.tsConfigPath', appName, 'path', options);
31 const binaryToRunOption = options.find(option => option.name === 'exec');
32 const debugModeOption = options.find(option => option.name === 'debug');
33 const watchModeOption = options.find(option => option.name === 'watch');
34 const isWatchEnabled = !!(watchModeOption && watchModeOption.value);
35 const watchAssetsModeOption = options.find(option => option.name === 'watchAssets');
36 const isWatchAssetsEnabled = !!(watchAssetsModeOption && watchAssetsModeOption.value);
37 const debugFlag = debugModeOption && debugModeOption.value;
38 const binaryToRun = binaryToRunOption && binaryToRunOption.value;
39 const { options: tsOptions } = this.tsConfigProvider.getByConfigFilename(pathToTsconfig);
40 const outDir = tsOptions.outDir || defaults_1.defaultOutDir;
41 const onSuccess = this.createOnSuccessHook(configuration, appName, debugFlag, outDir, binaryToRun);
42 yield this.runBuild(inputs, options, isWatchEnabled, isWatchAssetsEnabled, !!debugFlag, onSuccess);
43 }
44 catch (err) {
45 if (err instanceof Error) {
46 console.log(`\n${ui_1.ERROR_PREFIX} ${err.message}\n`);
47 }
48 else {
49 console.error(`\n${chalk.red(err)}\n`);
50 }
51 }
52 });
53 }
54 createOnSuccessHook(configuration, appName, debugFlag, outDirName, binaryToRun = 'node') {
55 const sourceRoot = get_value_or_default_1.getValueOrDefault(configuration, 'sourceRoot', appName);
56 const entryFile = get_value_or_default_1.getValueOrDefault(configuration, 'entryFile', appName);
57 let childProcessRef;
58 process.on('exit', () => childProcessRef && killProcess(childProcessRef.pid));
59 return () => {
60 if (childProcessRef) {
61 childProcessRef.removeAllListeners('exit');
62 childProcessRef.on('exit', () => {
63 childProcessRef = this.spawnChildProcess(entryFile, sourceRoot, debugFlag, outDirName, binaryToRun);
64 childProcessRef.on('exit', () => (childProcessRef = undefined));
65 });
66 childProcessRef.stdin && childProcessRef.stdin.pause();
67 killProcess(childProcessRef.pid);
68 }
69 else {
70 childProcessRef = this.spawnChildProcess(entryFile, sourceRoot, debugFlag, outDirName, binaryToRun);
71 childProcessRef.on('exit', () => (childProcessRef = undefined));
72 }
73 };
74 }
75 spawnChildProcess(entryFile, sourceRoot, debug, outDirName, binaryToRun) {
76 let outputFilePath = path_1.join(outDirName, sourceRoot, entryFile);
77 if (!fs.existsSync(outputFilePath + '.js')) {
78 outputFilePath = path_1.join(outDirName, entryFile);
79 }
80 let childProcessArgs = [];
81 const argsStartIndex = process.argv.indexOf('--');
82 if (argsStartIndex >= 0) {
83 childProcessArgs = process.argv.slice(argsStartIndex + 1);
84 }
85 outputFilePath =
86 outputFilePath.indexOf(' ') >= 0 ? `"${outputFilePath}"` : outputFilePath;
87 const processArgs = [outputFilePath, ...childProcessArgs];
88 if (debug) {
89 const inspectFlag = typeof debug === 'string' ? `--inspect=${debug}` : '--inspect';
90 processArgs.unshift(inspectFlag);
91 }
92 return child_process_1.spawn(binaryToRun, processArgs, {
93 stdio: 'inherit',
94 shell: true,
95 });
96 }
97}
98exports.StartAction = StartAction;