UNPKG

6.36 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.BuildAction = void 0;
13const chalk = require("chalk");
14const path_1 = require("path");
15const assets_manager_1 = require("../lib/compiler/assets-manager");
16const compiler_1 = require("../lib/compiler/compiler");
17const get_value_or_default_1 = require("../lib/compiler/helpers/get-value-or-default");
18const tsconfig_provider_1 = require("../lib/compiler/helpers/tsconfig-provider");
19const plugins_loader_1 = require("../lib/compiler/plugins-loader");
20const typescript_loader_1 = require("../lib/compiler/typescript-loader");
21const watch_compiler_1 = require("../lib/compiler/watch-compiler");
22const webpack_compiler_1 = require("../lib/compiler/webpack-compiler");
23const workspace_utils_1 = require("../lib/compiler/workspace-utils");
24const configuration_1 = require("../lib/configuration");
25const defaults_1 = require("../lib/configuration/defaults");
26const readers_1 = require("../lib/readers");
27const ui_1 = require("../lib/ui");
28const abstract_action_1 = require("./abstract.action");
29class BuildAction extends abstract_action_1.AbstractAction {
30 constructor() {
31 super(...arguments);
32 this.pluginsLoader = new plugins_loader_1.PluginsLoader();
33 this.tsLoader = new typescript_loader_1.TypeScriptBinaryLoader();
34 this.tsConfigProvider = new tsconfig_provider_1.TsConfigProvider(this.tsLoader);
35 this.compiler = new compiler_1.Compiler(this.pluginsLoader, this.tsConfigProvider, this.tsLoader);
36 this.webpackCompiler = new webpack_compiler_1.WebpackCompiler(this.pluginsLoader);
37 this.watchCompiler = new watch_compiler_1.WatchCompiler(this.pluginsLoader, this.tsConfigProvider, this.tsLoader);
38 this.fileSystemReader = new readers_1.FileSystemReader(process.cwd());
39 this.loader = new configuration_1.NestConfigurationLoader(this.fileSystemReader);
40 this.assetsManager = new assets_manager_1.AssetsManager();
41 this.workspaceUtils = new workspace_utils_1.WorkspaceUtils();
42 }
43 handle(inputs, options) {
44 return __awaiter(this, void 0, void 0, function* () {
45 try {
46 const watchModeOption = options.find((option) => option.name === 'watch');
47 const watchMode = !!(watchModeOption && watchModeOption.value);
48 const watchAssetsModeOption = options.find((option) => option.name === 'watchAssets');
49 const watchAssetsMode = !!(watchAssetsModeOption && watchAssetsModeOption.value);
50 yield this.runBuild(inputs, options, watchMode, watchAssetsMode);
51 }
52 catch (err) {
53 if (err instanceof Error) {
54 console.log(`\n${ui_1.ERROR_PREFIX} ${err.message}\n`);
55 }
56 else {
57 console.error(`\n${chalk.red(err)}\n`);
58 }
59 process.exit(1);
60 }
61 });
62 }
63 runBuild(inputs, options, watchMode, watchAssetsMode, isDebugEnabled = false, onSuccess) {
64 return __awaiter(this, void 0, void 0, function* () {
65 const configFileName = options.find((option) => option.name === 'config')
66 .value;
67 const configuration = yield this.loader.load(configFileName);
68 const appName = inputs.find((input) => input.name === 'app')
69 .value;
70 const pathToTsconfig = (0, get_value_or_default_1.getValueOrDefault)(configuration, 'compilerOptions.tsConfigPath', appName, 'path', options);
71 const { options: tsOptions } = this.tsConfigProvider.getByConfigFilename(pathToTsconfig);
72 const outDir = tsOptions.outDir || defaults_1.defaultOutDir;
73 const isWebpackEnabled = (0, get_value_or_default_1.getValueOrDefault)(configuration, 'compilerOptions.webpack', appName, 'webpack', options);
74 yield this.workspaceUtils.deleteOutDirIfEnabled(configuration, appName, outDir);
75 this.assetsManager.copyAssets(configuration, appName, outDir, watchAssetsMode);
76 if (isWebpackEnabled) {
77 const webpackPath = (0, get_value_or_default_1.getValueOrDefault)(configuration, 'compilerOptions.webpackConfigPath', appName, 'webpackPath', options);
78 const webpackConfigFactoryOrConfig = this.getWebpackConfigFactoryByPath(webpackPath, configuration.compilerOptions.webpackConfigPath);
79 return this.webpackCompiler.run(configuration, webpackConfigFactoryOrConfig, pathToTsconfig, appName, isDebugEnabled, watchMode, this.assetsManager, onSuccess);
80 }
81 if (watchMode) {
82 const tsCompilerOptions = {};
83 const isPreserveWatchOutputEnabled = options.find((option) => option.name === 'preserveWatchOutput' && option.value === true);
84 if (isPreserveWatchOutputEnabled) {
85 tsCompilerOptions.preserveWatchOutput = true;
86 }
87 this.watchCompiler.run(configuration, pathToTsconfig, appName, tsCompilerOptions, onSuccess);
88 }
89 else {
90 this.compiler.run(configuration, pathToTsconfig, appName, onSuccess);
91 this.assetsManager.closeWatchers();
92 }
93 });
94 }
95 getWebpackConfigFactoryByPath(webpackPath, defaultPath) {
96 const pathToWebpackFile = (0, path_1.join)(process.cwd(), webpackPath);
97 try {
98 return require(pathToWebpackFile);
99 }
100 catch (err) {
101 if (webpackPath !== defaultPath) {
102 throw err;
103 }
104 return ({}) => ({});
105 }
106 }
107}
108exports.BuildAction = BuildAction;