@depup/nestjs__cli 11.0.16-depup.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (196) hide show
  1. package/LICENSE +24 -0
  2. package/README.md +42 -0
  3. package/actions/abstract.action.d.ts +4 -0
  4. package/actions/abstract.action.js +6 -0
  5. package/actions/add.action.d.ts +12 -0
  6. package/actions/add.action.js +113 -0
  7. package/actions/build.action.d.ts +22 -0
  8. package/actions/build.action.js +145 -0
  9. package/actions/generate.action.d.ts +5 -0
  10. package/actions/generate.action.js +97 -0
  11. package/actions/index.d.ts +7 -0
  12. package/actions/index.js +23 -0
  13. package/actions/info.action.d.ts +35 -0
  14. package/actions/info.action.js +181 -0
  15. package/actions/new.action.d.ts +7 -0
  16. package/actions/new.action.js +186 -0
  17. package/actions/start.action.d.ts +10 -0
  18. package/actions/start.action.js +122 -0
  19. package/bin/nest.d.ts +2 -0
  20. package/bin/nest.js +25 -0
  21. package/changes.json +54 -0
  22. package/commands/abstract.command.d.ts +7 -0
  23. package/commands/abstract.command.js +9 -0
  24. package/commands/add.command.d.ts +5 -0
  25. package/commands/add.command.js +36 -0
  26. package/commands/build.command.d.ts +5 -0
  27. package/commands/build.command.js +74 -0
  28. package/commands/command.input.d.ts +5 -0
  29. package/commands/command.input.js +2 -0
  30. package/commands/command.loader.d.ts +5 -0
  31. package/commands/command.loader.js +31 -0
  32. package/commands/generate.command.d.ts +9 -0
  33. package/commands/generate.command.js +107 -0
  34. package/commands/index.d.ts +2 -0
  35. package/commands/index.js +18 -0
  36. package/commands/info.command.d.ts +5 -0
  37. package/commands/info.command.js +16 -0
  38. package/commands/new.command.d.ts +5 -0
  39. package/commands/new.command.js +61 -0
  40. package/commands/start.command.d.ts +5 -0
  41. package/commands/start.command.js +104 -0
  42. package/lib/compiler/assets-manager.d.ts +15 -0
  43. package/lib/compiler/assets-manager.js +129 -0
  44. package/lib/compiler/base-compiler.d.ts +9 -0
  45. package/lib/compiler/base-compiler.js +26 -0
  46. package/lib/compiler/compiler.d.ts +12 -0
  47. package/lib/compiler/compiler.js +61 -0
  48. package/lib/compiler/defaults/swc-defaults.d.ts +61 -0
  49. package/lib/compiler/defaults/swc-defaults.js +57 -0
  50. package/lib/compiler/defaults/webpack-defaults.d.ts +3 -0
  51. package/lib/compiler/defaults/webpack-defaults.js +104 -0
  52. package/lib/compiler/helpers/append-extension.d.ts +1 -0
  53. package/lib/compiler/helpers/append-extension.js +7 -0
  54. package/lib/compiler/helpers/copy-path-resolve.d.ts +7 -0
  55. package/lib/compiler/helpers/copy-path-resolve.js +25 -0
  56. package/lib/compiler/helpers/delete-out-dir.d.ts +2 -0
  57. package/lib/compiler/helpers/delete-out-dir.js +12 -0
  58. package/lib/compiler/helpers/get-builder.d.ts +21 -0
  59. package/lib/compiler/helpers/get-builder.js +19 -0
  60. package/lib/compiler/helpers/get-tsc-config.path.d.ts +10 -0
  61. package/lib/compiler/helpers/get-tsc-config.path.js +24 -0
  62. package/lib/compiler/helpers/get-value-or-default.d.ts +4 -0
  63. package/lib/compiler/helpers/get-value-or-default.js +66 -0
  64. package/lib/compiler/helpers/get-webpack-config-path.d.ts +10 -0
  65. package/lib/compiler/helpers/get-webpack-config-path.js +23 -0
  66. package/lib/compiler/helpers/manual-restart.d.ts +2 -0
  67. package/lib/compiler/helpers/manual-restart.js +17 -0
  68. package/lib/compiler/helpers/tsconfig-provider.d.ts +11 -0
  69. package/lib/compiler/helpers/tsconfig-provider.js +22 -0
  70. package/lib/compiler/hooks/tsconfig-paths.hook.d.ts +2 -0
  71. package/lib/compiler/hooks/tsconfig-paths.hook.js +76 -0
  72. package/lib/compiler/interfaces/readonly-visitor.interface.d.ts +10 -0
  73. package/lib/compiler/interfaces/readonly-visitor.interface.js +2 -0
  74. package/lib/compiler/plugins/plugin-metadata-generator.d.ts +58 -0
  75. package/lib/compiler/plugins/plugin-metadata-generator.js +84 -0
  76. package/lib/compiler/plugins/plugin-metadata-printer.d.ts +17 -0
  77. package/lib/compiler/plugins/plugin-metadata-printer.js +51 -0
  78. package/lib/compiler/plugins/plugins-loader.d.ts +30 -0
  79. package/lib/compiler/plugins/plugins-loader.js +70 -0
  80. package/lib/compiler/swc/constants.d.ts +9 -0
  81. package/lib/compiler/swc/constants.js +13 -0
  82. package/lib/compiler/swc/forked-type-checker.d.ts +1 -0
  83. package/lib/compiler/swc/forked-type-checker.js +63 -0
  84. package/lib/compiler/swc/swc-compiler.d.ts +25 -0
  85. package/lib/compiler/swc/swc-compiler.js +231 -0
  86. package/lib/compiler/swc/type-checker-host.d.ts +14 -0
  87. package/lib/compiler/swc/type-checker-host.js +90 -0
  88. package/lib/compiler/typescript-loader.d.ts +6 -0
  89. package/lib/compiler/typescript-loader.js +31 -0
  90. package/lib/compiler/watch-compiler.d.ts +22 -0
  91. package/lib/compiler/watch-compiler.js +89 -0
  92. package/lib/compiler/webpack-compiler.d.ts +21 -0
  93. package/lib/compiler/webpack-compiler.js +99 -0
  94. package/lib/configuration/configuration.d.ts +91 -0
  95. package/lib/configuration/configuration.js +2 -0
  96. package/lib/configuration/configuration.loader.d.ts +4 -0
  97. package/lib/configuration/configuration.loader.js +2 -0
  98. package/lib/configuration/defaults.d.ts +6 -0
  99. package/lib/configuration/defaults.js +86 -0
  100. package/lib/configuration/index.d.ts +3 -0
  101. package/lib/configuration/index.js +19 -0
  102. package/lib/configuration/nest-configuration.loader.d.ts +8 -0
  103. package/lib/configuration/nest-configuration.loader.js +61 -0
  104. package/lib/package-managers/abstract.package-manager.d.ts +25 -0
  105. package/lib/package-managers/abstract.package-manager.js +151 -0
  106. package/lib/package-managers/index.d.ts +8 -0
  107. package/lib/package-managers/index.js +24 -0
  108. package/lib/package-managers/npm.package-manager.d.ts +7 -0
  109. package/lib/package-managers/npm.package-manager.js +26 -0
  110. package/lib/package-managers/package-manager-commands.d.ts +9 -0
  111. package/lib/package-managers/package-manager-commands.js +2 -0
  112. package/lib/package-managers/package-manager.d.ts +5 -0
  113. package/lib/package-managers/package-manager.factory.d.ts +6 -0
  114. package/lib/package-managers/package-manager.factory.js +41 -0
  115. package/lib/package-managers/package-manager.js +9 -0
  116. package/lib/package-managers/pnpm.package-manager.d.ts +7 -0
  117. package/lib/package-managers/pnpm.package-manager.js +27 -0
  118. package/lib/package-managers/project.dependency.d.ts +4 -0
  119. package/lib/package-managers/project.dependency.js +2 -0
  120. package/lib/package-managers/yarn.package-manager.d.ts +7 -0
  121. package/lib/package-managers/yarn.package-manager.js +26 -0
  122. package/lib/questions/questions.d.ts +9 -0
  123. package/lib/questions/questions.js +27 -0
  124. package/lib/readers/file-system.reader.d.ts +8 -0
  125. package/lib/readers/file-system.reader.js +46 -0
  126. package/lib/readers/index.d.ts +2 -0
  127. package/lib/readers/index.js +18 -0
  128. package/lib/readers/reader.d.ts +10 -0
  129. package/lib/readers/reader.js +11 -0
  130. package/lib/runners/abstract.runner.d.ts +11 -0
  131. package/lib/runners/abstract.runner.js +47 -0
  132. package/lib/runners/git.runner.d.ts +4 -0
  133. package/lib/runners/git.runner.js +10 -0
  134. package/lib/runners/index.d.ts +3 -0
  135. package/lib/runners/index.js +19 -0
  136. package/lib/runners/npm.runner.d.ts +4 -0
  137. package/lib/runners/npm.runner.js +10 -0
  138. package/lib/runners/pnpm.runner.d.ts +4 -0
  139. package/lib/runners/pnpm.runner.js +10 -0
  140. package/lib/runners/runner.d.ts +6 -0
  141. package/lib/runners/runner.factory.d.ts +8 -0
  142. package/lib/runners/runner.factory.js +26 -0
  143. package/lib/runners/runner.js +10 -0
  144. package/lib/runners/schematic.runner.d.ts +6 -0
  145. package/lib/runners/schematic.runner.js +21 -0
  146. package/lib/runners/yarn.runner.d.ts +4 -0
  147. package/lib/runners/yarn.runner.js +10 -0
  148. package/lib/schematics/abstract.collection.d.ts +12 -0
  149. package/lib/schematics/abstract.collection.js +23 -0
  150. package/lib/schematics/collection.d.ts +3 -0
  151. package/lib/schematics/collection.factory.d.ts +5 -0
  152. package/lib/schematics/collection.factory.js +19 -0
  153. package/lib/schematics/collection.js +7 -0
  154. package/lib/schematics/custom.collection.d.ts +10 -0
  155. package/lib/schematics/custom.collection.js +33 -0
  156. package/lib/schematics/index.d.ts +4 -0
  157. package/lib/schematics/index.js +20 -0
  158. package/lib/schematics/nest.collection.d.ts +15 -0
  159. package/lib/schematics/nest.collection.js +126 -0
  160. package/lib/schematics/schematic.option.d.ts +8 -0
  161. package/lib/schematics/schematic.option.js +44 -0
  162. package/lib/ui/banner.d.ts +1 -0
  163. package/lib/ui/banner.js +12 -0
  164. package/lib/ui/emojis.d.ts +19 -0
  165. package/lib/ui/emojis.js +23 -0
  166. package/lib/ui/errors.d.ts +4 -0
  167. package/lib/ui/errors.js +8 -0
  168. package/lib/ui/index.d.ts +5 -0
  169. package/lib/ui/index.js +21 -0
  170. package/lib/ui/messages.d.ts +24 -0
  171. package/lib/ui/messages.js +32 -0
  172. package/lib/ui/prefixes.d.ts +2 -0
  173. package/lib/ui/prefixes.js +6 -0
  174. package/lib/utils/formatting.d.ts +8 -0
  175. package/lib/utils/formatting.js +18 -0
  176. package/lib/utils/get-default-tsconfig-path.d.ts +1 -0
  177. package/lib/utils/get-default-tsconfig-path.js +12 -0
  178. package/lib/utils/gracefully-exit-on-prompt-error.d.ts +1 -0
  179. package/lib/utils/gracefully-exit-on-prompt-error.js +11 -0
  180. package/lib/utils/is-module-available.d.ts +1 -0
  181. package/lib/utils/is-module-available.js +12 -0
  182. package/lib/utils/load-configuration.d.ts +2 -0
  183. package/lib/utils/load-configuration.js +9 -0
  184. package/lib/utils/local-binaries.d.ts +3 -0
  185. package/lib/utils/local-binaries.js +15 -0
  186. package/lib/utils/os-info.utils.d.ts +1 -0
  187. package/lib/utils/os-info.utils.js +23 -0
  188. package/lib/utils/project-utils.d.ts +11 -0
  189. package/lib/utils/project-utils.js +83 -0
  190. package/lib/utils/remaining-flags.d.ts +2 -0
  191. package/lib/utils/remaining-flags.js +36 -0
  192. package/lib/utils/tree-kill.d.ts +1 -0
  193. package/lib/utils/tree-kill.js +68 -0
  194. package/lib/utils/type-assertions.d.ts +1 -0
  195. package/lib/utils/type-assertions.js +8 -0
  196. package/package.json +161 -0
@@ -0,0 +1,231 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SwcCompiler = void 0;
4
+ const ansis_1 = require("ansis");
5
+ const child_process_1 = require("child_process");
6
+ const chokidar = require("chokidar");
7
+ const fs_1 = require("fs");
8
+ const promises_1 = require("fs/promises");
9
+ const path = require("path");
10
+ const path_1 = require("path");
11
+ const ui_1 = require("../../ui");
12
+ const tree_kill_1 = require("../../utils/tree-kill");
13
+ const base_compiler_1 = require("../base-compiler");
14
+ const swc_defaults_1 = require("../defaults/swc-defaults");
15
+ const get_value_or_default_1 = require("../helpers/get-value-or-default");
16
+ const plugin_metadata_generator_1 = require("../plugins/plugin-metadata-generator");
17
+ const constants_1 = require("./constants");
18
+ const type_checker_host_1 = require("./type-checker-host");
19
+ class SwcCompiler extends base_compiler_1.BaseCompiler {
20
+ constructor(pluginsLoader) {
21
+ super(pluginsLoader);
22
+ this.pluginMetadataGenerator = new plugin_metadata_generator_1.PluginMetadataGenerator();
23
+ this.typeCheckerHost = new type_checker_host_1.TypeCheckerHost();
24
+ }
25
+ async run(configuration, tsConfigPath, appName, extras, onSuccess) {
26
+ const swcOptions = (0, swc_defaults_1.swcDefaultsFactory)(extras.tsOptions, configuration);
27
+ const swcrcFilePath = (0, get_value_or_default_1.getValueOrDefault)(configuration, 'compilerOptions.builder.options.swcrcPath', appName);
28
+ if (extras.watch) {
29
+ if (extras.typeCheck) {
30
+ this.runTypeChecker(configuration, tsConfigPath, appName, extras);
31
+ }
32
+ await this.runSwc(swcOptions, extras, swcrcFilePath);
33
+ if (onSuccess) {
34
+ onSuccess();
35
+ const debounceTime = 150;
36
+ const callback = this.debounce(onSuccess, debounceTime);
37
+ this.watchFilesInOutDir(swcOptions, callback);
38
+ }
39
+ }
40
+ else {
41
+ if (extras.typeCheck) {
42
+ await this.runTypeChecker(configuration, tsConfigPath, appName, extras);
43
+ }
44
+ await this.runSwc(swcOptions, extras, swcrcFilePath);
45
+ if (onSuccess) {
46
+ onSuccess();
47
+ }
48
+ extras.assetsManager?.closeWatchers();
49
+ }
50
+ }
51
+ runTypeChecker(configuration, tsConfigPath, appName, extras) {
52
+ if (extras.watch) {
53
+ const args = [
54
+ tsConfigPath,
55
+ appName ?? 'undefined',
56
+ configuration.sourceRoot ?? 'src',
57
+ JSON.stringify(configuration.compilerOptions.plugins ?? []),
58
+ ];
59
+ const childProcessRef = (0, child_process_1.fork)((0, path_1.join)(__dirname, 'forked-type-checker.js'), args, {
60
+ cwd: process.cwd(),
61
+ });
62
+ process.on('exit', () => childProcessRef && (0, tree_kill_1.treeKillSync)(childProcessRef.pid));
63
+ }
64
+ else {
65
+ const { readonlyVisitors } = this.loadPlugins(configuration, tsConfigPath, appName);
66
+ const outputDir = this.getPathToSource(configuration, tsConfigPath, appName);
67
+ let fulfilled = false;
68
+ return new Promise((resolve, reject) => {
69
+ try {
70
+ this.typeCheckerHost.run(tsConfigPath, {
71
+ watch: extras.watch,
72
+ onTypeCheck: (program) => {
73
+ if (!fulfilled) {
74
+ fulfilled = true;
75
+ resolve();
76
+ }
77
+ if (readonlyVisitors.length > 0) {
78
+ process.nextTick(() => console.log(constants_1.FOUND_NO_ISSUES_GENERATING_METADATA));
79
+ this.pluginMetadataGenerator.generate({
80
+ outputDir,
81
+ visitors: readonlyVisitors,
82
+ tsProgramRef: program,
83
+ });
84
+ }
85
+ else {
86
+ process.nextTick(() => console.log(constants_1.FOUND_NO_ISSUES_METADATA_GENERATION_SKIPPED));
87
+ }
88
+ },
89
+ });
90
+ }
91
+ catch (err) {
92
+ if (!fulfilled) {
93
+ fulfilled = true;
94
+ reject(err);
95
+ }
96
+ }
97
+ });
98
+ }
99
+ }
100
+ async runSwc(options, extras, swcrcFilePath) {
101
+ process.nextTick(() => console.log(constants_1.SWC_LOG_PREFIX, (0, ansis_1.cyan)('Running...')));
102
+ const swcCli = this.loadSwcCliBinary();
103
+ const swcRcFile = await this.getSwcRcFileContentIfExists(swcrcFilePath);
104
+ const swcOptions = this.deepMerge(options.swcOptions, swcRcFile);
105
+ if (swcOptions?.jsc?.baseUrl && !(0, path_1.isAbsolute)(swcOptions?.jsc?.baseUrl)) {
106
+ // jsc.baseUrl should be resolved by the caller, if it's passed as an object.
107
+ // https://github.com/swc-project/swc/pull/7827
108
+ const rootDir = process.cwd();
109
+ swcOptions.jsc.baseUrl = path.join(rootDir, swcOptions.jsc.baseUrl);
110
+ }
111
+ const swcCliOpts = {
112
+ ...options,
113
+ swcOptions,
114
+ cliOptions: {
115
+ ...options.cliOptions,
116
+ watch: extras.watch,
117
+ },
118
+ };
119
+ if (extras.watch) {
120
+ // This is required since SWC no longer supports auto-compiling of newly added files in watch mode.
121
+ // We need to watch the source directory and trigger SWC compilation manually.
122
+ await this.watchFilesInSrcDir(options, async (file) => {
123
+ // Transpile newly added file
124
+ await swcCli.default({
125
+ ...swcCliOpts,
126
+ cliOptions: {
127
+ ...swcCliOpts.cliOptions,
128
+ filenames: [file],
129
+ },
130
+ });
131
+ });
132
+ }
133
+ await swcCli.default(swcCliOpts);
134
+ }
135
+ loadSwcCliBinary() {
136
+ try {
137
+ return require('@swc/cli/lib/swc/dir');
138
+ }
139
+ catch (err) {
140
+ console.error(ui_1.ERROR_PREFIX +
141
+ ' Failed to load "@swc/cli" and/or "@swc/core" required packages. Please, make sure to install them as development dependencies.');
142
+ process.exit(1);
143
+ }
144
+ }
145
+ getSwcRcFileContentIfExists(swcrcFilePath) {
146
+ try {
147
+ return JSON.parse((0, fs_1.readFileSync)((0, path_1.join)(process.cwd(), swcrcFilePath ?? '.swcrc'), 'utf8'));
148
+ }
149
+ catch (err) {
150
+ if (swcrcFilePath !== undefined) {
151
+ console.error(ui_1.ERROR_PREFIX +
152
+ ` Failed to load "${swcrcFilePath}". Please, check if the file exists and is valid JSON.`);
153
+ process.exit(1);
154
+ }
155
+ return {};
156
+ }
157
+ }
158
+ deepMerge(target, source) {
159
+ if (typeof target !== 'object' ||
160
+ target === null ||
161
+ typeof source !== 'object' ||
162
+ source === null) {
163
+ return source;
164
+ }
165
+ if (Array.isArray(target) && Array.isArray(source)) {
166
+ return source.reduce((acc, value, index) => {
167
+ acc[index] = this.deepMerge(target[index], value);
168
+ return acc;
169
+ }, target);
170
+ }
171
+ const merged = { ...target };
172
+ for (const key in source) {
173
+ if (source.hasOwnProperty(key)) {
174
+ if (key in target) {
175
+ merged[key] = this.deepMerge(target[key], source[key]);
176
+ }
177
+ else {
178
+ merged[key] = source[key];
179
+ }
180
+ }
181
+ }
182
+ return merged;
183
+ }
184
+ debounce(callback, wait) {
185
+ let timeout;
186
+ return () => {
187
+ clearTimeout(timeout);
188
+ timeout = setTimeout(callback, wait);
189
+ };
190
+ }
191
+ async watchFilesInSrcDir(options, onFileAdded) {
192
+ const srcDir = options.cliOptions?.filenames?.[0];
193
+ const isDirectory = await (0, promises_1.stat)(srcDir)
194
+ .then((stats) => stats.isDirectory())
195
+ .catch(() => false);
196
+ if (!srcDir || !isDirectory) {
197
+ // Skip watching if source directory is not a default "src" folder
198
+ // or any other specified directory
199
+ return;
200
+ }
201
+ const extensions = options.cliOptions?.extensions ?? ['ts'];
202
+ const watcher = chokidar.watch(srcDir, {
203
+ ignored: (file, stats) => (stats?.isFile() &&
204
+ extensions.includes(path.extname(file).slice(1))),
205
+ ignoreInitial: true,
206
+ awaitWriteFinish: {
207
+ stabilityThreshold: 50,
208
+ pollInterval: 10,
209
+ },
210
+ });
211
+ watcher.on('add', async (file) => onFileAdded(file));
212
+ }
213
+ watchFilesInOutDir(options, onChange) {
214
+ const dir = (0, path_1.isAbsolute)(options.cliOptions.outDir)
215
+ ? options.cliOptions.outDir
216
+ : (0, path_1.join)(process.cwd(), options.cliOptions.outDir);
217
+ const watcher = chokidar.watch(dir, {
218
+ ignored: (file, stats) => (stats?.isFile() &&
219
+ !(file.endsWith('.js') || file.endsWith('.mjs'))),
220
+ ignoreInitial: true,
221
+ awaitWriteFinish: {
222
+ stabilityThreshold: 50,
223
+ pollInterval: 10,
224
+ },
225
+ });
226
+ for (const type of ['add', 'change']) {
227
+ watcher.on(type, async () => onChange());
228
+ }
229
+ }
230
+ }
231
+ exports.SwcCompiler = SwcCompiler;
@@ -0,0 +1,14 @@
1
+ import * as ts from 'typescript';
2
+ export interface TypeCheckerHostRunOptions {
3
+ watch?: boolean;
4
+ onTypeCheck?: (program: ts.Program) => void;
5
+ onProgramInit?: (program: ts.Program) => void;
6
+ }
7
+ export declare class TypeCheckerHost {
8
+ private readonly typescriptLoader;
9
+ private readonly tsConfigProvider;
10
+ run(tsconfigPath: string | undefined, options: TypeCheckerHostRunOptions): void;
11
+ private runInWatchMode;
12
+ private runOnce;
13
+ private createWatchCompilerHost;
14
+ }
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TypeCheckerHost = void 0;
4
+ const ansis_1 = require("ansis");
5
+ const ora = require("ora");
6
+ const ts = require("typescript");
7
+ const tsconfig_provider_1 = require("../helpers/tsconfig-provider");
8
+ const typescript_loader_1 = require("../typescript-loader");
9
+ const constants_1 = require("./constants");
10
+ class TypeCheckerHost {
11
+ constructor() {
12
+ this.typescriptLoader = new typescript_loader_1.TypeScriptBinaryLoader();
13
+ this.tsConfigProvider = new tsconfig_provider_1.TsConfigProvider(this.typescriptLoader);
14
+ }
15
+ run(tsconfigPath, options) {
16
+ if (!tsconfigPath) {
17
+ throw new Error('"tsconfigPath" is required when "tsProgramRef" is not provided.');
18
+ }
19
+ const tsBinary = this.typescriptLoader.load();
20
+ const spinner = ora({
21
+ text: constants_1.INITIALIZING_TYPE_CHECKER,
22
+ });
23
+ if (options.watch) {
24
+ console.log();
25
+ spinner.start();
26
+ this.runInWatchMode(tsconfigPath, tsBinary, options);
27
+ spinner.succeed();
28
+ return;
29
+ }
30
+ spinner.start();
31
+ this.runOnce(tsconfigPath, tsBinary, options);
32
+ spinner.succeed();
33
+ }
34
+ runInWatchMode(tsconfigPath, tsBinary, options) {
35
+ const { options: tsOptions } = this.tsConfigProvider.getByConfigFilename(tsconfigPath);
36
+ let builderProgram = undefined;
37
+ const reportWatchStatusCallback = (diagnostic) => {
38
+ if (diagnostic.messageText !== constants_1.TSC_NO_ERRORS_MESSAGE) {
39
+ if (diagnostic.messageText?.includes('Found')) {
40
+ console.log(constants_1.TSC_LOG_ERROR_PREFIX, (0, ansis_1.red)(diagnostic.messageText.toString()));
41
+ }
42
+ return;
43
+ }
44
+ if (!builderProgram) {
45
+ return;
46
+ }
47
+ const tsProgram = builderProgram.getProgram().getProgram();
48
+ options.onTypeCheck?.(tsProgram);
49
+ };
50
+ const host = this.createWatchCompilerHost(tsBinary, tsconfigPath, tsOptions, reportWatchStatusCallback);
51
+ builderProgram = tsBinary.createWatchProgram(host);
52
+ process.nextTick(() => {
53
+ options.onProgramInit?.(builderProgram.getProgram().getProgram());
54
+ });
55
+ }
56
+ runOnce(tsconfigPath, tsBinary, options) {
57
+ const { options: tsOptions, fileNames, projectReferences, } = this.tsConfigProvider.getByConfigFilename(tsconfigPath);
58
+ const createProgram = tsBinary.createIncrementalProgram ?? tsBinary.createProgram;
59
+ const program = createProgram.call(ts, {
60
+ rootNames: fileNames,
61
+ projectReferences,
62
+ options: tsOptions,
63
+ });
64
+ const programRef = program.getProgram
65
+ ? program.getProgram()
66
+ : program;
67
+ const diagnostics = tsBinary.getPreEmitDiagnostics(programRef);
68
+ if (diagnostics.length > 0) {
69
+ const formatDiagnosticsHost = {
70
+ getCanonicalFileName: (path) => path,
71
+ getCurrentDirectory: tsBinary.sys.getCurrentDirectory,
72
+ getNewLine: () => tsBinary.sys.newLine,
73
+ };
74
+ console.log();
75
+ console.log(tsBinary.formatDiagnosticsWithColorAndContext(diagnostics, formatDiagnosticsHost));
76
+ process.exit(1);
77
+ }
78
+ options.onTypeCheck?.(programRef);
79
+ }
80
+ createWatchCompilerHost(tsBinary, tsConfigPath, options, reportWatchStatusCallback) {
81
+ const origDiagnosticReporter = tsBinary.createDiagnosticReporter(tsBinary.sys, true);
82
+ const tsOptions = {
83
+ ...options,
84
+ preserveWatchOutput: true,
85
+ noEmit: true,
86
+ };
87
+ return tsBinary.createWatchCompilerHost(tsConfigPath, tsOptions, tsBinary.sys, undefined, origDiagnosticReporter, reportWatchStatusCallback);
88
+ }
89
+ }
90
+ exports.TypeCheckerHost = TypeCheckerHost;
@@ -0,0 +1,6 @@
1
+ import * as ts from 'typescript';
2
+ export declare class TypeScriptBinaryLoader {
3
+ private tsBinary?;
4
+ load(): typeof ts;
5
+ getModulePaths(): string[];
6
+ }
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TypeScriptBinaryLoader = void 0;
4
+ class TypeScriptBinaryLoader {
5
+ load() {
6
+ if (this.tsBinary) {
7
+ return this.tsBinary;
8
+ }
9
+ try {
10
+ const tsBinaryPath = require.resolve('typescript', {
11
+ paths: [process.cwd(), ...this.getModulePaths()],
12
+ });
13
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
14
+ const tsBinary = require(tsBinaryPath);
15
+ this.tsBinary = tsBinary;
16
+ return tsBinary;
17
+ }
18
+ catch {
19
+ throw new Error('TypeScript could not be found! Please, install "typescript" package.');
20
+ }
21
+ }
22
+ getModulePaths() {
23
+ const modulePaths = module.paths.slice(2, module.paths.length);
24
+ const packageDeps = modulePaths.slice(0, 3);
25
+ return [
26
+ ...packageDeps.reverse(),
27
+ ...modulePaths.slice(3, modulePaths.length).reverse(),
28
+ ];
29
+ }
30
+ }
31
+ exports.TypeScriptBinaryLoader = TypeScriptBinaryLoader;
@@ -0,0 +1,22 @@
1
+ import { Configuration } from '../configuration';
2
+ import { BaseCompiler } from './base-compiler';
3
+ import { TsConfigProvider } from './helpers/tsconfig-provider';
4
+ import { PluginsLoader } from './plugins/plugins-loader';
5
+ import { TypeScriptBinaryLoader } from './typescript-loader';
6
+ type TypescriptWatchCompilerExtras = {
7
+ /**
8
+ * If `undefined`, the value of 'preserveWatchOutput' option from tsconfig
9
+ * file will be used instead.
10
+ */
11
+ preserveWatchOutput: boolean | undefined;
12
+ };
13
+ export declare class WatchCompiler extends BaseCompiler<TypescriptWatchCompilerExtras> {
14
+ private readonly tsConfigProvider;
15
+ private readonly typescriptLoader;
16
+ constructor(pluginsLoader: PluginsLoader, tsConfigProvider: TsConfigProvider, typescriptLoader: TypeScriptBinaryLoader);
17
+ run(configuration: Required<Configuration>, tsConfigPath: string, appName: string | undefined, extras: TypescriptWatchCompilerExtras, onSuccess?: () => void): void;
18
+ private overrideCreateProgramFn;
19
+ private createDiagnosticReporter;
20
+ private createWatchStatusChanged;
21
+ }
22
+ export {};
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WatchCompiler = void 0;
4
+ const errors_1 = require("../ui/errors");
5
+ const base_compiler_1 = require("./base-compiler");
6
+ const get_value_or_default_1 = require("./helpers/get-value-or-default");
7
+ const manual_restart_1 = require("./helpers/manual-restart");
8
+ const tsconfig_paths_hook_1 = require("./hooks/tsconfig-paths.hook");
9
+ class WatchCompiler extends base_compiler_1.BaseCompiler {
10
+ constructor(pluginsLoader, tsConfigProvider, typescriptLoader) {
11
+ super(pluginsLoader);
12
+ this.tsConfigProvider = tsConfigProvider;
13
+ this.typescriptLoader = typescriptLoader;
14
+ }
15
+ run(configuration, tsConfigPath, appName, extras, onSuccess) {
16
+ const tsBin = this.typescriptLoader.load();
17
+ const configPath = tsBin.findConfigFile(process.cwd(), tsBin.sys.fileExists, tsConfigPath);
18
+ if (!configPath) {
19
+ throw new Error(errors_1.CLI_ERRORS.MISSING_TYPESCRIPT(tsConfigPath));
20
+ }
21
+ const { options, projectReferences } = this.tsConfigProvider.getByConfigFilename(tsConfigPath);
22
+ const createProgram = tsBin.createEmitAndSemanticDiagnosticsBuilderProgram;
23
+ const origDiagnosticReporter = tsBin.createDiagnosticReporter(tsBin.sys, true);
24
+ const origWatchStatusReporter = tsBin.createWatchStatusReporter(tsBin.sys, true);
25
+ const host = tsBin.createWatchCompilerHost(configPath, {
26
+ ...options,
27
+ preserveWatchOutput: extras.preserveWatchOutput ?? options.preserveWatchOutput,
28
+ }, tsBin.sys, createProgram, this.createDiagnosticReporter(origDiagnosticReporter), this.createWatchStatusChanged(origWatchStatusReporter, onSuccess));
29
+ const manualRestart = (0, get_value_or_default_1.getValueOrDefault)(configuration, 'compilerOptions.manualRestart', appName);
30
+ const plugins = this.loadPlugins(configuration, tsConfigPath, appName);
31
+ this.overrideCreateProgramFn(host, manualRestart, projectReferences, plugins);
32
+ const watchProgram = tsBin.createWatchProgram(host);
33
+ if (manualRestart) {
34
+ (0, manual_restart_1.listenForManualRestart)(() => {
35
+ watchProgram.close();
36
+ this.run(configuration, tsConfigPath, appName, extras, onSuccess);
37
+ });
38
+ }
39
+ }
40
+ overrideCreateProgramFn(host, manualRestart, projectReferences, plugins) {
41
+ const origCreateProgram = host.createProgram;
42
+ host.createProgram = (rootNames, options,
43
+ // tslint:disable-next-line:no-shadowed-variable
44
+ host, oldProgram) => {
45
+ if (manualRestart) {
46
+ (0, manual_restart_1.displayManualRestartTip)();
47
+ }
48
+ const tsconfigPathsPlugin = options
49
+ ? (0, tsconfig_paths_hook_1.tsconfigPathsBeforeHookFactory)(options)
50
+ : null;
51
+ const program = origCreateProgram(rootNames, options, host, oldProgram, undefined, projectReferences);
52
+ const origProgramEmit = program.emit;
53
+ program.emit = (targetSourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, customTransformers) => {
54
+ let transforms = customTransformers;
55
+ transforms = typeof transforms !== 'object' ? {} : transforms;
56
+ const before = plugins.beforeHooks.map((hook) => hook(program.getProgram()));
57
+ const after = plugins.afterHooks.map((hook) => hook(program.getProgram()));
58
+ const afterDeclarations = plugins.afterDeclarationsHooks.map((hook) => hook(program.getProgram()));
59
+ if (tsconfigPathsPlugin) {
60
+ before.unshift(tsconfigPathsPlugin);
61
+ }
62
+ transforms.before = before.concat(transforms.before || []);
63
+ transforms.after = after.concat(transforms.after || []);
64
+ transforms.afterDeclarations = afterDeclarations.concat(transforms.afterDeclarations || []);
65
+ return origProgramEmit(targetSourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, transforms);
66
+ };
67
+ return program;
68
+ };
69
+ }
70
+ createDiagnosticReporter(diagnosticReporter) {
71
+ return function (diagnostic, ...args) {
72
+ return diagnosticReporter.call(this, diagnostic, ...args);
73
+ };
74
+ }
75
+ createWatchStatusChanged(watchStatusReporter, onSuccess) {
76
+ return function (diagnostic, ...args) {
77
+ const messageText = diagnostic && diagnostic.messageText;
78
+ const noErrorsMessage = '0 errors';
79
+ if (messageText &&
80
+ messageText.includes &&
81
+ messageText.includes(noErrorsMessage) &&
82
+ onSuccess) {
83
+ onSuccess();
84
+ }
85
+ return watchStatusReporter.call(this, diagnostic, ...args);
86
+ };
87
+ }
88
+ }
89
+ exports.WatchCompiler = WatchCompiler;
@@ -0,0 +1,21 @@
1
+ import { Input } from '../../commands';
2
+ import { Configuration } from '../configuration';
3
+ import { AssetsManager } from './assets-manager';
4
+ import { BaseCompiler } from './base-compiler';
5
+ import { PluginsLoader } from './plugins/plugins-loader';
6
+ import webpack = require('webpack');
7
+ type WebpackConfigFactory = (config: webpack.Configuration, webpackRef: typeof webpack) => webpack.Configuration;
8
+ type WebpackConfigFactoryOrConfig = WebpackConfigFactory | webpack.Configuration;
9
+ type WebpackCompilerExtras = {
10
+ inputs: Input[];
11
+ assetsManager: AssetsManager;
12
+ webpackConfigFactoryOrConfig: WebpackConfigFactoryOrConfig | WebpackConfigFactoryOrConfig[];
13
+ debug?: boolean;
14
+ watchMode?: boolean;
15
+ };
16
+ export declare class WebpackCompiler extends BaseCompiler<WebpackCompilerExtras> {
17
+ constructor(pluginsLoader: PluginsLoader);
18
+ run(configuration: Required<Configuration>, tsConfigPath: string, appName: string | undefined, extras: WebpackCompilerExtras, onSuccess?: () => void): void;
19
+ private createAfterCallback;
20
+ }
21
+ export {};
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WebpackCompiler = void 0;
4
+ const fs_1 = require("fs");
5
+ const path_1 = require("path");
6
+ const ui_1 = require("../ui");
7
+ const base_compiler_1 = require("./base-compiler");
8
+ const webpack_defaults_1 = require("./defaults/webpack-defaults");
9
+ const get_value_or_default_1 = require("./helpers/get-value-or-default");
10
+ const webpack = require("webpack");
11
+ class WebpackCompiler extends base_compiler_1.BaseCompiler {
12
+ constructor(pluginsLoader) {
13
+ super(pluginsLoader);
14
+ }
15
+ run(configuration, tsConfigPath, appName, extras, onSuccess) {
16
+ const cwd = process.cwd();
17
+ const configPath = (0, path_1.join)(cwd, tsConfigPath);
18
+ if (!(0, fs_1.existsSync)(configPath)) {
19
+ throw new Error(`Could not find TypeScript configuration file "${tsConfigPath}".`);
20
+ }
21
+ const plugins = this.loadPlugins(configuration, tsConfigPath, appName);
22
+ const pathToSource = this.getPathToSource(configuration, tsConfigPath, appName);
23
+ const entryFile = (0, get_value_or_default_1.getValueOrDefault)(configuration, 'entryFile', appName, 'entryFile', extras.inputs);
24
+ const entryFileRoot = (0, get_value_or_default_1.getValueOrDefault)(configuration, 'root', appName) || '';
25
+ const defaultOptions = (0, webpack_defaults_1.webpackDefaultsFactory)(pathToSource, entryFileRoot, entryFile, extras.debug ?? false, tsConfigPath, plugins);
26
+ let compiler;
27
+ let watchOptions;
28
+ let watch;
29
+ if (Array.isArray(extras.webpackConfigFactoryOrConfig)) {
30
+ const webpackConfigurations = extras.webpackConfigFactoryOrConfig.map((configOrFactory) => {
31
+ const unwrappedConfig = typeof configOrFactory !== 'function'
32
+ ? configOrFactory
33
+ : configOrFactory(defaultOptions, webpack);
34
+ return {
35
+ ...defaultOptions,
36
+ mode: extras.watchMode ? 'development' : defaultOptions.mode,
37
+ ...unwrappedConfig,
38
+ };
39
+ });
40
+ compiler = webpack(webpackConfigurations);
41
+ watchOptions = webpackConfigurations.map((config) => config.watchOptions || {});
42
+ watch = webpackConfigurations.some((config) => config.watch);
43
+ }
44
+ else {
45
+ const projectWebpackOptions = typeof extras.webpackConfigFactoryOrConfig !== 'function'
46
+ ? extras.webpackConfigFactoryOrConfig
47
+ : extras.webpackConfigFactoryOrConfig(defaultOptions, webpack);
48
+ const webpackConfiguration = {
49
+ ...defaultOptions,
50
+ mode: extras.watchMode ? 'development' : defaultOptions.mode,
51
+ ...projectWebpackOptions,
52
+ };
53
+ compiler = webpack(webpackConfiguration);
54
+ watchOptions = webpackConfiguration.watchOptions;
55
+ watch = webpackConfiguration.watch;
56
+ }
57
+ const afterCallback = this.createAfterCallback(onSuccess, extras.assetsManager, extras.watchMode ?? false, watch);
58
+ if (extras.watchMode || watch) {
59
+ compiler.hooks.watchRun.tapAsync('Rebuild info', (params, callback) => {
60
+ console.log(`\n${ui_1.INFO_PREFIX} Webpack is building your sources...\n`);
61
+ callback();
62
+ });
63
+ compiler.watch(watchOptions || {}, afterCallback);
64
+ }
65
+ else {
66
+ compiler.run(afterCallback);
67
+ }
68
+ }
69
+ createAfterCallback(onSuccess, assetsManager, watchMode, watch) {
70
+ return (err, stats) => {
71
+ if (err && stats === undefined) {
72
+ // Could not complete the compilation
73
+ // The error caught is most likely thrown by underlying tasks
74
+ console.log(err);
75
+ return process.exit(1);
76
+ }
77
+ const statsOutput = stats.toString({
78
+ chunks: false,
79
+ colors: true,
80
+ modules: false,
81
+ assets: false,
82
+ });
83
+ if (!err && !stats.hasErrors()) {
84
+ if (!onSuccess) {
85
+ assetsManager.closeWatchers();
86
+ }
87
+ else {
88
+ onSuccess();
89
+ }
90
+ }
91
+ else if (!watchMode && !watch) {
92
+ console.log(statsOutput);
93
+ return process.exit(1);
94
+ }
95
+ console.log(statsOutput);
96
+ };
97
+ }
98
+ }
99
+ exports.WebpackCompiler = WebpackCompiler;