UNPKG

6.31 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 }
60 });
61 }
62 runBuild(inputs, options, watchMode, watchAssetsMode, isDebugEnabled = false, onSuccess) {
63 return __awaiter(this, void 0, void 0, function* () {
64 const configFileName = options.find((option) => option.name === 'config')
65 .value;
66 const configuration = yield this.loader.load(configFileName);
67 const appName = inputs.find((input) => input.name === 'app')
68 .value;
69 const pathToTsconfig = get_value_or_default_1.getValueOrDefault(configuration, 'compilerOptions.tsConfigPath', appName, 'path', options);
70 const { options: tsOptions } = this.tsConfigProvider.getByConfigFilename(pathToTsconfig);
71 const outDir = tsOptions.outDir || defaults_1.defaultOutDir;
72 const isWebpackEnabled = get_value_or_default_1.getValueOrDefault(configuration, 'compilerOptions.webpack', appName, 'webpack', options);
73 yield this.workspaceUtils.deleteOutDirIfEnabled(configuration, appName, outDir);
74 this.assetsManager.copyAssets(configuration, appName, outDir, watchAssetsMode);
75 if (isWebpackEnabled) {
76 const webpackPath = get_value_or_default_1.getValueOrDefault(configuration, 'compilerOptions.webpackConfigPath', appName, 'webpackPath', options);
77 const webpackConfigFactoryOrConfig = this.getWebpackConfigFactoryByPath(webpackPath, configuration.compilerOptions.webpackConfigPath);
78 return this.webpackCompiler.run(configuration, webpackConfigFactoryOrConfig, pathToTsconfig, appName, isDebugEnabled, watchMode, this.assetsManager, onSuccess);
79 }
80 if (watchMode) {
81 const tsCompilerOptions = {};
82 const isPreserveWatchOutputEnabled = options.find((option) => option.name === 'preserveWatchOutput' && option.value === true);
83 if (isPreserveWatchOutputEnabled) {
84 tsCompilerOptions.preserveWatchOutput = true;
85 }
86 this.watchCompiler.run(configuration, pathToTsconfig, appName, tsCompilerOptions, onSuccess);
87 }
88 else {
89 this.compiler.run(configuration, pathToTsconfig, appName, onSuccess);
90 this.assetsManager.closeWatchers();
91 }
92 });
93 }
94 getWebpackConfigFactoryByPath(webpackPath, defaultPath) {
95 const pathToWebpackFile = path_1.join(process.cwd(), webpackPath);
96 try {
97 return require(pathToWebpackFile);
98 }
99 catch (err) {
100 if (webpackPath !== defaultPath) {
101 throw err;
102 }
103 return ({}) => ({});
104 }
105 }
106}
107exports.BuildAction = BuildAction;