@powerlines/nx 0.1.0

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 (128) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +490 -0
  3. package/dist/chunk-23KFTIT2.mjs +1 -0
  4. package/dist/chunk-2IKNOV7W.mjs +305 -0
  5. package/dist/chunk-326QB2VK.mjs +14 -0
  6. package/dist/chunk-3QPPK4K2.js +18 -0
  7. package/dist/chunk-5K7DMFAV.mjs +15 -0
  8. package/dist/chunk-6UG6SXHV.js +4307 -0
  9. package/dist/chunk-6ZABD5GY.js +18 -0
  10. package/dist/chunk-AKX3TTDA.js +18 -0
  11. package/dist/chunk-CQP2LCFV.mjs +19 -0
  12. package/dist/chunk-CYWOTYVX.mjs +108 -0
  13. package/dist/chunk-E6VIOC6R.mjs +19 -0
  14. package/dist/chunk-ED7GH5H4.js +30 -0
  15. package/dist/chunk-ESI25Y6Y.mjs +15 -0
  16. package/dist/chunk-FK3VJFET.mjs +28 -0
  17. package/dist/chunk-FXCIWWXO.mjs +15 -0
  18. package/dist/chunk-HHPODCTP.js +126 -0
  19. package/dist/chunk-N2YKXZ5R.js +2 -0
  20. package/dist/chunk-O6BSKBJP.js +26 -0
  21. package/dist/chunk-O6YSETKJ.mjs +4 -0
  22. package/dist/chunk-OOERKIVS.js +312 -0
  23. package/dist/chunk-OZMYXUES.mjs +4276 -0
  24. package/dist/chunk-SHUYVCID.js +6 -0
  25. package/dist/chunk-TCSCYVA7.mjs +76 -0
  26. package/dist/chunk-UV4HQO3Y.mjs +1 -0
  27. package/dist/chunk-WKH7IEJU.js +26 -0
  28. package/dist/chunk-WUJKJGEW.js +17 -0
  29. package/dist/chunk-XO62WWX4.js +2 -0
  30. package/dist/chunk-XPEFTYN7.js +82 -0
  31. package/dist/executor-B2Y0F4JC.d.ts +83 -0
  32. package/dist/executor-BAcpDLQu.d.ts +83 -0
  33. package/dist/executor-BHIUeaip.d.ts +93 -0
  34. package/dist/executor-BYGMOBlU.d.mts +83 -0
  35. package/dist/executor-Ba95I3EK.d.mts +93 -0
  36. package/dist/executor-CIEyQL42.d.mts +83 -0
  37. package/dist/executor-CkqLFUNx.d.ts +83 -0
  38. package/dist/executor-D055lNr2.d.ts +83 -0
  39. package/dist/executor-D0PJOQkn.d.mts +83 -0
  40. package/dist/executor-DsRaA9mk.d.mts +83 -0
  41. package/dist/executors.d.mts +12 -0
  42. package/dist/executors.d.ts +12 -0
  43. package/dist/executors.js +34 -0
  44. package/dist/executors.mjs +9 -0
  45. package/dist/generators.d.mts +19 -0
  46. package/dist/generators.d.ts +19 -0
  47. package/dist/generators.js +16 -0
  48. package/dist/generators.mjs +3 -0
  49. package/dist/index.d.mts +16 -0
  50. package/dist/index.d.ts +16 -0
  51. package/dist/index.js +53 -0
  52. package/dist/index.mjs +12 -0
  53. package/dist/src/base/base-executor.d.mts +27 -0
  54. package/dist/src/base/base-executor.d.ts +27 -0
  55. package/dist/src/base/base-executor.js +12 -0
  56. package/dist/src/base/base-executor.mjs +3 -0
  57. package/dist/src/base/base-executor.schema.d.d.mts +75 -0
  58. package/dist/src/base/base-executor.schema.d.d.ts +75 -0
  59. package/dist/src/base/base-executor.schema.d.js +2 -0
  60. package/dist/src/base/base-executor.schema.d.mjs +1 -0
  61. package/dist/src/base/base-executor.schema.d.ts +75 -0
  62. package/dist/src/base/base-executor.schema.json +77 -0
  63. package/dist/src/base/base-executor.untyped.d.mts +5 -0
  64. package/dist/src/base/base-executor.untyped.d.ts +5 -0
  65. package/dist/src/base/base-executor.untyped.js +10 -0
  66. package/dist/src/base/base-executor.untyped.mjs +2 -0
  67. package/dist/src/executors/build/executor.d.mts +8 -0
  68. package/dist/src/executors/build/executor.d.ts +8 -0
  69. package/dist/src/executors/build/executor.js +19 -0
  70. package/dist/src/executors/build/executor.mjs +4 -0
  71. package/dist/src/executors/build/untyped.d.mts +5 -0
  72. package/dist/src/executors/build/untyped.d.ts +5 -0
  73. package/dist/src/executors/build/untyped.js +37 -0
  74. package/dist/src/executors/build/untyped.mjs +33 -0
  75. package/dist/src/executors/clean/executor.d.mts +8 -0
  76. package/dist/src/executors/clean/executor.d.ts +8 -0
  77. package/dist/src/executors/clean/executor.js +19 -0
  78. package/dist/src/executors/clean/executor.mjs +4 -0
  79. package/dist/src/executors/clean/untyped.d.mts +5 -0
  80. package/dist/src/executors/clean/untyped.d.ts +5 -0
  81. package/dist/src/executors/clean/untyped.js +19 -0
  82. package/dist/src/executors/clean/untyped.mjs +15 -0
  83. package/dist/src/executors/docs/executor.d.mts +8 -0
  84. package/dist/src/executors/docs/executor.d.ts +8 -0
  85. package/dist/src/executors/docs/executor.js +19 -0
  86. package/dist/src/executors/docs/executor.mjs +4 -0
  87. package/dist/src/executors/docs/untyped.d.mts +5 -0
  88. package/dist/src/executors/docs/untyped.d.ts +5 -0
  89. package/dist/src/executors/docs/untyped.js +20 -0
  90. package/dist/src/executors/docs/untyped.mjs +16 -0
  91. package/dist/src/executors/lint/executor.d.mts +8 -0
  92. package/dist/src/executors/lint/executor.d.ts +8 -0
  93. package/dist/src/executors/lint/executor.js +19 -0
  94. package/dist/src/executors/lint/executor.mjs +4 -0
  95. package/dist/src/executors/lint/untyped.d.mts +5 -0
  96. package/dist/src/executors/lint/untyped.d.ts +5 -0
  97. package/dist/src/executors/lint/untyped.js +20 -0
  98. package/dist/src/executors/lint/untyped.mjs +16 -0
  99. package/dist/src/executors/prepare/executor.d.mts +8 -0
  100. package/dist/src/executors/prepare/executor.d.ts +8 -0
  101. package/dist/src/executors/prepare/executor.js +19 -0
  102. package/dist/src/executors/prepare/executor.mjs +4 -0
  103. package/dist/src/executors/prepare/untyped.d.mts +5 -0
  104. package/dist/src/executors/prepare/untyped.d.ts +5 -0
  105. package/dist/src/executors/prepare/untyped.js +11 -0
  106. package/dist/src/executors/prepare/untyped.mjs +3 -0
  107. package/dist/src/generators/sync/generator.d.mts +6 -0
  108. package/dist/src/generators/sync/generator.d.ts +6 -0
  109. package/dist/src/generators/sync/generator.js +17 -0
  110. package/dist/src/generators/sync/generator.mjs +2 -0
  111. package/dist/src/generators/sync/untyped.d.mts +5 -0
  112. package/dist/src/generators/sync/untyped.d.ts +5 -0
  113. package/dist/src/generators/sync/untyped.js +25 -0
  114. package/dist/src/generators/sync/untyped.mjs +21 -0
  115. package/dist/src/plugin/index.d.mts +8 -0
  116. package/dist/src/plugin/index.d.ts +8 -0
  117. package/dist/src/plugin/index.js +16 -0
  118. package/dist/src/plugin/index.mjs +3 -0
  119. package/docs/api/base-executor.schema.md +72 -0
  120. package/docs/api/build/schema.md +82 -0
  121. package/docs/api/clean/schema.md +72 -0
  122. package/docs/api/docs/schema.md +72 -0
  123. package/docs/api/lint/schema.md +72 -0
  124. package/docs/api/prepare/schema.md +72 -0
  125. package/docs/api/sync/schema.md +16 -0
  126. package/executors.json +30 -0
  127. package/generators.json +14 -0
  128. package/package.json +172 -0
@@ -0,0 +1,4276 @@
1
+ import { __VFS_CACHE__, __VFS_RESOLVER__, __VFS_VIRTUAL__, __VFS_UNIFIED__, __VFS_INIT__, __VFS_REVERT__, loadWorkspaceConfig, CACHE_HASH_LENGTH, PROJECT_ROOT_HASH_LENGTH, getPrefixedProjectRootHash, getChecksum, loadUserConfigFile, writeMetaFile } from './chunk-CYWOTYVX.mjs';
2
+ import { __name } from './chunk-O6YSETKJ.mjs';
3
+ import { getLogFn, getLogLevel, writeError } from '@storm-software/config-tools/logger';
4
+ import { withRunExecutor } from '@storm-software/workspace-tools/base/base-executor';
5
+ import { isError } from '@stryke/type-checks/is-error';
6
+ import defu4, { defu } from 'defu';
7
+ import { formatLogMessage } from '@storm-software/config-tools/logger/console';
8
+ import { LogLevelLabel } from '@storm-software/config-tools/types';
9
+ import { toArray } from '@stryke/convert/to-array';
10
+ import { copyFiles } from '@stryke/fs/copy-file';
11
+ import { existsSync } from '@stryke/fs/exists';
12
+ import { createDirectory, removeDirectory } from '@stryke/fs/helpers';
13
+ import { install } from '@stryke/fs/install';
14
+ import { listFiles, listFilesSync } from '@stryke/fs/list-files';
15
+ import { isPackageExists, isPackageListed, doesPackageMatch, getPackageListing } from '@stryke/fs/package-fns';
16
+ import { findFilePath, findFileExtensionSafe, relativePath, findFileName } from '@stryke/path/file-path-fns';
17
+ import { isParentPath } from '@stryke/path/is-parent-path';
18
+ import { joinPaths } from '@stryke/path/join-paths';
19
+ import { replacePath } from '@stryke/path/replace';
20
+ import { isFunction } from '@stryke/type-checks/is-function';
21
+ import { isNumber } from '@stryke/type-checks/is-number';
22
+ import { isSet } from '@stryke/type-checks/is-set';
23
+ import { isSetObject } from '@stryke/type-checks/is-set-object';
24
+ import { isSetString } from '@stryke/type-checks/is-set-string';
25
+ import chalk5 from 'chalk';
26
+ import Handlebars from 'handlebars';
27
+ import { writeFile as writeFile$1 } from 'node:fs/promises';
28
+ import path, { sep } from 'node:path';
29
+ import { Application, TypeDocReader, PackageJsonReader, TSConfigReader, Converter, ReflectionKind, PageEvent } from 'typedoc';
30
+ import { readJsonFile, readJsonFileSync } from '@stryke/fs/json';
31
+ import { appendPath } from '@stryke/path/append';
32
+ import ts3, { createProgram, createCompilerHost, getPreEmitDiagnostics, getLineAndCharacterOfPosition, flattenDiagnosticMessageText } from 'typescript';
33
+ import { isUndefined } from '@stryke/type-checks/is-undefined';
34
+ import { writeFile } from '@stryke/fs/write-file';
35
+ import { format, resolveConfig } from 'prettier';
36
+ import { resolvePackage } from '@stryke/fs/resolve';
37
+ import { titleCase } from '@stryke/string-format/title-case';
38
+ import { getEnvPaths } from '@stryke/env/get-env-paths';
39
+ import { relativeToWorkspaceRoot, getWorkspaceRoot } from '@stryke/fs/get-workspace-root';
40
+ import { murmurhash } from '@stryke/hash/murmurhash';
41
+ import { getUnique, getUniqueBy } from '@stryke/helpers/get-unique';
42
+ import { omit } from '@stryke/helpers/omit';
43
+ import { joinPaths as joinPaths$1 } from '@stryke/path/join';
44
+ import { isNull } from '@stryke/type-checks/is-null';
45
+ import { isString } from '@stryke/type-checks/is-string';
46
+ import { uuid } from '@stryke/unique-id/uuid';
47
+ import { parseTypeDefinition } from '@stryke/convert/parse-type-definition';
48
+ import { getColor } from '@storm-software/config-tools/utilities/colors';
49
+ import { noop } from '@stryke/helpers/noop';
50
+ import { createJiti } from 'jiti';
51
+ import { bufferToString } from '@stryke/convert/buffer-to-string';
52
+ import { isAbsolutePath } from '@stryke/path/is-type';
53
+ import { prettyBytes } from '@stryke/string-format/pretty-bytes';
54
+ import { isBuffer } from '@stryke/type-checks/is-buffer';
55
+ import { Volume } from 'memfs';
56
+ import { Blob } from 'node:buffer';
57
+ import fs from 'node:fs';
58
+ import { Union } from 'unionfs';
59
+ import { isObject } from '@stryke/type-checks/is-object';
60
+ import { isDirectory } from '@stryke/fs/is-file';
61
+ import { readFile, readFileIfExistingSync } from '@stryke/fs/read-file';
62
+ import { isValidRange, parseVersion } from '@stryke/fs/semver-fns';
63
+ import { getParentPath } from '@stryke/path/get-parent-path';
64
+ import { hasPackageVersion, getPackageName, getPackageVersion } from '@stryke/string-format/package';
65
+ import { transformAsync } from '@babel/core';
66
+ import MagicString from 'magic-string';
67
+ import { declare } from '@babel/helper-plugin-utils';
68
+ import * as t from '@babel/types';
69
+ import { getObjectDiff } from '@donedeal0/superdiff';
70
+ import { loadTsConfig } from '@stryke/fs/tsconfig';
71
+ import { StormJSON } from '@stryke/json/storm-json';
72
+ import { minimatch } from 'minimatch';
73
+ import { isDevelopmentMode, isTestMode } from '@stryke/env/environment-checks';
74
+ import { createVitePlugin, createEsbuildPlugin, createWebpackPlugin, createRolldownPlugin, createRollupPlugin, createRspackPlugin, createUnloaderPlugin } from 'unplugin';
75
+ import { match, tsconfigPathsToRegExp } from 'bundle-require';
76
+ import { defineNuxtModule, addVitePlugin, addWebpackPlugin } from '@nuxt/kit';
77
+ import '@nuxt/schema';
78
+
79
+ var objectToFrontmatter = /* @__PURE__ */ __name((object = {}) => Object.entries(object).filter(([, value]) => {
80
+ return value !== void 0 && value !== null && value !== "" && typeof value === "string" || Array.isArray(value) && value.length > 0;
81
+ }).map(([key, value]) => `${key}: ${value}`).join("\n"), "objectToFrontmatter");
82
+ var onRendererPageEnd = /* @__PURE__ */ __name((frontmatterObject) => (event) => {
83
+ if (!event.contents) {
84
+ return;
85
+ } else if (/README\.md$/.test(event.url)) {
86
+ event.preventDefault();
87
+ return;
88
+ }
89
+ const frontmatter = `---
90
+ title: '${event.model.name}'
91
+ ${objectToFrontmatter(frontmatterObject)}
92
+ ---
93
+
94
+ `;
95
+ event.contents = frontmatter + event.contents;
96
+ }, "onRendererPageEnd");
97
+ var buildNavigationFromProjectReflection = /* @__PURE__ */ __name((baseUrl = "", project) => {
98
+ const baseUrlWithoutTrailingSlash = baseUrl.replace(/\/$/gm, "");
99
+ const result = {
100
+ type: "flat"
101
+ };
102
+ const isGroupOfModules = /* @__PURE__ */ __name((group) => group.title === "Modules", "isGroupOfModules");
103
+ const reflectionToNavItem = /* @__PURE__ */ __name((reflection) => {
104
+ return {
105
+ title: reflection.name,
106
+ url: `${baseUrlWithoutTrailingSlash}/${reflection.url}`.replace(/\.md$/, "")
107
+ };
108
+ }, "reflectionToNavItem");
109
+ const modulesGroupToNavigationGroup = /* @__PURE__ */ __name((module) => ({
110
+ items: (module.groups ?? []).flatMap((group) => group.children.map(reflectionToNavItem)),
111
+ name: module.name
112
+ }), "modulesGroupToNavigationGroup");
113
+ const navFromReflectionGroups = /* @__PURE__ */ __name((groups, nav = {
114
+ type: "flat"
115
+ }) => {
116
+ groups.forEach((group) => {
117
+ if (isGroupOfModules(group)) {
118
+ nav.type = "modular";
119
+ nav.modules = group.children.map(modulesGroupToNavigationGroup);
120
+ } else {
121
+ nav.items = nav?.items?.length ? nav.items : [];
122
+ nav.items = nav.items.concat(group.children.flatMap(reflectionToNavItem));
123
+ }
124
+ });
125
+ return nav;
126
+ }, "navFromReflectionGroups");
127
+ return navFromReflectionGroups(project.groups, result);
128
+ }, "buildNavigationFromProjectReflection");
129
+ var onDeclaration = /* @__PURE__ */ __name((entryPoints = []) => (context, reflection) => {
130
+ if (reflection.kind === ReflectionKind.Module) {
131
+ const matchingEntryPoint = entryPoints.find((entryPoint) => entryPoint.path === reflection.sources[0].fullFileName);
132
+ reflection.name = matchingEntryPoint?.name ?? reflection.name;
133
+ }
134
+ }, "onDeclaration");
135
+ var typedocConfig = {
136
+ excludeExternals: true,
137
+ excludeInternal: true,
138
+ excludePrivate: true,
139
+ excludeProtected: true,
140
+ githubPages: false
141
+ };
142
+ var markdownPluginConfig = {
143
+ hideBreadcrumbs: true,
144
+ hideInPageTOC: true,
145
+ hidePageHeader: true,
146
+ hidePageTitle: true
147
+ };
148
+ var removeTrailingSlash = /* @__PURE__ */ __name((pathString = "") => pathString.endsWith(sep) ? pathString.slice(0, pathString.length - 1) : pathString, "removeTrailingSlash");
149
+ var initTypedoc = /* @__PURE__ */ __name(async (context, options) => {
150
+ const { baseUrl = "/docs/", outputPath } = options;
151
+ const entryPoints = options.entryPoints ?? context.entry.map((entry) => ({
152
+ name: entry.name || entry.output || findFileName(entry.file, {
153
+ withExtension: false
154
+ }),
155
+ path: joinPaths(context.config.projectRoot, entry.file)
156
+ }));
157
+ const app = await Application.bootstrapWithPlugins({
158
+ ...typedocConfig,
159
+ ...markdownPluginConfig,
160
+ gitRevision: context.workspaceConfig.branch || "main",
161
+ tsconfig: context.tsconfig.tsconfigFilePath,
162
+ exclude: context.tsconfig.tsconfigJson.exclude?.filter(Boolean),
163
+ out: outputPath,
164
+ basePath: baseUrl,
165
+ entryPoints: entryPoints?.map((e) => e.path),
166
+ plugin: [
167
+ "typedoc-plugin-markdown",
168
+ "powerlines/lib/typedoc/plugin"
169
+ ],
170
+ theme: "powerlines",
171
+ readme: "none",
172
+ excludePrivate: true,
173
+ hideGenerator: true
174
+ }, [
175
+ new TypeDocReader(),
176
+ new PackageJsonReader(),
177
+ new TSConfigReader()
178
+ ]);
179
+ app.options.addReader(new TSConfigReader());
180
+ app.converter.on(Converter.EVENT_CREATE_DECLARATION, onDeclaration(entryPoints));
181
+ const getReflections = /* @__PURE__ */ __name(async () => app.convert(), "getReflections");
182
+ const generateDocs = /* @__PURE__ */ __name(async (opts) => {
183
+ const { outputPath: outputFolder, project, frontmatter } = opts;
184
+ app.renderer.on(PageEvent.END, onRendererPageEnd(frontmatter));
185
+ await app.generateDocs(project, outputFolder || outputPath);
186
+ }, "generateDocs");
187
+ const generateNavigationJSON = /* @__PURE__ */ __name(async (project, outputFolder = outputPath) => {
188
+ const navigation = buildNavigationFromProjectReflection(baseUrl, project);
189
+ await writeFile$1(`${removeTrailingSlash(outputFolder)}/nav.json`, JSON.stringify(navigation));
190
+ }, "generateNavigationJSON");
191
+ return {
192
+ app,
193
+ generateDocs,
194
+ generateNavigationJSON,
195
+ getReflections
196
+ };
197
+ }, "initTypedoc");
198
+ function getTsconfigFilePath(workspaceRoot, projectRoot, tsconfig = "tsconfig.json") {
199
+ let tsconfigFilePath = tsconfig;
200
+ if (!existsSync(tsconfigFilePath)) {
201
+ tsconfigFilePath = appendPath(tsconfig, projectRoot);
202
+ if (!existsSync(tsconfigFilePath)) {
203
+ tsconfigFilePath = appendPath(tsconfig, workspaceRoot);
204
+ if (!existsSync(tsconfigFilePath)) {
205
+ tsconfigFilePath = appendPath(tsconfig, joinPaths(workspaceRoot, projectRoot));
206
+ if (!existsSync(tsconfigFilePath)) {
207
+ throw new Error(`Cannot find the \`tsconfig.json\` configuration file at ${tsconfig}, ${appendPath(tsconfig, projectRoot)}, ${appendPath(tsconfig, workspaceRoot)}, or ${tsconfigFilePath}`);
208
+ }
209
+ }
210
+ }
211
+ }
212
+ return tsconfigFilePath;
213
+ }
214
+ __name(getTsconfigFilePath, "getTsconfigFilePath");
215
+ function findMatch(tsconfigType, types, extensions = [
216
+ ".ts",
217
+ ".tsx",
218
+ ".d.ts"
219
+ ]) {
220
+ return types.find((type) => tsconfigType?.toString().toLowerCase() === type?.toString().toLowerCase() || tsconfigType?.toString().toLowerCase() === `./${type?.toString().toLowerCase()}` || `./${tsconfigType?.toString().toLowerCase()}` === type?.toString().toLowerCase() || extensions.some((ext) => `${tsconfigType?.toString().toLowerCase()}${ext}` === type?.toString().toLowerCase() || `${tsconfigType?.toString().toLowerCase()}${ext}` === `./${type?.toString().toLowerCase()}` || `${type?.toString().toLowerCase()}${ext}` === `./${tsconfigType?.toString().toLowerCase()}` || tsconfigType?.toString().toLowerCase() === `${type?.toString().toLowerCase()}${ext}` || tsconfigType?.toString().toLowerCase() === `./${type?.toString().toLowerCase()}${ext}` || type?.toString().toLowerCase() === `./${tsconfigType?.toString().toLowerCase()}${ext}`));
221
+ }
222
+ __name(findMatch, "findMatch");
223
+ function findIncludeMatch(tsconfigType, types) {
224
+ return findMatch(tsconfigType, types, [
225
+ ".ts",
226
+ ".tsx",
227
+ ".d.ts",
228
+ ".js",
229
+ ".jsx",
230
+ ".mjs",
231
+ ".cjs",
232
+ ".mts",
233
+ ".cts",
234
+ "/*.ts",
235
+ "/*.tsx",
236
+ "/*.d.ts",
237
+ "/*.js",
238
+ "/*.jsx",
239
+ "/*.mjs",
240
+ "/*.cjs",
241
+ "/*.mts",
242
+ "/*.cts",
243
+ "/**/*.ts",
244
+ "/**/*.tsx",
245
+ "/**/*.d.ts",
246
+ "/**/*.js",
247
+ "/**/*.jsx",
248
+ "/**/*.mjs",
249
+ "/**/*.cjs",
250
+ "/**/*.mts",
251
+ "/**/*.cts"
252
+ ]);
253
+ }
254
+ __name(findIncludeMatch, "findIncludeMatch");
255
+ function isIncludeMatchFound(tsconfigType, types) {
256
+ return findIncludeMatch(tsconfigType, types) !== void 0;
257
+ }
258
+ __name(isIncludeMatchFound, "isIncludeMatchFound");
259
+ function getParsedTypeScriptConfig(workspaceRoot, projectRoot, tsconfig, tsconfigRaw = {}, originalTsconfigJson, host = ts3.sys) {
260
+ const tsconfigFilePath = getTsconfigFilePath(workspaceRoot, projectRoot, tsconfig);
261
+ const tsconfigJson = readJsonFileSync(tsconfigFilePath);
262
+ if (!tsconfigJson) {
263
+ throw new Error(`Cannot find the \`tsconfig.json\` configuration file at ${joinPaths(projectRoot, tsconfig ?? "tsconfig.json")}`);
264
+ }
265
+ const parsedCommandLine = ts3.parseJsonConfigFileContent(defu4(tsconfigRaw ?? {}, tsconfigJson), host, appendPath(projectRoot, workspaceRoot));
266
+ if (parsedCommandLine.errors.length > 0) {
267
+ const errorMessage = `Cannot parse the TypeScript compiler options. Please investigate the following issues:
268
+ ${parsedCommandLine.errors.map((error) => `- ${(error.category !== void 0 && error.code ? `[${error.category}-${error.code}]: ` : "") + error.messageText.toString()}`).join("\n")}
269
+ `;
270
+ throw new Error(errorMessage);
271
+ }
272
+ return {
273
+ ...parsedCommandLine,
274
+ originalTsconfigJson: originalTsconfigJson ?? tsconfigJson,
275
+ tsconfigJson,
276
+ tsconfigFilePath
277
+ };
278
+ }
279
+ __name(getParsedTypeScriptConfig, "getParsedTypeScriptConfig");
280
+
281
+ // ../powerlines/src/lib/utilities/file-header.ts
282
+ function getBaseFileHeader() {
283
+ return `
284
+ // Generated with Powerlines
285
+ // Note: Do not edit this file manually - it will be overwritten automatically
286
+ `;
287
+ }
288
+ __name(getBaseFileHeader, "getBaseFileHeader");
289
+ function getFileHeader(directive = "", prettierIgnore = false) {
290
+ if (directive && process.env.POWERLINES_LOCAL) {
291
+ directive = directive.replaceAll("powerlines/runtime-types", "../../dist/packages/types");
292
+ }
293
+ return `/* eslint-disable */
294
+ // biome-ignore lint: disable
295
+ ${prettierIgnore ? `// prettier-ignore` : ""}${directive ? `
296
+
297
+ ${directive}
298
+ ` : "\n"}
299
+ ${getBaseFileHeader()}
300
+
301
+ `;
302
+ }
303
+ __name(getFileHeader, "getFileHeader");
304
+
305
+ // ../powerlines/src/types/commands.ts
306
+ var SUPPORTED_COMMANDS = [
307
+ "new",
308
+ "clean",
309
+ "prepare",
310
+ "lint",
311
+ "test",
312
+ "build",
313
+ "docs",
314
+ "release",
315
+ "finalize"
316
+ ];
317
+
318
+ // ../powerlines/src/lib/utilities/plugin-helpers.ts
319
+ function isPlugin(value) {
320
+ return isSetObject(value) && "name" in value && isSetString(value.name) && (isUndefined(value.applyToEnvironment) || "applyToEnvironment" in value && isFunction(value.applyToEnvironment)) && (isUndefined(value.dedupe) || "dedupe" in value && isFunction(value.dedupe)) && (isUndefined(value.dependsOn) || "dependsOn" in value && Array.isArray(value.dependsOn) && value.dependsOn.every(isPluginConfig)) && SUPPORTED_COMMANDS.every((command) => isUndefined(value[command]) || command in value && (isFunction(value[command]) || isSetObject(value[command]) && "handler" in value[command] && isFunction(value[command].handler)));
321
+ }
322
+ __name(isPlugin, "isPlugin");
323
+ function isPluginConfigObject(value) {
324
+ return isSetObject(value) && "plugin" in value && ((isSetString(value.plugin) || isFunction(value.plugin)) && "options" in value && isSetObject(value.options) || isPlugin(value.plugin));
325
+ }
326
+ __name(isPluginConfigObject, "isPluginConfigObject");
327
+ function isPluginConfigTuple(value) {
328
+ return Array.isArray(value) && (value.length === 1 || value.length === 2) && ((isSetString(value[0]) || isFunction(value[0])) && value.length > 1 && isSetObject(value[1]) || isPlugin(value[0]));
329
+ }
330
+ __name(isPluginConfigTuple, "isPluginConfigTuple");
331
+ function isPluginConfig(value) {
332
+ return isSetString(value) || isFunction(value) || isPlugin(value) || isPluginConfigObject(value) || isPluginConfigTuple(value);
333
+ }
334
+ __name(isPluginConfig, "isPluginConfig");
335
+ function isPluginHookFunction(value) {
336
+ return isFunction(value) || isSetObject(value) && "handler" in value && isFunction(value.handler);
337
+ }
338
+ __name(isPluginHookFunction, "isPluginHookFunction");
339
+ function isPluginHookObject(value) {
340
+ return isSetObject(value) && "handler" in value && isFunction(value.handler);
341
+ }
342
+ __name(isPluginHookObject, "isPluginHookObject");
343
+ function isPluginHook(value) {
344
+ return isPluginHookFunction(value) || isPluginHookObject(value);
345
+ }
346
+ __name(isPluginHook, "isPluginHook");
347
+ function getHookHandler(pluginHook) {
348
+ return isFunction(pluginHook) ? pluginHook : pluginHook.handler;
349
+ }
350
+ __name(getHookHandler, "getHookHandler");
351
+ function isHookExternal(hook) {
352
+ return hook.startsWith("vite:") || hook.startsWith("esbuild:") || hook.startsWith("rolldown:") || hook.startsWith("rollup:") || hook.startsWith("webpack:") || hook.startsWith("rspack:") || hook.startsWith("farm:");
353
+ }
354
+ __name(isHookExternal, "isHookExternal");
355
+ function checkDedupe(plugin, plugins2) {
356
+ return plugins2.some((p) => p.dedupe !== false && (isFunction(p.dedupe) && p.dedupe(plugin) || p.name === plugin.name));
357
+ }
358
+ __name(checkDedupe, "checkDedupe");
359
+ function addPluginHook(context, plugin, pluginHook, hooksList) {
360
+ if (!checkDedupe(plugin, hooksList.map((hook) => hook.plugin))) {
361
+ hooksList.push(isFunction(pluginHook) ? {
362
+ plugin,
363
+ handler: getHookHandler(pluginHook).bind(context)
364
+ } : {
365
+ plugin,
366
+ ...pluginHook,
367
+ handler: getHookHandler(pluginHook).bind(context)
368
+ });
369
+ }
370
+ }
371
+ __name(addPluginHook, "addPluginHook");
372
+ async function writeFile2(log, filepath, content, skipFormat = false) {
373
+ try {
374
+ if (skipFormat) {
375
+ await writeFile(filepath, content);
376
+ } else {
377
+ const config = await resolveConfig(filepath);
378
+ const formatted = await format(content, {
379
+ ...config ?? {},
380
+ filepath
381
+ });
382
+ await writeFile(filepath, formatted || "");
383
+ }
384
+ } catch (error) {
385
+ log(LogLevelLabel.ERROR, `Failed to write file ${filepath} to disk
386
+ ${error?.message ? error.message : ""}`);
387
+ }
388
+ }
389
+ __name(writeFile2, "writeFile");
390
+ var DEFAULT_ENVIRONMENT = "default";
391
+ function createEnvironment(name, userConfig) {
392
+ return defu4(userConfig.environments?.[name] ?? {}, {
393
+ name,
394
+ title: userConfig.title || titleCase(userConfig.name),
395
+ ssr: false,
396
+ mainFields: userConfig.build?.platform === "browser" ? [
397
+ "browser",
398
+ "module",
399
+ "jsnext:main",
400
+ "jsnext"
401
+ ] : [
402
+ "module",
403
+ "jsnext:main",
404
+ "jsnext"
405
+ ],
406
+ extensions: [
407
+ ".mjs",
408
+ ".js",
409
+ ".mts",
410
+ ".ts",
411
+ ".jsx",
412
+ ".tsx",
413
+ ".json"
414
+ ],
415
+ consumer: userConfig.build?.platform === "browser" ? "client" : "server",
416
+ preview: userConfig.build?.platform === "browser" ? {
417
+ port: 5173,
418
+ open: true,
419
+ strictPort: false,
420
+ // https: false,
421
+ host: "localhost",
422
+ allowedHosts: [
423
+ "."
424
+ ],
425
+ cors: true,
426
+ headers: {}
427
+ } : void 0
428
+ }, userConfig);
429
+ }
430
+ __name(createEnvironment, "createEnvironment");
431
+ function createDefaultEnvironment(userConfig) {
432
+ return createEnvironment(DEFAULT_ENVIRONMENT, userConfig);
433
+ }
434
+ __name(createDefaultEnvironment, "createDefaultEnvironment");
435
+ function resolveEntryInputFile(context, typeDefinition) {
436
+ return replacePath(typeDefinition.file, joinPaths(context.workspaceConfig.workspaceRoot, context.config.projectRoot));
437
+ }
438
+ __name(resolveEntryInputFile, "resolveEntryInputFile");
439
+ function resolveEntryInput(context, typeDefinition) {
440
+ return {
441
+ file: resolveEntryInputFile(context, typeDefinition),
442
+ name: typeDefinition.name
443
+ };
444
+ }
445
+ __name(resolveEntryInput, "resolveEntryInput");
446
+ function resolveEntryOutput(context, typeDefinition) {
447
+ return joinPaths(replacePath(replacePath(replacePath(replacePath(typeDefinition.file, joinPaths(context.workspaceConfig.workspaceRoot, context.config.sourceRoot)), joinPaths(context.workspaceConfig.workspaceRoot, context.config.projectRoot)), context.config.sourceRoot), context.config.projectRoot)).replace(`.${findFileExtensionSafe(typeDefinition.file)}`, "");
448
+ }
449
+ __name(resolveEntryOutput, "resolveEntryOutput");
450
+ function resolveEntry(context, typeDefinition) {
451
+ const input = resolveEntryInput(context, typeDefinition);
452
+ return {
453
+ ...input,
454
+ input,
455
+ output: resolveEntryOutput(context, typeDefinition)
456
+ };
457
+ }
458
+ __name(resolveEntry, "resolveEntry");
459
+ function resolveEntriesSync(context, typeDefinitions) {
460
+ return typeDefinitions.map((typeDefinition) => {
461
+ const parsed = parseTypeDefinition(typeDefinition);
462
+ return listFilesSync(appendPath(parsed.file, context.config.projectRoot)).map((file) => resolveEntry(context, {
463
+ file,
464
+ name: parsed.name
465
+ }));
466
+ }).flat().filter(Boolean);
467
+ }
468
+ __name(resolveEntriesSync, "resolveEntriesSync");
469
+ function getUniqueEntries(entries = []) {
470
+ return getUniqueBy(toArray(entries), (item) => isSetString(item) ? item : murmurhash(item ?? {}, {
471
+ maxLength: 24
472
+ }));
473
+ }
474
+ __name(getUniqueEntries, "getUniqueEntries");
475
+ var createLog = /* @__PURE__ */ __name((name, options = {}) => {
476
+ const logLevel = options.logLevel === null ? LogLevelLabel.SILENT : options.logLevel || LogLevelLabel.INFO;
477
+ if (logLevel === LogLevelLabel.SILENT) {
478
+ return noop;
479
+ }
480
+ if (options.customLogger) {
481
+ return options.customLogger;
482
+ }
483
+ return (type, ...args) => getLogFn(getLogLevel(type), {
484
+ ...options,
485
+ logLevel
486
+ })(`${chalk5.bold.hex(getColor("brand", options))(`powerlines${name ? `:${name}` : ""}${options.name ? ` ${chalk5.gray("> ")}${options.name}` : ""} ${chalk5.gray("> ")}`)}${args.join(" ")} `.trim());
487
+ }, "createLog");
488
+ var BADGE_COLORS = [
489
+ "#00A0DD",
490
+ "#6FCE4E",
491
+ "#FBBF24",
492
+ "#F43F5E",
493
+ "#3B82F6",
494
+ "#A855F7",
495
+ "#469592",
496
+ "#288EDF",
497
+ "#10B981",
498
+ "#EF4444",
499
+ "#F0EC56",
500
+ "#F472B6",
501
+ "#22D3EE",
502
+ "#EAB308",
503
+ "#84CC16",
504
+ "#F87171",
505
+ "#0EA5E9",
506
+ "#D946EF",
507
+ "#FACC15",
508
+ "#34D399"
509
+ ];
510
+ var extendLog = /* @__PURE__ */ __name((logFn, name) => {
511
+ return (type, ...args) => logFn(type, ` ${chalk5.inverse.hex(BADGE_COLORS[name.split("").map((char) => char.charCodeAt(0)).reduce((ret, charCode) => ret + charCode, 0) % BADGE_COLORS.length] || BADGE_COLORS[0])(` ${titleCase(name)} `)} ${args.join(" ")} `);
512
+ }, "extendLog");
513
+ function resolveOptions(options) {
514
+ return defu4(options, {
515
+ interopDefault: true,
516
+ fsCache: options.mode !== "development" ? joinPaths(options.cacheDir, "jiti") : false,
517
+ moduleCache: options.mode !== "development"
518
+ });
519
+ }
520
+ __name(resolveOptions, "resolveOptions");
521
+ function createPluginResolver(options) {
522
+ return createJiti(joinPaths(options.workspaceRoot, options.projectRoot), resolveOptions({
523
+ ...options
524
+ }));
525
+ }
526
+ __name(createPluginResolver, "createPluginResolver");
527
+ function createResolver(options) {
528
+ const baseResolver = createJiti(joinPaths(options.workspaceRoot, options.projectRoot), resolveOptions(options));
529
+ baseResolver.plugin = createPluginResolver(options);
530
+ return baseResolver;
531
+ }
532
+ __name(createResolver, "createResolver");
533
+ var VirtualFileSystem = class {
534
+ static {
535
+ __name(this, "VirtualFileSystem");
536
+ }
537
+ /**
538
+ * The internal map of virtual files.
539
+ */
540
+ #builtinIdMap = /* @__PURE__ */ new Map();
541
+ /**
542
+ * A map of virtual file paths to their underlying file content.
543
+ */
544
+ #cachedFS = /* @__PURE__ */ new Map();
545
+ /**
546
+ * A map of virtual file paths to their underlying file content.
547
+ */
548
+ #cachedResolver = /* @__PURE__ */ new Map();
549
+ /**
550
+ * The internal map of virtual files.
551
+ */
552
+ #virtualFS = new Volume();
553
+ /**
554
+ * The physical file system.
555
+ */
556
+ #fs = cloneFS(fs);
557
+ /**
558
+ * The unified volume that combines the virtual file system with the real file system.
559
+ *
560
+ * @remarks
561
+ * This volume allows for seamless access to both virtual and real files.
562
+ */
563
+ #unifiedFS = new Union();
564
+ /**
565
+ * Indicator specifying if the file system module is patched
566
+ */
567
+ #isPatched = false;
568
+ /**
569
+ * Function to revert require patch
570
+ */
571
+ #revert;
572
+ /**
573
+ * The context of the virtual file system.
574
+ */
575
+ #context;
576
+ /**
577
+ * The file system's logging function.
578
+ */
579
+ #log;
580
+ /**
581
+ * Exposes the internal VFS map for advanced usage.
582
+ */
583
+ get [__VFS_CACHE__]() {
584
+ return this.#cachedFS;
585
+ }
586
+ /**
587
+ * Exposes the internal VFS resolver cache for advanced usage.
588
+ */
589
+ get [__VFS_RESOLVER__]() {
590
+ return this.#cachedResolver;
591
+ }
592
+ /**
593
+ * Exposes the internal VFS map for advanced usage.
594
+ */
595
+ get [__VFS_VIRTUAL__]() {
596
+ return this.#virtualFS;
597
+ }
598
+ /**
599
+ * Exposes the internal UFS map for advanced usage.
600
+ */
601
+ get [__VFS_UNIFIED__]() {
602
+ return this.#unifiedFS;
603
+ }
604
+ /**
605
+ * Creates a new instance of the VirtualFileSystem.
606
+ *
607
+ * @param context - The context of the virtual file system, typically containing options and logging functions.
608
+ * @param serialized - A map of files/file contents to populate in cache
609
+ */
610
+ constructor(context, serialized) {
611
+ this.#context = context;
612
+ this.#cachedFS = /* @__PURE__ */ new Map();
613
+ this.#builtinIdMap = new Map(Object.entries(serialized?.builtinIdMap ?? {}));
614
+ if (!this.#fs.existsSync(this.#context.dataPath)) {
615
+ this.#fs.mkdirSync(this.#context.dataPath, {
616
+ recursive: true
617
+ });
618
+ }
619
+ if (!this.#fs.existsSync(this.#context.cachePath)) {
620
+ this.#fs.mkdirSync(this.#context.cachePath, {
621
+ recursive: true
622
+ });
623
+ }
624
+ if (!this.#fs.existsSync(joinPaths(this.#context.workspaceConfig.workspaceRoot, this.#context.config.output.outputPath))) {
625
+ this.#fs.mkdirSync(joinPaths(this.#context.workspaceConfig.workspaceRoot, this.#context.config.output.outputPath), {
626
+ recursive: true
627
+ });
628
+ }
629
+ this.#unifiedFS = this.#unifiedFS.use(this.#fs);
630
+ if (this.#context.config.output.mode !== "fs") {
631
+ if (serialized?.virtualFiles && Object.keys(serialized.virtualFiles).length > 0) {
632
+ this.#virtualFS = Volume.fromJSON(serialized.virtualFiles);
633
+ }
634
+ if (!this.#virtualFS.existsSync(this.#context.artifactsPath)) {
635
+ this.#virtualFS.mkdirSync(this.#context.artifactsPath, {
636
+ recursive: true
637
+ });
638
+ }
639
+ if (!this.#virtualFS.existsSync(this.#context.builtinsPath)) {
640
+ this.#virtualFS.mkdirSync(this.#context.builtinsPath, {
641
+ recursive: true
642
+ });
643
+ }
644
+ if (!this.#virtualFS.existsSync(this.#context.entryPath)) {
645
+ this.#virtualFS.mkdirSync(this.#context.entryPath, {
646
+ recursive: true
647
+ });
648
+ }
649
+ if (!this.#virtualFS.existsSync(this.#context.dtsPath)) {
650
+ this.#virtualFS.mkdirSync(this.#context.dtsPath, {
651
+ recursive: true
652
+ });
653
+ }
654
+ this.#unifiedFS = this.#unifiedFS.use(this.#virtualFS);
655
+ } else if (this.#context.config.projectType === "application") {
656
+ if (!this.#fs.existsSync(this.#context.artifactsPath)) {
657
+ this.#fs.mkdirSync(this.#context.artifactsPath, {
658
+ recursive: true
659
+ });
660
+ }
661
+ if (!this.#fs.existsSync(this.#context.builtinsPath)) {
662
+ this.#fs.mkdirSync(this.#context.builtinsPath, {
663
+ recursive: true
664
+ });
665
+ }
666
+ if (!this.#fs.existsSync(this.#context.entryPath)) {
667
+ this.#fs.mkdirSync(this.#context.entryPath, {
668
+ recursive: true
669
+ });
670
+ }
671
+ if (!this.#fs.existsSync(this.#context.dtsPath)) {
672
+ this.#fs.mkdirSync(this.#context.dtsPath, {
673
+ recursive: true
674
+ });
675
+ }
676
+ }
677
+ this.#log = extendLog(this.#context.log, "virtual-file-system");
678
+ }
679
+ [__VFS_INIT__]() {
680
+ if (!this.#isPatched && this.#context.config.output.mode !== "fs") {
681
+ this.#revert = patchFS(fs, this);
682
+ this.#isPatched = true;
683
+ }
684
+ }
685
+ [__VFS_REVERT__]() {
686
+ if (this.#isPatched && this.#context.config.output.mode !== "fs") {
687
+ if (!this.#revert) {
688
+ throw new Error("Attempting to revert File System patch prior to calling `__init__` function");
689
+ }
690
+ this.#revert?.();
691
+ this.#isPatched = false;
692
+ }
693
+ }
694
+ /**
695
+ * Returns a Map of all runtime file IDs and their corresponding paths in the virtual file system.
696
+ *
697
+ * @returns A Map where the keys are runtime file IDs (strings) and the values are their corresponding paths (strings).
698
+ */
699
+ get builtinIdMap() {
700
+ return this.#builtinIdMap;
701
+ }
702
+ /**
703
+ * Lists all runtime IDs in the virtual file system.
704
+ *
705
+ * @returns An array of formatted runtime IDs.
706
+ */
707
+ get runtimeIds() {
708
+ return Array.from(this.builtinIdMap.keys()).map((id) => this.formatRuntimeId(id));
709
+ }
710
+ /**
711
+ * Checks if a given path or ID corresponds to a runtime file.
712
+ *
713
+ * @param pathOrId - The path or ID to check.
714
+ * @param options - Options for resolving the path, such as paths to check.
715
+ * @returns `true` if the path or ID corresponds to a runtime file, otherwise `false`.
716
+ */
717
+ isBuiltinFile(pathOrId, options) {
718
+ return !!this.builtinIdMap.values().find((path2) => path2 === this.resolvePath(pathOrId, {
719
+ ...options,
720
+ type: "file"
721
+ }));
722
+ }
723
+ /**
724
+ * Checks if a provided string is a valid runtime ID (does not need to already be created in the file system).
725
+ *
726
+ * @param id - The ID to check.
727
+ * @returns Whether the ID is a valid runtime ID.
728
+ */
729
+ isValidBuiltinId(id) {
730
+ return id.startsWith(`${this.#context.config.builtinPrefix}:`);
731
+ }
732
+ /**
733
+ * Check if a path or ID corresponds to a virtual file.
734
+ *
735
+ * @param pathOrId - The path or ID to check.
736
+ * @param options - Options for resolving the path, such as paths to check.
737
+ * @returns Whether the path or ID corresponds to a virtual file.
738
+ */
739
+ isVirtualFile(pathOrId, options = {}) {
740
+ if (!pathOrId) {
741
+ return false;
742
+ }
743
+ const resolvedPath = this.resolvePath(pathOrId, {
744
+ ...options,
745
+ type: "file"
746
+ });
747
+ if (!resolvedPath) {
748
+ return false;
749
+ }
750
+ if (this.builtinIdMap.values().find((path2) => path2 === resolvedPath)) {
751
+ return true;
752
+ }
753
+ return this.#virtualFS.existsSync(resolvedPath);
754
+ }
755
+ /**
756
+ * Check if a path exists within one of the directories specified in the tsconfig.json's `path` field.
757
+ *
758
+ * @see https://www.typescriptlang.org/tsconfig#paths
759
+ *
760
+ * @param pathOrId - The path or ID to check.
761
+ * @returns Whether the path or ID corresponds to a virtual file.
762
+ */
763
+ isTsconfigPath(pathOrId) {
764
+ return !!this.#context.tsconfig.options.paths && Object.keys(this.#context.tsconfig.options.paths).some((path2) => pathOrId.startsWith(path2.replaceAll("*", "")));
765
+ }
766
+ /**
767
+ * Checks if a given ID corresponds to a runtime file path.
768
+ *
769
+ * @param id - The unique identifier for the runtime file.
770
+ * @param pathOrId - The path or ID to check.
771
+ * @returns `true` if the ID corresponds to the path or ID of a runtime file, otherwise `false`.
772
+ */
773
+ isMatchingBuiltinId(id, pathOrId) {
774
+ const resolvedPath = this.resolvePath(pathOrId);
775
+ const resolvedId = this.resolveId(pathOrId);
776
+ return !!(this.isBuiltinFile(pathOrId) && (resolvedPath && (resolvedPath === this.builtinIdMap.get(id) || resolvedPath === this.builtinIdMap.get(this.formatRuntimeId(id))) || resolvedId && (resolvedId === this.builtinIdMap.get(id) || resolvedId === this.builtinIdMap.get(this.formatRuntimeId(id)))));
777
+ }
778
+ /**
779
+ * Lists all runtime files in the virtual file system.
780
+ *
781
+ * @returns A promise that resolves to an array of runtime files.
782
+ */
783
+ async listBuiltinFiles() {
784
+ const runtimeFiles = [];
785
+ for (const [id, path2] of this.builtinIdMap.entries()) {
786
+ const contents = await this.readFile(path2);
787
+ if (contents) {
788
+ runtimeFiles.push({
789
+ id: this.formatRuntimeId(id),
790
+ path: path2,
791
+ contents
792
+ });
793
+ }
794
+ }
795
+ return runtimeFiles;
796
+ }
797
+ /**
798
+ * Lists files in a given path.
799
+ *
800
+ * @param path - The path to list files from.
801
+ * @param options - Options for listing files, such as encoding and recursion.
802
+ * @returns An array of file names in the specified path.
803
+ */
804
+ readdirSync(path2, options = "utf8") {
805
+ return this.resolveFS(path2).readdirSync(toFilePath(path2), options);
806
+ }
807
+ /**
808
+ * Removes a file in the virtual file system (VFS).
809
+ *
810
+ * @param path - The path to create the directory at.
811
+ */
812
+ unlinkSync(path2, options) {
813
+ const formattedPath = toFilePath(path2);
814
+ if (!this.fileExistsSync(path2)) {
815
+ return;
816
+ }
817
+ this.#log(LogLevelLabel.TRACE, `Synchronously removing file: ${formattedPath}`);
818
+ this.resolveFS(path2, options).unlinkSync(formattedPath);
819
+ this.#cachedFS.delete(formattedPath);
820
+ this.clearResolverCache(formattedPath);
821
+ }
822
+ /**
823
+ * Removes a file in the virtual file system (VFS).
824
+ *
825
+ * @param path - The path to create the directory at.
826
+ */
827
+ async unlink(path2, options) {
828
+ const formattedPath = toFilePath(path2);
829
+ if (!this.fileExistsSync(path2)) {
830
+ return;
831
+ }
832
+ this.#log(LogLevelLabel.TRACE, `Removing file: ${formattedPath}`);
833
+ if (isFunction(this.resolveFS(path2, options).promises.unlink)) {
834
+ await this.resolveFS(path2, options).promises.unlink(formattedPath);
835
+ this.#cachedFS.delete(formattedPath);
836
+ this.clearResolverCache(formattedPath);
837
+ } else {
838
+ this.unlinkSync(formattedPath, options);
839
+ }
840
+ }
841
+ /**
842
+ * Removes a directory in the virtual file system (VFS).
843
+ *
844
+ * @param path - The path to create the directory at.
845
+ * @param options - Options for creating the directory.
846
+ */
847
+ rmdirSync(path2, options = {}) {
848
+ const formattedPath = toFilePath(path2);
849
+ if (!this.directoryExistsSync(path2)) {
850
+ return;
851
+ }
852
+ this.#log(LogLevelLabel.TRACE, `Synchronously removing directory: ${formattedPath}`);
853
+ this.resolveFS(path2, options).rmdirSync(formattedPath, defu4(options, {
854
+ recursive: true
855
+ }));
856
+ this.#cachedFS.delete(formattedPath);
857
+ this.clearResolverCache(formattedPath);
858
+ }
859
+ /**
860
+ * Removes a directory in the virtual file system (VFS).
861
+ *
862
+ * @param path - The path to create the directory at.
863
+ * @param options - Options for creating the directory.
864
+ * @returns A promise that resolves to the path of the created directory, or undefined if the directory could not be created.
865
+ */
866
+ async rmdir(path2, options = {}) {
867
+ const formattedPath = toFilePath(path2);
868
+ if (!this.directoryExistsSync(path2)) {
869
+ return;
870
+ }
871
+ this.#log(LogLevelLabel.TRACE, `Removing directory: ${formattedPath}`);
872
+ if (isFunction(this.resolveFS(path2, options).promises.rm)) {
873
+ await this.resolveFS(path2, options).promises.rm(formattedPath, defu4(options, {
874
+ force: true,
875
+ recursive: true
876
+ }));
877
+ this.#cachedFS.delete(formattedPath);
878
+ this.clearResolverCache(formattedPath);
879
+ } else {
880
+ this.rmdirSync(formattedPath, defu4(options ?? {}, {
881
+ force: true,
882
+ recursive: true
883
+ }));
884
+ }
885
+ }
886
+ /**
887
+ * Removes a file in the virtual file system (VFS).
888
+ *
889
+ * @param path - The path to the file to remove.
890
+ * @param options - Options for removing the file.
891
+ * @returns A promise that resolves when the file is removed.
892
+ */
893
+ async rm(path2, options = {}) {
894
+ this.#log(LogLevelLabel.TRACE, `Removing: ${toFilePath(path2)}`);
895
+ if (this.directoryExistsSync(path2)) {
896
+ return this.rmdir(path2, options);
897
+ }
898
+ return this.unlink(path2, options);
899
+ }
900
+ /**
901
+ * Creates a directory in the virtual file system (VFS).
902
+ *
903
+ * @param path - The path to create the directory at.
904
+ * @param options - Options for creating the directory.
905
+ * @returns A promise that resolves to the path of the created directory, or undefined if the directory could not be created.
906
+ */
907
+ mkdirSync(path2, options = {}) {
908
+ const filePath = toFilePath(path2);
909
+ this.clearResolverCache(filePath);
910
+ return this.resolveFS(filePath, options).mkdirSync(filePath, defu4(options ?? {}, {
911
+ recursive: true
912
+ }));
913
+ }
914
+ /**
915
+ * Creates a directory in the virtual file system (VFS).
916
+ *
917
+ * @param path - The path to create the directory at.
918
+ * @param options - Options for creating the directory.
919
+ * @returns A promise that resolves to the path of the created directory, or undefined if the directory could not be created.
920
+ */
921
+ async mkdir(path2, options = {}) {
922
+ let result;
923
+ const filePath = toFilePath(path2);
924
+ if (isFunction(this.resolveFS(filePath, options).promises.mkdir)) {
925
+ result = await this.resolveFS(filePath, options).promises.mkdir(filePath, defu4(options ?? {}, {
926
+ recursive: true
927
+ }));
928
+ } else {
929
+ result = this.resolveFS(filePath, options).mkdirSync(filePath, defu4(options ?? {}, {
930
+ recursive: true
931
+ }));
932
+ }
933
+ this.clearResolverCache(filePath);
934
+ return result;
935
+ }
936
+ /**
937
+ * Lists files in a given path.
938
+ *
939
+ * @param path - The path to list files from.
940
+ * @param options - Options for listing files, such as encoding and recursion.
941
+ * @returns An array of file names in the specified path.
942
+ */
943
+ async readdir(path2, options = "utf8") {
944
+ return this.resolveFS(path2).promises.readdir(toFilePath(path2), options);
945
+ }
946
+ /**
947
+ * Asynchronously reads a file from the virtual file system (VFS).
948
+ *
949
+ * @param pathOrId - The path or ID of the file to read.
950
+ * @returns A promise that resolves to the contents of the file as a string, or undefined if the file does not exist.
951
+ */
952
+ async readFile(pathOrId, options = "utf8") {
953
+ if (!pathOrId) {
954
+ return void 0;
955
+ }
956
+ const filePath = this.resolvePath(toFilePath(pathOrId), {
957
+ type: "file"
958
+ });
959
+ if (filePath) {
960
+ if (this.#cachedFS.has(filePath)) {
961
+ return this.#cachedFS.get(filePath);
962
+ }
963
+ let result;
964
+ if (isFunction(this.resolveFS(filePath).promises.readFile)) {
965
+ result = (await this.resolveFS(filePath).promises.readFile(filePath, options))?.toString("utf8");
966
+ } else {
967
+ result = this.resolveFS(filePath).readFileSync(filePath, options);
968
+ }
969
+ const content = isBuffer(result) ? bufferToString(result) : result;
970
+ this.#cachedFS.set(filePath, content);
971
+ return content;
972
+ }
973
+ return void 0;
974
+ }
975
+ /**
976
+ * Synchronously reads a file from the virtual file system (VFS).
977
+ *
978
+ * @param pathOrId - The path or ID of the file to read.
979
+ * @returns The contents of the file as a string, or undefined if the file does not exist.
980
+ */
981
+ readFileSync(pathOrId, options = "utf8") {
982
+ if (!pathOrId) {
983
+ return void 0;
984
+ }
985
+ const filePath = this.resolvePath(toFilePath(pathOrId), {
986
+ type: "file"
987
+ });
988
+ if (filePath) {
989
+ if (this.#cachedFS.has(filePath)) {
990
+ return this.#cachedFS.get(filePath);
991
+ }
992
+ const result = this.resolveFS(filePath).readFileSync(filePath, options);
993
+ const content = isBuffer(result) ? bufferToString(result) : result;
994
+ this.#cachedFS.set(filePath, content);
995
+ return content;
996
+ }
997
+ return void 0;
998
+ }
999
+ /**
1000
+ * Writes a file to the virtual file system (VFS).
1001
+ *
1002
+ * @param file - The path to the file.
1003
+ * @param data - The contents of the file.
1004
+ * @param options - Optional parameters for writing the file.
1005
+ * @returns A promise that resolves when the file is written.
1006
+ */
1007
+ async writeFile(file, data = "", options = "utf8") {
1008
+ const absolutePath = this.formatAbsoluteFilePath(toFilePath(file));
1009
+ if (!this.directoryExistsSync(findFilePath(absolutePath))) {
1010
+ await this.mkdir(findFilePath(absolutePath), options);
1011
+ }
1012
+ this.#log(LogLevelLabel.TRACE, `Writing ${absolutePath} file to the ${this.resolveOutputMode(absolutePath, options) === "fs" ? "" : "virtual "}file system (size: ${prettyBytes(new Blob(toArray(data)).size)})`);
1013
+ this.#cachedFS.set(absolutePath, data.toString());
1014
+ this.clearResolverCache(absolutePath);
1015
+ const ifs = this.resolveFS(absolutePath, options);
1016
+ if (isFunction(ifs.promises.writeFile)) {
1017
+ return ifs.promises.writeFile(absolutePath, data, omit(options, [
1018
+ "mode"
1019
+ ]));
1020
+ }
1021
+ return ifs.writeFileSync(absolutePath, data, omit(options, [
1022
+ "mode"
1023
+ ]));
1024
+ }
1025
+ /**
1026
+ * Synchronously writes a file to the virtual file system (VFS).
1027
+ *
1028
+ * @param file - The file to write.
1029
+ * @param data - The contents of the file.
1030
+ * @param options - Optional parameters for writing the file.
1031
+ */
1032
+ writeFileSync(file, data = "", options = "utf8") {
1033
+ const absolutePath = this.formatAbsoluteFilePath(toFilePath(file));
1034
+ if (!this.directoryExistsSync(findFilePath(absolutePath))) {
1035
+ this.mkdirSync(findFilePath(absolutePath));
1036
+ }
1037
+ this.#log(LogLevelLabel.TRACE, `Writing ${absolutePath} file to the ${this.resolveOutputMode(absolutePath, options) === "fs" ? "" : "virtual "} (size: ${prettyBytes(new Blob(toArray(data)).size)})`);
1038
+ this.#cachedFS.set(absolutePath, data.toString());
1039
+ this.clearResolverCache(absolutePath);
1040
+ const writeStream = this.resolveFS(absolutePath, omit(options, [
1041
+ "mode"
1042
+ ])).createWriteStream(absolutePath);
1043
+ try {
1044
+ writeStream.write(data);
1045
+ } finally {
1046
+ writeStream.close();
1047
+ }
1048
+ }
1049
+ /**
1050
+ * Writes a runtime file to the virtual file system (VFS).
1051
+ *
1052
+ * @param id - The unique identifier for the runtime file.
1053
+ * @param path - The path to the runtime file.
1054
+ * @param contents - The contents of the runtime file.
1055
+ * @param options - Optional parameters for writing the runtime file.
1056
+ * @returns A promise that resolves when the file is written.
1057
+ */
1058
+ async writeBuiltinFile(id, path2, contents, options = {}) {
1059
+ const formattedId = this.formatRuntimeId(id);
1060
+ const absolutePath = this.formatAbsoluteFilePath(toFilePath(path2));
1061
+ this.builtinIdMap.set(formattedId, absolutePath);
1062
+ let data = contents;
1063
+ if (!options.skipFormat) {
1064
+ data = await format(contents, {
1065
+ absolutePath,
1066
+ ...await resolveConfig(absolutePath)
1067
+ });
1068
+ }
1069
+ const _options = defu4(isSetString(options) ? {} : options ?? {}, {
1070
+ encoding: isSetString(options) ? options : "utf8",
1071
+ mode: "virtual"
1072
+ });
1073
+ this.#log(LogLevelLabel.DEBUG, `Writing runtime file ${absolutePath} (size: ${prettyBytes(new Blob(toArray(data)).size)}) to ${this.resolveOutputMode(absolutePath, _options) === "fs" ? "disk" : "memory"}`);
1074
+ return this.writeFile(absolutePath, data, _options);
1075
+ }
1076
+ /**
1077
+ * Adds an entry file to the virtual file system.
1078
+ *
1079
+ * @param name - The file name or absolute path of the entry module.
1080
+ * @param contents - The contents of the entry file.
1081
+ * @param options - Optional parameters for writing the entry file.
1082
+ */
1083
+ async writeEntryFile(name, contents, options = {}) {
1084
+ const absolutePath = this.formatAbsoluteFilePath(isAbsolutePath(toFilePath(name)) ? toFilePath(name) : toFilePath(joinPaths(this.#context.entryPath, name)));
1085
+ let data = contents;
1086
+ if (!options.skipFormat) {
1087
+ data = await format(contents, {
1088
+ absolutePath,
1089
+ ...await resolveConfig(absolutePath)
1090
+ });
1091
+ }
1092
+ const _options = defu4(isSetString(options) ? {} : options ?? {}, {
1093
+ encoding: isSetString(options) ? options : "utf8",
1094
+ mode: "virtual"
1095
+ });
1096
+ this.#log(LogLevelLabel.DEBUG, `Writing entry file ${absolutePath} (size: ${prettyBytes(new Blob(toArray(data)).size)}) to ${this.resolveOutputMode(absolutePath, _options) === "fs" ? "disk" : "virtual memory"}`);
1097
+ return this.writeFile(absolutePath, data, _options);
1098
+ }
1099
+ /**
1100
+ * Writes a file to disk from the physical file system (on disk).
1101
+ *
1102
+ * @param path - The path to the file to write.
1103
+ * @param contents - The contents of the file to write.
1104
+ * @param options - Optional parameters for writing the file.
1105
+ * @returns A promise that resolves when the file is written.
1106
+ */
1107
+ async writeFileToDisk(path2, contents, options = {}) {
1108
+ const absolutePath = this.formatAbsoluteFilePath(toFilePath(path2));
1109
+ let data = contents;
1110
+ if (!options.skipFormat) {
1111
+ const resolvedConfig = await resolveConfig(absolutePath);
1112
+ if (resolvedConfig) {
1113
+ data = await format(contents, {
1114
+ absolutePath,
1115
+ ...resolvedConfig
1116
+ });
1117
+ }
1118
+ }
1119
+ return this.writeFile(absolutePath, data, defu4({
1120
+ mode: "fs"
1121
+ }, isSetString(options) ? {} : options ?? {}, {
1122
+ encoding: isSetString(options) ? options : "utf8"
1123
+ }));
1124
+ }
1125
+ /**
1126
+ * Synchronously checks if a file exists in the virtual file system (VFS).
1127
+ *
1128
+ * @param pathOrId - The path or ID of the file to check.
1129
+ * @returns `true` if the file exists, otherwise `false`.
1130
+ */
1131
+ existsSync(pathOrId) {
1132
+ return this.pathExistsSync(this.resolvePath(toFilePath(pathOrId)) || toFilePath(pathOrId));
1133
+ }
1134
+ /**
1135
+ * Checks if a file exists in the virtual file system (VFS).
1136
+ *
1137
+ * @remarks
1138
+ * This is a base method used by {@link existsSync} - it does not try to resolve the path prior to checking if it exists or not.
1139
+ *
1140
+ * @param path - The path of the file to check.
1141
+ * @returns `true` if the file exists, otherwise `false`.
1142
+ */
1143
+ fileExistsSync(path2) {
1144
+ const formattedPath = this.formatAbsoluteFilePath(toFilePath(path2));
1145
+ return this.isValidBuiltinId(formattedPath) || this.#virtualFS.existsSync(formattedPath) && this.#virtualFS.lstatSync(formattedPath).isFile() || this.#fs.existsSync(formattedPath) && this.#fs.lstatSync(formattedPath).isFile() || this.resolveFS(path2).existsSync(formattedPath) && this.resolveFS(path2).lstatSync(formattedPath).isFile();
1146
+ }
1147
+ /**
1148
+ * Checks if a directory exists in the virtual file system (VFS).
1149
+ *
1150
+ * @param path - The path of the directory to check.
1151
+ * @returns `true` if the directory exists, otherwise `false`.
1152
+ */
1153
+ directoryExistsSync(path2) {
1154
+ const formattedPath = this.formatAbsoluteFilePath(toFilePath(path2));
1155
+ return this.#virtualFS.existsSync(formattedPath) && this.#virtualFS.lstatSync(formattedPath).isDirectory() || this.#fs.existsSync(formattedPath) && this.#fs.lstatSync(formattedPath).isDirectory() || this.resolveFS(path2).existsSync(formattedPath) && this.resolveFS(path2).lstatSync(formattedPath).isDirectory();
1156
+ }
1157
+ /**
1158
+ * Checks if a path exists in the virtual file system (VFS).
1159
+ *
1160
+ * @param path - The path to check.
1161
+ * @returns `true` if the path exists, otherwise `false`.
1162
+ */
1163
+ pathExistsSync(path2) {
1164
+ const formattedPath = this.formatAbsoluteFilePath(toFilePath(path2));
1165
+ return this.isValidBuiltinId(formattedPath) || this.#virtualFS.existsSync(formattedPath) || this.#fs.existsSync(formattedPath) || this.resolveFS(path2).existsSync(formattedPath);
1166
+ }
1167
+ /**
1168
+ * Retrieves the status of a file in the virtual file system (VFS).
1169
+ *
1170
+ * @param pathOrId - The path or ID of the file to retrieve status for.
1171
+ * @returns A promise that resolves to the file's status information, or false if the file does not exist.
1172
+ */
1173
+ async stat(pathOrId, options) {
1174
+ return this.resolveFS(pathOrId).promises.stat(this.resolvePath(toFilePath(pathOrId)) || toFilePath(pathOrId), options);
1175
+ }
1176
+ /**
1177
+ * Synchronously retrieves the status of a file in the virtual file system (VFS).
1178
+ *
1179
+ * @param pathOrId - The path or ID of the file to retrieve status for.
1180
+ * @returns The file's status information, or false if the file does not exist.
1181
+ */
1182
+ statSync(pathOrId) {
1183
+ return this.resolveFS(pathOrId).statSync(this.resolvePath(toFilePath(pathOrId)) || toFilePath(pathOrId));
1184
+ }
1185
+ /**
1186
+ * Retrieves the status of a symbolic link in the virtual file system (VFS).
1187
+ *
1188
+ * @param pathOrId - The path or ID of the symbolic link to retrieve status for.
1189
+ * @returns A promise that resolves to the symbolic link's status information, or false if the link does not exist.
1190
+ */
1191
+ async lstat(pathOrId, options) {
1192
+ return this.resolveFS(pathOrId).promises.lstat(this.resolvePath(toFilePath(pathOrId)) || toFilePath(pathOrId), options);
1193
+ }
1194
+ /**
1195
+ * Synchronously retrieves the status of a symbolic link in the virtual file system (VFS).
1196
+ *
1197
+ * @param pathOrId - The path or ID of the symbolic link to retrieve status for.
1198
+ * @returns The symbolic link's status information, or false if the link does not exist.
1199
+ */
1200
+ lstatSync(pathOrId, options) {
1201
+ return this.resolveFS(pathOrId).lstatSync(this.resolvePath(toFilePath(pathOrId)) || toFilePath(pathOrId), options);
1202
+ }
1203
+ /**
1204
+ * Resolves a path or ID to a runtime file id in the virtual file system.
1205
+ *
1206
+ * @param pathOrId - The path or id of the file to resolve.
1207
+ * @returns The resolved id of the runtime file if it exists, otherwise false.
1208
+ */
1209
+ resolveId(pathOrId) {
1210
+ if (this.builtinIdMap.has(this.formatRuntimeId(toFilePath(pathOrId)))) {
1211
+ return this.formatRuntimeId(toFilePath(pathOrId));
1212
+ }
1213
+ const filePath = this.resolvePath(toFilePath(pathOrId));
1214
+ if (filePath) {
1215
+ return this.builtinIdMap.keys().find((id) => this.builtinIdMap.get(id) === filePath) || false;
1216
+ }
1217
+ return false;
1218
+ }
1219
+ /**
1220
+ * Resolves a path based on TypeScript's `tsconfig.json` paths.
1221
+ *
1222
+ * @see https://www.typescriptlang.org/tsconfig#paths
1223
+ *
1224
+ * @param path - The path to check.
1225
+ * @returns The resolved file path if it exists, otherwise undefined.
1226
+ */
1227
+ resolveTsconfigPath(path2) {
1228
+ if (this.#context.tsconfig.options.paths) {
1229
+ for (const tsconfigPathKey of Object.keys(this.#context.tsconfig.options.paths).filter((tsconfigPath) => path2.startsWith(tsconfigPath.replaceAll("*", "")))) {
1230
+ const resolvedPath = this.#context.tsconfig.options.paths[tsconfigPathKey]?.find((tsconfigPath) => this.resolvePathName(joinPaths(this.#context.workspaceConfig.workspaceRoot, tsconfigPath.replaceAll("*", ""), path2.replace(tsconfigPathKey.replaceAll("*", ""), ""))) || this.formatAbsoluteFilePath(tsconfigPath) === this.formatAbsoluteFilePath(path2));
1231
+ if (resolvedPath) {
1232
+ return this.formatAbsoluteFilePath(resolvedPath) === this.formatAbsoluteFilePath(path2) ? this.formatAbsoluteFilePath(resolvedPath) : this.resolvePathName(joinPaths(this.#context.workspaceConfig.workspaceRoot, resolvedPath.replaceAll("*", ""), path2.replace(tsconfigPathKey.replaceAll("*", ""), "")));
1233
+ }
1234
+ }
1235
+ }
1236
+ return false;
1237
+ }
1238
+ /**
1239
+ * Resolves a path based on TypeScript's `tsconfig.json` paths.
1240
+ *
1241
+ * @see https://www.typescriptlang.org/tsconfig#paths
1242
+ *
1243
+ * @param path - The path to check.
1244
+ * @returns The resolved file path if it exists, otherwise undefined.
1245
+ */
1246
+ resolveTsconfigPathPackage(path2) {
1247
+ if (this.#context.tsconfig.options.paths) {
1248
+ const tsconfigPathKeys = Object.keys(this.#context.tsconfig.options.paths).filter((tsconfigPath) => path2.startsWith(tsconfigPath.replaceAll("*", "")));
1249
+ if (tsconfigPathKeys.length > 0 && tsconfigPathKeys[0]) {
1250
+ return tsconfigPathKeys[0].replace(/\/\*$/, "");
1251
+ }
1252
+ }
1253
+ return false;
1254
+ }
1255
+ /**
1256
+ * Resolves a path or ID to its real path in the virtual file system (VFS).
1257
+ *
1258
+ * @param pathOrId - The path or ID to resolve.
1259
+ * @returns The resolved real path if it exists, otherwise undefined.
1260
+ */
1261
+ realpathSync(pathOrId) {
1262
+ const filePath = this.resolvePath(toFilePath(pathOrId));
1263
+ if (!filePath) {
1264
+ throw new Error(`File not found: ${toFilePath(pathOrId)}`);
1265
+ }
1266
+ return filePath;
1267
+ }
1268
+ /**
1269
+ * Resolves a path or ID parameter to a corresponding virtual file path in the virtual file system (VFS).
1270
+ *
1271
+ * @param pathOrId - The path or ID to resolve.
1272
+ * @param options - Optional parameters for resolving the path, such as whether to include the file extension.
1273
+ * @returns The resolved file path if it exists, otherwise undefined.
1274
+ */
1275
+ resolvePath(pathOrId, options = {}) {
1276
+ const formattedPath = toFilePath(pathOrId);
1277
+ const resolverKey = `${formattedPath}${options.withExtension ? "-ext" : ""}${options.paths ? `-${murmurhash(options.paths)}` : ""}${options.type ? `-${options.type}` : ""}`;
1278
+ if (this.#cachedResolver.has(resolverKey)) {
1279
+ return this.#cachedResolver.get(resolverKey);
1280
+ } else if (this.#cachedFS.has(formattedPath)) {
1281
+ return formattedPath;
1282
+ }
1283
+ let result = false;
1284
+ if (this.isValidBuiltinId(formattedPath)) {
1285
+ result = this.builtinIdMap.get(this.formatRuntimeId(formattedPath));
1286
+ } else {
1287
+ result = this.resolvePathName(formattedPath, options);
1288
+ }
1289
+ if (!result) {
1290
+ result = false;
1291
+ } else {
1292
+ result = toFilePath(result);
1293
+ }
1294
+ if (result && options.withExtension === false) {
1295
+ return result.replace(/\.[m|c]?[t|j]sx?$/, "");
1296
+ }
1297
+ this.#cachedResolver.set(resolverKey, result);
1298
+ return result;
1299
+ }
1300
+ /**
1301
+ * Formats a file path by removing the runtime prefix and leading null character.
1302
+ *
1303
+ * @param path - The file path to format.
1304
+ * @returns The formatted file path.
1305
+ */
1306
+ formatFilePath(path2) {
1307
+ if (!isSetString(path2)) {
1308
+ throw new Error(`Invalid path provided. Expected a string or a valid file path.`);
1309
+ }
1310
+ return path2.replace(new RegExp(`^${this.#context.config.builtinPrefix}:`), "").replace(/^\\0/, "");
1311
+ }
1312
+ /**
1313
+ * Converts a relative path to an absolute path based on the workspace and project root.
1314
+ *
1315
+ * @param path - The relative path to convert.
1316
+ * @returns The absolute path.
1317
+ */
1318
+ formatAbsoluteFilePath = /* @__PURE__ */ __name((path2) => {
1319
+ const formattedPath = this.formatFilePath(path2);
1320
+ if (isAbsolutePath(formattedPath) || formattedPath.startsWith(this.#context.workspaceConfig.workspaceRoot)) {
1321
+ return formattedPath;
1322
+ } else if (formattedPath.startsWith(this.#context.config.projectRoot)) {
1323
+ return joinPaths(this.#context.workspaceConfig.workspaceRoot, formattedPath);
1324
+ }
1325
+ return formattedPath;
1326
+ }, "formatAbsoluteFilePath");
1327
+ /**
1328
+ * Formats a runtime ID by removing the file extension and prepending the runtime prefix.
1329
+ *
1330
+ * @param id - The runtime ID to format.
1331
+ * @returns The formatted runtime ID.
1332
+ */
1333
+ formatRuntimeId(id) {
1334
+ return `${this.#context.config.builtinPrefix}:${this.formatFilePath(id).replace(/\.[m|c]?[t|j]sx?$/, "")}`;
1335
+ }
1336
+ /**
1337
+ * Resolves a path or ID parameter to a corresponding virtual file path in the virtual file system (VFS).
1338
+ *
1339
+ * @param pathOrId - The path or ID to resolve.
1340
+ * @returns The resolved file path if it exists, otherwise undefined.
1341
+ */
1342
+ resolvePathName(pathOrId, options = {}) {
1343
+ if (pathOrId.startsWith(`${this.#context.config.builtinPrefix}:`)) {
1344
+ return false;
1345
+ }
1346
+ if (isAbsolutePath(pathOrId)) {
1347
+ if (options.type === "file" ? this.fileExistsSync(pathOrId) : this.pathExistsSync(pathOrId)) {
1348
+ return pathOrId;
1349
+ }
1350
+ const result = checkVariants(pathOrId, this);
1351
+ if (result) {
1352
+ return result;
1353
+ }
1354
+ }
1355
+ for (const path2 of this.resolveParentPaths(pathOrId, options.paths)) {
1356
+ const request = joinPaths(path2, pathOrId);
1357
+ if (options.type === "file" ? this.fileExistsSync(pathOrId) : this.pathExistsSync(pathOrId)) {
1358
+ return request;
1359
+ }
1360
+ const result = checkVariants(request, this);
1361
+ if (result) {
1362
+ return result;
1363
+ }
1364
+ }
1365
+ return false;
1366
+ }
1367
+ resolveParentPaths(request, current = []) {
1368
+ let paths = [
1369
+ this.#context.workspaceConfig.workspaceRoot,
1370
+ joinPaths(this.#context.workspaceConfig.workspaceRoot, this.#context.config.projectRoot)
1371
+ ];
1372
+ if (this.#context.tsconfig.options.paths) {
1373
+ paths = this.#context.tsconfig.options.paths ? Object.keys(this.#context.tsconfig.options.paths).filter((tsconfigPath) => request.startsWith(tsconfigPath.replaceAll("*", ""))).map((tsconfigPath) => this.#context.tsconfig.options.paths?.[tsconfigPath]).flat().reduce((ret, path2) => {
1374
+ if (path2 && !ret.includes(joinPaths(this.#context.workspaceConfig.workspaceRoot, path2))) {
1375
+ ret.push(joinPaths(this.#context.workspaceConfig.workspaceRoot, path2));
1376
+ }
1377
+ return ret;
1378
+ }, paths) : paths;
1379
+ }
1380
+ return paths.reduce((ret, path2) => {
1381
+ if (!ret.includes(path2)) {
1382
+ ret.push(path2);
1383
+ }
1384
+ return ret;
1385
+ }, current.filter(Boolean).map((p) => this.formatAbsoluteFilePath(toFilePath(p))));
1386
+ }
1387
+ /**
1388
+ * Select the file system module to use for the operation based on the path or URL.
1389
+ *
1390
+ * @param pathOrUrl - The path to perform the file system operation on.
1391
+ * @param options - Options for the operation, such as output mode.
1392
+ * @returns The file system module used for the operation.
1393
+ */
1394
+ resolveFS(pathOrUrl, options = {}) {
1395
+ const mode = this.resolveOutputMode(pathOrUrl, options);
1396
+ if (mode === "virtual") {
1397
+ return this.#virtualFS;
1398
+ } else if (mode === "fs") {
1399
+ return this.#fs;
1400
+ }
1401
+ return this.#unifiedFS;
1402
+ }
1403
+ /**
1404
+ * Select the file system module to use for the operation based on the path or URL.
1405
+ *
1406
+ * @param pathOrUrl - The path to perform the file system operation on.
1407
+ * @param options - Options for the operation, such as output mode.
1408
+ * @returns The file system module used for the operation.
1409
+ */
1410
+ resolveOutputMode(pathOrUrl, options = {}) {
1411
+ if (options.mode === "virtual" && this.#context.config.output.mode !== "fs" && isParentPath(toFilePath(pathOrUrl), this.#context.artifactsPath)) {
1412
+ return "virtual";
1413
+ } else if (options.mode === "fs" || this.#context.config.output.mode === "fs" || isParentPath(toFilePath(pathOrUrl), this.#context.dataPath) || isParentPath(toFilePath(pathOrUrl), this.#context.cachePath) || isParentPath(toFilePath(pathOrUrl), joinPaths(this.#context.workspaceConfig.workspaceRoot, this.#context.config.output.outputPath))) {
1414
+ return "fs";
1415
+ }
1416
+ return null;
1417
+ }
1418
+ /**
1419
+ * Clears the resolver cache for a given path.
1420
+ *
1421
+ * @param path - The path to clear the resolver cache for.
1422
+ */
1423
+ clearResolverCache(path2) {
1424
+ this.#cachedResolver.keys().filter((key) => key.startsWith(toFilePath(path2))).forEach((key) => this.#cachedResolver.delete(key));
1425
+ }
1426
+ };
1427
+ var FILE_PREFIX = "file://";
1428
+ function toFilePath(pathOrUrl) {
1429
+ if (!pathOrUrl) {
1430
+ throw new Error("No Path or URL provided to Virtual File System");
1431
+ }
1432
+ let result = pathOrUrl.toString();
1433
+ if (result.startsWith(FILE_PREFIX)) {
1434
+ result = result.slice(FILE_PREFIX.length);
1435
+ }
1436
+ return result;
1437
+ }
1438
+ __name(toFilePath, "toFilePath");
1439
+ var FS_METHODS = [
1440
+ "mkdir",
1441
+ "mkdirSync",
1442
+ "rmdir",
1443
+ "rmdirSync",
1444
+ "unlink",
1445
+ "unlinkSync",
1446
+ "existsSync",
1447
+ "realpathSync",
1448
+ "writeFileSync",
1449
+ "readFileSync",
1450
+ "readdirSync",
1451
+ "createWriteStream",
1452
+ "WriteStream",
1453
+ "createReadStream",
1454
+ "ReadStream"
1455
+ ];
1456
+ var FS_PROMISE_METHODS = [
1457
+ "mkdir",
1458
+ "rm",
1459
+ "rmdir",
1460
+ "unlink",
1461
+ "writeFile",
1462
+ "readFile",
1463
+ "readdir",
1464
+ "stat",
1465
+ "lstat"
1466
+ ];
1467
+ function cloneFS(originalFS) {
1468
+ const clonedFS = {
1469
+ ...originalFS,
1470
+ promises: {
1471
+ ...originalFS.promises ?? {}
1472
+ }
1473
+ };
1474
+ for (const method of FS_METHODS) {
1475
+ if (originalFS[method]) {
1476
+ clonedFS[method] = originalFS[method];
1477
+ }
1478
+ }
1479
+ originalFS.promises ??= {};
1480
+ for (const method of FS_PROMISE_METHODS) {
1481
+ if (originalFS.promises[method]) {
1482
+ clonedFS.promises ??= {};
1483
+ clonedFS.promises[method] = originalFS.promises[method];
1484
+ clonedFS[method] = originalFS.promises[method];
1485
+ }
1486
+ }
1487
+ for (const prop in clonedFS) {
1488
+ if (isFunction(clonedFS[prop])) {
1489
+ clonedFS[prop] = clonedFS[prop].bind(originalFS);
1490
+ if (isFunction(clonedFS.promises[prop])) {
1491
+ clonedFS.promises[prop] = clonedFS.promises[prop].bind(originalFS);
1492
+ }
1493
+ }
1494
+ }
1495
+ for (const prop in clonedFS.promises) {
1496
+ if (isFunction(clonedFS.promises[prop])) {
1497
+ clonedFS.promises[prop] = clonedFS.promises[prop].bind(originalFS);
1498
+ }
1499
+ }
1500
+ return clonedFS;
1501
+ }
1502
+ __name(cloneFS, "cloneFS");
1503
+ function patchFS(originalFS, vfs) {
1504
+ const clonedFS = cloneFS(originalFS);
1505
+ originalFS.mkdirSync = (file, options) => vfs.mkdirSync(toFilePath(file), options);
1506
+ originalFS.mkdir = (file, options, callback) => vfs.mkdir(toFilePath(file), options, callback);
1507
+ originalFS.promises.mkdir = async (file, options) => vfs.mkdir(toFilePath(file), options);
1508
+ originalFS.unlinkSync = (file) => vfs.unlinkSync(toFilePath(file));
1509
+ originalFS.promises.rm = async (file, options) => vfs.rm(toFilePath(file), options);
1510
+ originalFS.promises.unlink = async (file) => vfs.unlink(toFilePath(file));
1511
+ originalFS.existsSync = (file) => vfs.existsSync(toFilePath(file));
1512
+ Object.defineProperty(originalFS, "realpathSync", {
1513
+ value: /* @__PURE__ */ __name((file, options) => vfs.realpathSync(toFilePath(file), options), "value")
1514
+ });
1515
+ originalFS.writeFileSync = (file, data, options) => vfs.writeFileSync(toFilePath(file), data, options);
1516
+ originalFS.promises.writeFile = async (file, data, options) => vfs.writeFile(toFilePath(file), data, options);
1517
+ originalFS.readFileSync = (file, options) => vfs.readFileSync(toFilePath(file), options);
1518
+ originalFS.promises.readFile = (file, options) => vfs.readFile(toFilePath(file), options);
1519
+ originalFS.readdirSync = (file, options) => vfs.readdirSync(toFilePath(file), options);
1520
+ originalFS.promises.readdir = (file, options) => vfs.readdir(toFilePath(file), options);
1521
+ Object.defineProperty(originalFS, "statSync", {
1522
+ value: /* @__PURE__ */ __name((file, options) => vfs.statSync(toFilePath(file), options), "value")
1523
+ });
1524
+ originalFS.stat = (file, options) => vfs.statSync(toFilePath(file), options);
1525
+ originalFS.promises.stat = (file, options) => vfs.stat(toFilePath(file), options);
1526
+ Object.defineProperty(originalFS, "lstatSync", {
1527
+ value: /* @__PURE__ */ __name((file, options) => vfs.lstatSync(toFilePath(file), options), "value")
1528
+ });
1529
+ originalFS.lstat = (file, options) => vfs.lstatSync(toFilePath(file), options);
1530
+ originalFS.promises.lstat = (file, options) => vfs.lstat(toFilePath(file), options);
1531
+ return () => {
1532
+ originalFS.mkdirSync = clonedFS.mkdirSync;
1533
+ originalFS.mkdir = clonedFS.mkdir;
1534
+ originalFS.promises.mkdir = clonedFS.promises.mkdir;
1535
+ originalFS.unlinkSync = clonedFS.unlinkSync;
1536
+ originalFS.promises.rm = clonedFS.promises.rm;
1537
+ originalFS.promises.unlink = clonedFS.promises.unlink;
1538
+ originalFS.existsSync = clonedFS.existsSync;
1539
+ originalFS.realpathSync = clonedFS.realpathSync;
1540
+ originalFS.writeFileSync = clonedFS.writeFileSync;
1541
+ originalFS.promises.writeFile = clonedFS.promises.writeFile;
1542
+ originalFS.readFileSync = clonedFS.readFileSync;
1543
+ originalFS.promises.readFile = clonedFS.promises.readFile;
1544
+ originalFS.readdirSync = clonedFS.readdirSync;
1545
+ originalFS.promises.readdir = clonedFS.promises.readdir;
1546
+ Object.defineProperty(originalFS, "statSync", {
1547
+ value: clonedFS.statSync
1548
+ });
1549
+ originalFS.stat = clonedFS.stat;
1550
+ originalFS.promises.stat = clonedFS.promises.stat;
1551
+ Object.defineProperty(originalFS, "lstatSync", {
1552
+ value: clonedFS.lstatSync
1553
+ });
1554
+ originalFS.lstat = clonedFS.lstat;
1555
+ originalFS.promises.lstat = clonedFS.promises.lstat;
1556
+ };
1557
+ }
1558
+ __name(patchFS, "patchFS");
1559
+ function checkVariants(request, vfs, parentPath) {
1560
+ const path2 = parentPath ? joinPaths(parentPath, request) : request;
1561
+ let file = checkExtensions(path2, vfs);
1562
+ if (file) {
1563
+ return file;
1564
+ }
1565
+ file = checkIndex(path2, vfs);
1566
+ if (file) {
1567
+ return file;
1568
+ }
1569
+ return false;
1570
+ }
1571
+ __name(checkVariants, "checkVariants");
1572
+ function checkIndex(request, vfs) {
1573
+ let file = joinPaths(request, "index");
1574
+ if (vfs.fileExistsSync(file)) {
1575
+ return file;
1576
+ }
1577
+ file = checkExtensions(file, vfs);
1578
+ if (file) {
1579
+ return file;
1580
+ }
1581
+ return false;
1582
+ }
1583
+ __name(checkIndex, "checkIndex");
1584
+ function checkExtensions(request, vfs) {
1585
+ let file = `${request}.ts`;
1586
+ if (vfs.fileExistsSync(file)) {
1587
+ return file;
1588
+ }
1589
+ file = `${request}.mts`;
1590
+ if (vfs.fileExistsSync(file)) {
1591
+ return file;
1592
+ }
1593
+ file = `${request}.cts`;
1594
+ if (vfs.fileExistsSync(file)) {
1595
+ return file;
1596
+ }
1597
+ file = `${request}.tsx`;
1598
+ if (vfs.fileExistsSync(file)) {
1599
+ return file;
1600
+ }
1601
+ file = `${request}.js`;
1602
+ if (vfs.fileExistsSync(file)) {
1603
+ return file;
1604
+ }
1605
+ file = `${request}.mjs`;
1606
+ if (vfs.fileExistsSync(file)) {
1607
+ return file;
1608
+ }
1609
+ file = `${request}.cjs`;
1610
+ if (vfs.fileExistsSync(file)) {
1611
+ return file;
1612
+ }
1613
+ file = `${request}.jsx`;
1614
+ if (vfs.fileExistsSync(file)) {
1615
+ return file;
1616
+ }
1617
+ file = `${request}.json`;
1618
+ if (vfs.fileExistsSync(file)) {
1619
+ return file;
1620
+ }
1621
+ file = `${request}.d.ts`;
1622
+ if (vfs.fileExistsSync(file)) {
1623
+ return file;
1624
+ }
1625
+ return false;
1626
+ }
1627
+ __name(checkExtensions, "checkExtensions");
1628
+ function createVfs(context) {
1629
+ const vfs = new VirtualFileSystem(context);
1630
+ return vfs;
1631
+ }
1632
+ __name(createVfs, "createVfs");
1633
+
1634
+ // ../powerlines/src/internal/contexts/context.ts
1635
+ var cache = /* @__PURE__ */ new WeakMap();
1636
+ var PowerlinesContext = class _PowerlinesContext {
1637
+ static {
1638
+ __name(this, "PowerlinesContext");
1639
+ }
1640
+ #workspaceConfig;
1641
+ #checksum = null;
1642
+ #buildId = uuid();
1643
+ #releaseId = uuid();
1644
+ #timestamp = Date.now();
1645
+ #envPaths;
1646
+ #fs;
1647
+ #tsconfig;
1648
+ // #entry: ResolvedEntryTypeDefinition[] = [] as ResolvedEntryTypeDefinition[];
1649
+ #getConfigProps(config = {}) {
1650
+ return {
1651
+ variant: config.build?.variant,
1652
+ projectType: config.type,
1653
+ projectRoot: config.root,
1654
+ name: config.name,
1655
+ title: config.title,
1656
+ description: config.description,
1657
+ sourceRoot: config.sourceRoot,
1658
+ configFile: config.configFile,
1659
+ customLogger: config.customLogger,
1660
+ logLevel: config.logLevel,
1661
+ builtinPrefix: config.builtinPrefix,
1662
+ tsconfig: config.tsconfig,
1663
+ tsconfigRaw: config.tsconfigRaw,
1664
+ skipCache: config.skipCache,
1665
+ skipInstalls: config.skipInstalls,
1666
+ entry: config.entry,
1667
+ output: config.output,
1668
+ plugins: config.plugins,
1669
+ mode: config.mode,
1670
+ lint: config.lint,
1671
+ transform: config.transform,
1672
+ build: config.build,
1673
+ override: config.override
1674
+ };
1675
+ }
1676
+ /**
1677
+ * Create a new Storm context from the workspace root and user config.
1678
+ *
1679
+ * @param workspaceRoot - The root directory of the workspace.
1680
+ * @param config - The user configuration options.
1681
+ * @returns A promise that resolves to the new context.
1682
+ */
1683
+ static async from(workspaceRoot, config) {
1684
+ const context = new _PowerlinesContext(await loadWorkspaceConfig(workspaceRoot, config.root));
1685
+ await context.withUserConfig(config);
1686
+ context.corePackagePath = process.env.POWERLINES_LOCAL ? joinPaths$1(context.workspaceConfig.workspaceRoot, "packages/core") : await resolvePackage("powerlines");
1687
+ if (!context.corePackagePath) {
1688
+ throw new Error("Could not resolve powerlines package location.");
1689
+ }
1690
+ return context;
1691
+ }
1692
+ dependencies = {};
1693
+ reflections = {};
1694
+ persistedMeta = void 0;
1695
+ corePackagePath;
1696
+ packageJson;
1697
+ projectJson = void 0;
1698
+ resolver;
1699
+ get entry() {
1700
+ return resolveEntriesSync(this, toArray(this.config.entry));
1701
+ }
1702
+ /**
1703
+ * The TypeScript configuration parsed from the tsconfig file
1704
+ */
1705
+ get tsconfig() {
1706
+ if (!this.#tsconfig) {
1707
+ this.#tsconfig = {
1708
+ tsconfigFilePath: this.config.tsconfig
1709
+ };
1710
+ }
1711
+ return this.#tsconfig;
1712
+ }
1713
+ set tsconfig(value) {
1714
+ this.#tsconfig = value;
1715
+ }
1716
+ get fs() {
1717
+ if (!this.#fs) {
1718
+ this.#fs = createVfs(this);
1719
+ }
1720
+ return this.#fs;
1721
+ }
1722
+ /**
1723
+ * Get the checksum of the project's current state
1724
+ */
1725
+ get checksum() {
1726
+ return this.#checksum;
1727
+ }
1728
+ /**
1729
+ * The meta information about the current build
1730
+ */
1731
+ get meta() {
1732
+ return {
1733
+ buildId: this.#buildId,
1734
+ releaseId: this.#releaseId,
1735
+ checksum: this.#checksum,
1736
+ timestamp: this.#timestamp,
1737
+ projectRootHash: murmurhash({
1738
+ workspaceRoot: this.workspaceConfig?.workspaceRoot,
1739
+ projectRoot: this.config?.projectRoot
1740
+ }, {
1741
+ maxLength: PROJECT_ROOT_HASH_LENGTH
1742
+ }),
1743
+ configHash: murmurhash(this.config, {
1744
+ maxLength: CACHE_HASH_LENGTH
1745
+ }),
1746
+ builtinIdMap: {},
1747
+ virtualFiles: {}
1748
+ };
1749
+ }
1750
+ /**
1751
+ * The resolved configuration options
1752
+ */
1753
+ get config() {
1754
+ return this.resolvedConfig ?? {};
1755
+ }
1756
+ /**
1757
+ * The logger function
1758
+ */
1759
+ get log() {
1760
+ if (!this.logFn) {
1761
+ this.logFn = this.createLog();
1762
+ }
1763
+ return this.logFn;
1764
+ }
1765
+ /**
1766
+ * The workspace configuration
1767
+ */
1768
+ get workspaceConfig() {
1769
+ return this.#workspaceConfig;
1770
+ }
1771
+ get envPaths() {
1772
+ if (!this.#envPaths) {
1773
+ this.#envPaths = getEnvPaths({
1774
+ orgId: "storm-software",
1775
+ appId: "powerlines",
1776
+ workspaceRoot: this.workspaceConfig.workspaceRoot
1777
+ });
1778
+ }
1779
+ return this.#envPaths;
1780
+ }
1781
+ /**
1782
+ * Get the path to the artifacts directory for the project
1783
+ */
1784
+ get artifactsPath() {
1785
+ return joinPaths$1(this.workspaceConfig.workspaceRoot, this.config.projectRoot, ".storm");
1786
+ }
1787
+ /**
1788
+ * Get the path to the builtin modules used by the project
1789
+ */
1790
+ get builtinsPath() {
1791
+ return joinPaths$1(this.artifactsPath, "builtins");
1792
+ }
1793
+ /**
1794
+ * Get the path to the entry directory for the project
1795
+ */
1796
+ get entryPath() {
1797
+ return joinPaths$1(this.artifactsPath, "entry");
1798
+ }
1799
+ /**
1800
+ * Get the path to the data directory for the project
1801
+ */
1802
+ get dataPath() {
1803
+ return joinPaths$1(this.envPaths.data, "projects", getPrefixedProjectRootHash(this.config.name, this.meta.projectRootHash));
1804
+ }
1805
+ /**
1806
+ * Get the path to the cache directory for the project
1807
+ */
1808
+ get cachePath() {
1809
+ return joinPaths$1(this.envPaths.cache, "projects", murmurhash({
1810
+ checksum: this.#checksum,
1811
+ config: this.meta.configHash
1812
+ }, {
1813
+ maxLength: CACHE_HASH_LENGTH
1814
+ }));
1815
+ }
1816
+ /**
1817
+ * Get the path to the generated declaration file for the project
1818
+ */
1819
+ get dtsPath() {
1820
+ return this.config.output.dts ? appendPath(this.config.output.dts, this.workspaceConfig.workspaceRoot) : joinPaths$1(this.workspaceConfig.workspaceRoot, this.config.projectRoot, "storm.d.ts");
1821
+ }
1822
+ /**
1823
+ * Get the project root relative to the workspace root
1824
+ */
1825
+ get relativeToWorkspaceRoot() {
1826
+ return relativeToWorkspaceRoot(this.config.projectRoot);
1827
+ }
1828
+ /**
1829
+ * Update the context using a new user configuration options
1830
+ *
1831
+ * @param userConfig - The new user configuration options.
1832
+ */
1833
+ async withUserConfig(userConfig, options = {
1834
+ isHighPriority: true
1835
+ }) {
1836
+ this.mergeUserConfig(userConfig);
1837
+ await this.init(this.config.userConfig, options);
1838
+ }
1839
+ /**
1840
+ * Update the context using a new inline configuration options
1841
+ *
1842
+ * @param inlineConfig - The new inline configuration options.
1843
+ */
1844
+ async withInlineConfig(inlineConfig, options = {
1845
+ isHighPriority: true
1846
+ }) {
1847
+ this.config.inlineConfig = inlineConfig;
1848
+ if (inlineConfig.command === "new") {
1849
+ const workspacePackageJsonPath = joinPaths$1(this.workspaceConfig.workspaceRoot, "package.json");
1850
+ if (!existsSync(workspacePackageJsonPath)) {
1851
+ throw new Error(`The workspace package.json file could not be found at ${workspacePackageJsonPath}`);
1852
+ }
1853
+ this.packageJson = await readJsonFile(workspacePackageJsonPath);
1854
+ this.workspaceConfig.repository ??= isSetString(this.packageJson?.repository) ? this.packageJson.repository : this.packageJson?.repository?.url;
1855
+ }
1856
+ await this.init(this.config.inlineConfig, options);
1857
+ }
1858
+ /**
1859
+ * Create a new logger instance
1860
+ *
1861
+ * @param name - The name to use for the logger instance
1862
+ * @returns A logger function
1863
+ */
1864
+ createLog(name = null) {
1865
+ return createLog(name, {
1866
+ ...this.config,
1867
+ logLevel: isNull(this.config.logLevel) ? "silent" : this.config.logLevel
1868
+ });
1869
+ }
1870
+ /**
1871
+ * Extend the current logger instance with a new name
1872
+ *
1873
+ * @param name - The name to use for the extended logger instance
1874
+ * @returns A logger function
1875
+ */
1876
+ extendLog(name) {
1877
+ return extendLog(this.log, name);
1878
+ }
1879
+ /**
1880
+ * Creates a new StormContext instance.
1881
+ *
1882
+ * @param workspaceConfig - The workspace configuration.
1883
+ */
1884
+ constructor(workspaceConfig) {
1885
+ this.#workspaceConfig = workspaceConfig;
1886
+ this.#envPaths = getEnvPaths({
1887
+ orgId: "storm-software",
1888
+ appId: "powerlines",
1889
+ workspaceRoot: workspaceConfig.workspaceRoot
1890
+ });
1891
+ }
1892
+ /**
1893
+ * The resolved configuration for this context
1894
+ */
1895
+ resolvedConfig = {};
1896
+ /**
1897
+ * A logger function specific to this context
1898
+ */
1899
+ logFn;
1900
+ /**
1901
+ * Initialize the context with the provided configuration options
1902
+ *
1903
+ * @param config - The partial user configuration to use for initialization.
1904
+ */
1905
+ async init(config = {}, options = {
1906
+ isHighPriority: true
1907
+ }) {
1908
+ const cacheKey = {
1909
+ projectRoot: config.root ?? this.config.projectRoot ?? this.config.userConfig?.root ?? this.config.inlineConfig?.root,
1910
+ mode: (config.mode ?? this.config.mode) || this.workspaceConfig.mode,
1911
+ skipCache: config.skipCache ?? this.config.skipCache ?? false,
1912
+ configFile: config.configFile ?? this.config.configFile,
1913
+ command: this.config.inlineConfig?.command
1914
+ };
1915
+ if (cache.has(cacheKey)) {
1916
+ const result = cache.get(cacheKey);
1917
+ this.projectJson = result.projectJson;
1918
+ this.packageJson = result.packageJson;
1919
+ this.#checksum = result.checksum;
1920
+ this.resolver = result.resolver;
1921
+ this.mergeUserConfig(result.userConfig.config, this.config.userConfig);
1922
+ } else {
1923
+ const projectJsonPath = joinPaths$1(cacheKey.projectRoot, "project.json");
1924
+ if (existsSync(projectJsonPath)) {
1925
+ this.projectJson = await readJsonFile(projectJsonPath);
1926
+ }
1927
+ const packageJsonPath = joinPaths$1(cacheKey.projectRoot, "package.json");
1928
+ if (existsSync(packageJsonPath)) {
1929
+ this.packageJson = await readJsonFile(packageJsonPath);
1930
+ }
1931
+ this.#checksum = await getChecksum(cacheKey.projectRoot);
1932
+ this.resolver = createResolver({
1933
+ workspaceRoot: this.workspaceConfig.workspaceRoot,
1934
+ projectRoot: cacheKey.projectRoot,
1935
+ cacheDir: this.cachePath,
1936
+ mode: cacheKey.mode,
1937
+ skipCache: cacheKey.skipCache
1938
+ });
1939
+ const userConfig = await loadUserConfigFile(cacheKey.projectRoot, this.resolver, cacheKey.command, cacheKey.mode, cacheKey.configFile);
1940
+ this.mergeUserConfig(userConfig.config);
1941
+ cache.set(cacheKey, {
1942
+ projectJson: this.projectJson,
1943
+ packageJson: this.packageJson,
1944
+ checksum: this.#checksum,
1945
+ resolver: this.resolver,
1946
+ userConfig
1947
+ });
1948
+ }
1949
+ if (isSetObject(config)) {
1950
+ this.resolvedConfig = defu4({
1951
+ inlineConfig: this.config.inlineConfig,
1952
+ userConfig: this.config.userConfig
1953
+ }, options.isHighPriority ? this.#getConfigProps(config) : {}, {
1954
+ command: this.config.inlineConfig?.command,
1955
+ ...this.#getConfigProps(this.config.inlineConfig)
1956
+ }, this.#getConfigProps(this.config.userConfig), {
1957
+ mode: this.workspaceConfig?.mode,
1958
+ logLevel: this.workspaceConfig?.logLevel,
1959
+ skipCache: this.workspaceConfig?.skipCache
1960
+ }, {
1961
+ name: this.projectJson?.name || this.packageJson?.name,
1962
+ version: this.packageJson?.version,
1963
+ description: this.packageJson?.description,
1964
+ tsconfig: appendPath("tsconfig.json", cacheKey.projectRoot),
1965
+ sourceRoot: this.projectJson?.sourceRoot || appendPath("src", cacheKey.projectRoot),
1966
+ output: {
1967
+ outputPath: joinPaths$1("dist", cacheKey.projectRoot),
1968
+ mode: "virtual",
1969
+ dts: joinPaths$1(cacheKey.projectRoot, "storm.d.ts"),
1970
+ assets: [
1971
+ {
1972
+ glob: "LICENSE"
1973
+ },
1974
+ {
1975
+ input: cacheKey.projectRoot,
1976
+ glob: "*.md"
1977
+ },
1978
+ {
1979
+ input: cacheKey.projectRoot,
1980
+ glob: "package.json"
1981
+ }
1982
+ ]
1983
+ }
1984
+ }, options.isHighPriority ? {} : this.#getConfigProps(config), {
1985
+ inlineConfig: {},
1986
+ userConfig: {},
1987
+ platform: "neutral",
1988
+ mode: "production",
1989
+ projectType: "application",
1990
+ logLevel: "info",
1991
+ builtinPrefix: "storm",
1992
+ preview: false,
1993
+ environments: {},
1994
+ transform: {
1995
+ babel: {
1996
+ plugins: [],
1997
+ presets: []
1998
+ }
1999
+ },
2000
+ lint: {
2001
+ eslint: {}
2002
+ },
2003
+ build: {
2004
+ target: "esnext"
2005
+ },
2006
+ override: {}
2007
+ });
2008
+ }
2009
+ this.config.entry = getUniqueEntries(this.config.entry);
2010
+ if (this.config.name?.startsWith("@") && this.config.name.split("/").filter(Boolean).length > 1) {
2011
+ this.config.name = this.config.name.split("/").filter(Boolean)[1];
2012
+ }
2013
+ this.config.title ??= titleCase(this.config.name);
2014
+ if (this.config.build.external) {
2015
+ this.config.build.external = getUnique(this.config.build.external);
2016
+ }
2017
+ if (this.config.build.noExternal) {
2018
+ this.config.build.noExternal = getUnique(this.config.build.noExternal);
2019
+ }
2020
+ this.config.output.format = getUnique(toArray(this.config.output?.format ?? (this.config.projectType === "library" ? [
2021
+ "cjs",
2022
+ "esm"
2023
+ ] : [
2024
+ "esm"
2025
+ ])));
2026
+ this.config.output.outputPath ??= joinPaths$1("dist", this.config.projectRoot || ".");
2027
+ this.config.output.assets = getUnique(this.config.output.assets.map((asset) => {
2028
+ return {
2029
+ glob: isSetObject(asset) ? asset.glob : asset,
2030
+ input: isString(asset) || !asset.input || asset.input === "." || asset.input === "/" || asset.input === "./" ? this.workspaceConfig.workspaceRoot : appendPath(asset.input, this.workspaceConfig.workspaceRoot),
2031
+ output: appendPath(isSetObject(asset) && asset.output ? joinPaths$1(this.config.output.outputPath, replacePath(asset.output, this.config.output.outputPath)) : this.config.output.outputPath, this.workspaceConfig.workspaceRoot),
2032
+ ignore: isSetObject(asset) && asset.ignore ? toArray(asset.ignore) : void 0
2033
+ };
2034
+ }));
2035
+ this.config.plugins = (this.config.plugins ?? []).filter(Boolean).reduce((ret, plugin) => {
2036
+ if (isPlugin(plugin) && checkDedupe(plugin, ret.filter((p) => isPlugin(p)))) {
2037
+ return ret;
2038
+ }
2039
+ ret.push(plugin);
2040
+ return ret;
2041
+ }, []);
2042
+ }
2043
+ mergeUserConfig(from = {}, into = this.config.userConfig ?? {}) {
2044
+ this.config.userConfig = defu4({
2045
+ entry: Array.isArray(from.entry) && from.entry.length > 0 ? from.entry : Array.isArray(into?.entry) && into.entry.length > 0 ? into.entry : []
2046
+ }, omit(from ?? {}, [
2047
+ "entry"
2048
+ ]), omit(into ?? {}, [
2049
+ "entry"
2050
+ ]));
2051
+ if (this.config.userConfig.output?.format) {
2052
+ this.config.userConfig.output.format = getUnique(toArray(this.config.userConfig.output?.format));
2053
+ }
2054
+ this.config.userConfig.plugins = (this.config.userConfig.plugins ?? []).filter(Boolean).reduce((ret, plugin) => {
2055
+ if (isPlugin(plugin) && checkDedupe(plugin, ret.filter((p) => isPlugin(p)))) {
2056
+ return ret;
2057
+ }
2058
+ ret.push(plugin);
2059
+ return ret;
2060
+ }, []);
2061
+ }
2062
+ };
2063
+
2064
+ // ../powerlines/src/types/plugin.ts
2065
+ var PLUGIN_NON_HOOK_FIELDS = [
2066
+ "name",
2067
+ "enforce",
2068
+ "dedupe",
2069
+ "dependsOn",
2070
+ "applyToEnvironment"
2071
+ ];
2072
+ async function callHook(context, hook, options, ...args) {
2073
+ const handlers = context.selectHooks(hook, options);
2074
+ if (handlers.length > 0) {
2075
+ context.log(LogLevelLabel.DEBUG, ` \u{1F9E9} Calling plugin hook: ${chalk5.bold.cyanBright(`${hook}${options?.order ? ` (${options.order})` : ""}`)}`);
2076
+ let results = [];
2077
+ if (options?.sequential === false) {
2078
+ results = await Promise.all(handlers.map(async (handler) => {
2079
+ if (!isFunction(handler)) {
2080
+ throw new Error(`Plugin hook handler for hook "${hook}" is not a function.`);
2081
+ }
2082
+ return Promise.resolve(handler.apply(null, ...args));
2083
+ }));
2084
+ } else {
2085
+ for (const handler of handlers) {
2086
+ if (!isFunction(handler)) {
2087
+ throw new Error(`Plugin hook handler for hook "${hook}" is not a function.`);
2088
+ }
2089
+ results.push(await Promise.resolve(handler.apply(null, ...args)));
2090
+ if (options?.result === "first" && isSet(results[results.length - 1])) {
2091
+ break;
2092
+ }
2093
+ }
2094
+ }
2095
+ const definedResults = results.filter((result) => isSet(result));
2096
+ if (definedResults.length > 0) {
2097
+ let mergedResult = void 0;
2098
+ for (const result of definedResults) {
2099
+ mergedResult = defu(result, mergedResult ?? {});
2100
+ }
2101
+ return mergedResult;
2102
+ }
2103
+ }
2104
+ return void 0;
2105
+ }
2106
+ __name(callHook, "callHook");
2107
+
2108
+ // ../powerlines/src/internal/contexts/plugin-context.ts
2109
+ function createPluginContext(plugin, environment) {
2110
+ const normalizeMessage = /* @__PURE__ */ __name((message) => {
2111
+ return isString(message) ? message : message.message;
2112
+ }, "normalizeMessage");
2113
+ const log = environment.extendLog(plugin.name);
2114
+ const callHookFn = /* @__PURE__ */ __name(async (hook, options, ...args) => {
2115
+ return callHook(environment, hook, {
2116
+ sequential: true,
2117
+ ...options
2118
+ }, ...args);
2119
+ }, "callHookFn");
2120
+ return new Proxy({}, {
2121
+ get(_, prop) {
2122
+ if (prop === "$$internal") {
2123
+ return {
2124
+ environment,
2125
+ callHook: callHookFn
2126
+ };
2127
+ }
2128
+ if (prop === "log" || prop === "logger") {
2129
+ return log;
2130
+ }
2131
+ if (prop === "error") {
2132
+ return (message) => {
2133
+ log(LogLevelLabel.ERROR, normalizeMessage(message));
2134
+ };
2135
+ }
2136
+ if (prop === "warn") {
2137
+ return (message) => {
2138
+ log(LogLevelLabel.WARN, normalizeMessage(message));
2139
+ };
2140
+ }
2141
+ return environment[prop];
2142
+ },
2143
+ set(_, prop, value) {
2144
+ if ([
2145
+ "$$internal",
2146
+ "environment",
2147
+ "config",
2148
+ "log",
2149
+ "logger",
2150
+ "error",
2151
+ "warn",
2152
+ "plugins",
2153
+ "hooks",
2154
+ "addPlugin",
2155
+ "selectHooks"
2156
+ ].includes(prop)) {
2157
+ log(LogLevelLabel.WARN, `Cannot set read-only property "${String(prop)}"`);
2158
+ return false;
2159
+ }
2160
+ environment[prop] = value;
2161
+ return true;
2162
+ }
2163
+ });
2164
+ }
2165
+ __name(createPluginContext, "createPluginContext");
2166
+
2167
+ // ../powerlines/src/internal/contexts/environment-context.ts
2168
+ var PowerlinesEnvironmentContext = class _PowerlinesEnvironmentContext extends PowerlinesContext {
2169
+ static {
2170
+ __name(this, "PowerlinesEnvironmentContext");
2171
+ }
2172
+ #hooks = {};
2173
+ /**
2174
+ * Create a new Storm context from the workspace root and user config.
2175
+ *
2176
+ * @param workspaceConfig - The root directory of the workspace.
2177
+ * @param config - The user configuration options.
2178
+ * @returns A promise that resolves to the new context.
2179
+ */
2180
+ static async fromConfig(workspaceConfig, config) {
2181
+ const context = new _PowerlinesEnvironmentContext(config, workspaceConfig);
2182
+ await context.init();
2183
+ context.corePackagePath = process.env.POWERLINES_LOCAL ? joinPaths(context.workspaceConfig.workspaceRoot, "packages/core") : await resolvePackage("powerlines");
2184
+ if (!context.corePackagePath) {
2185
+ throw new Error("Could not resolve powerlines package location.");
2186
+ }
2187
+ return context;
2188
+ }
2189
+ environment;
2190
+ plugins = [];
2191
+ /**
2192
+ * The resolved configuration options
2193
+ */
2194
+ get config() {
2195
+ return super.config;
2196
+ }
2197
+ get hooks() {
2198
+ return this.#hooks;
2199
+ }
2200
+ async addPlugin(plugin) {
2201
+ let resolvedPlugin = plugin;
2202
+ if (plugin.applyToEnvironment) {
2203
+ const result = await Promise.resolve(plugin.applyToEnvironment(this.environment));
2204
+ if (!result || isObject(result) && Object.keys(result).length === 0) {
2205
+ return;
2206
+ }
2207
+ resolvedPlugin = isPlugin(result) ? result : plugin;
2208
+ }
2209
+ const context = createPluginContext(resolvedPlugin, this);
2210
+ this.plugins.push({
2211
+ plugin: resolvedPlugin,
2212
+ context
2213
+ });
2214
+ this.#hooks = Object.keys(resolvedPlugin).filter((key) => !PLUGIN_NON_HOOK_FIELDS.includes(key)).reduce((ret, key) => {
2215
+ const hook = key;
2216
+ const pluginHook = resolvedPlugin[hook];
2217
+ if (!isPluginHook(pluginHook)) {
2218
+ return ret;
2219
+ }
2220
+ if (!isHookExternal(hook)) {
2221
+ ret[hook] ??= {};
2222
+ if (resolvedPlugin.enforce) {
2223
+ ret[hook][`${resolvedPlugin.enforce}Enforced`] ??= [];
2224
+ addPluginHook(context, resolvedPlugin, pluginHook, ret[hook][`${resolvedPlugin.enforce}Enforced`]);
2225
+ return ret;
2226
+ }
2227
+ if (isFunction(pluginHook) || !pluginHook.order) {
2228
+ ret[hook].normal ??= [];
2229
+ addPluginHook(context, resolvedPlugin, pluginHook, ret[hook].normal);
2230
+ return ret;
2231
+ }
2232
+ ret[hook][`${pluginHook.order}Ordered`] ??= [];
2233
+ addPluginHook(context, resolvedPlugin, pluginHook, ret[hook][`${pluginHook.order}Ordered`]);
2234
+ } else {
2235
+ ret[hook] ??= [];
2236
+ ret[hook].push({
2237
+ plugin: resolvedPlugin,
2238
+ hook: getHookHandler(pluginHook).bind(context)
2239
+ });
2240
+ }
2241
+ return ret;
2242
+ }, this.hooks);
2243
+ }
2244
+ /**
2245
+ * Retrieves the hook handlers for a specific hook name
2246
+ */
2247
+ selectHooks(hook, options) {
2248
+ const handlers = [];
2249
+ if (this.hooks[hook]) {
2250
+ if (!isHookExternal(hook)) {
2251
+ const hooks = this.hooks[hook];
2252
+ if (options?.order) {
2253
+ if (options?.order === "pre") {
2254
+ handlers.push(...(hooks.preOrdered ?? []).map((h) => h.handler));
2255
+ handlers.push(...(hooks.preEnforced ?? []).map((h) => h.handler));
2256
+ } else if (options?.order === "post") {
2257
+ handlers.push(...(hooks.postOrdered ?? []).map((h) => h.handler));
2258
+ handlers.push(...(hooks.postEnforced ?? []).map((h) => h.handler));
2259
+ } else {
2260
+ handlers.push(...(hooks.normal ?? []).map((h) => h.handler));
2261
+ }
2262
+ } else {
2263
+ handlers.push(...this.selectHooks(hook, {
2264
+ order: "pre"
2265
+ }));
2266
+ handlers.push(...this.selectHooks(hook, {
2267
+ order: "normal"
2268
+ }));
2269
+ handlers.push(...this.selectHooks(hook, {
2270
+ order: "post"
2271
+ }));
2272
+ }
2273
+ } else {
2274
+ handlers.push(...this.hooks[hook].map((h) => h.handler));
2275
+ }
2276
+ }
2277
+ return handlers;
2278
+ }
2279
+ constructor(config, workspaceConfig) {
2280
+ super(workspaceConfig);
2281
+ this.resolvedConfig = config;
2282
+ }
2283
+ };
2284
+
2285
+ // ../powerlines/src/internal/contexts/api-context.ts
2286
+ var PowerlinesAPIContext = class _PowerlinesAPIContext extends PowerlinesContext {
2287
+ static {
2288
+ __name(this, "PowerlinesAPIContext");
2289
+ }
2290
+ #environments = {};
2291
+ #plugins = [];
2292
+ #log;
2293
+ /**
2294
+ * Create a new Storm context from the workspace root and user config.
2295
+ *
2296
+ * @param workspaceRoot - The root directory of the workspace.
2297
+ * @param config - The user configuration options.
2298
+ * @returns A promise that resolves to the new context.
2299
+ */
2300
+ static async from(workspaceRoot, config) {
2301
+ const context = new _PowerlinesAPIContext(await loadWorkspaceConfig(workspaceRoot, config.root));
2302
+ await context.withUserConfig(config);
2303
+ context.corePackagePath = process.env.POWERLINES_LOCAL ? joinPaths(context.workspaceConfig.workspaceRoot, "packages/core") : await resolvePackage("powerlines");
2304
+ if (!context.corePackagePath) {
2305
+ throw new Error("Could not resolve powerlines package location.");
2306
+ }
2307
+ return context;
2308
+ }
2309
+ /**
2310
+ * A record of all environments by name
2311
+ */
2312
+ get environments() {
2313
+ return this.#environments;
2314
+ }
2315
+ get log() {
2316
+ if (!this.#log) {
2317
+ this.#log = this.createLog("engine");
2318
+ }
2319
+ return this.#log;
2320
+ }
2321
+ get plugins() {
2322
+ return this.#plugins;
2323
+ }
2324
+ constructor(workspaceConfig) {
2325
+ super(workspaceConfig);
2326
+ }
2327
+ /**
2328
+ * Initialize the context with the provided configuration options
2329
+ *
2330
+ * @param config - The partial user configuration to use for initialization.
2331
+ */
2332
+ async init(config = {}) {
2333
+ await super.init(config);
2334
+ await Promise.all(toArray(this.config.userConfig.environments && Object.keys(this.config.userConfig.environments).length > 0 ? Object.keys(this.config.userConfig.environments).map((name) => createEnvironment(name, this.config.userConfig)) : createDefaultEnvironment(this.config.userConfig)).map(async (env) => {
2335
+ this.#environments[env.name] = await this.in(env);
2336
+ }));
2337
+ }
2338
+ /**
2339
+ * A function to copy the context and update the fields for a specific environment
2340
+ *
2341
+ * @param environment - The environment configuration to use.
2342
+ * @returns A new context instance with the updated environment.
2343
+ */
2344
+ async in(environment) {
2345
+ let context;
2346
+ if (this.environments[environment.name]) {
2347
+ context = this.environments[environment.name];
2348
+ } else {
2349
+ context = await PowerlinesEnvironmentContext.fromConfig(this.workspaceConfig, this.config);
2350
+ }
2351
+ if (isSetObject(this.config.inlineConfig)) {
2352
+ await context.withInlineConfig(this.config.inlineConfig);
2353
+ }
2354
+ context.environment = environment;
2355
+ context.plugins = [];
2356
+ for (const plugin of this.plugins) {
2357
+ await context.addPlugin(plugin);
2358
+ }
2359
+ return context;
2360
+ }
2361
+ async addPlugin(plugin) {
2362
+ this.plugins.push(plugin);
2363
+ await Promise.all(Object.keys(this.environments).map(async (name) => {
2364
+ await this.environments[name].addPlugin(plugin);
2365
+ }));
2366
+ }
2367
+ async getEnvironment(name) {
2368
+ let environment;
2369
+ if (name) {
2370
+ environment = this.environments[name];
2371
+ }
2372
+ if (Object.keys(this.environments).length === 1) {
2373
+ environment = this.environments[Object.keys(this.environments)[0]];
2374
+ this.log(LogLevelLabel.DEBUG, `Applying the only configured environment: ${chalk5.bold.cyanBright(environment?.environment.name)}`);
2375
+ }
2376
+ if (!environment) {
2377
+ if (name) {
2378
+ throw new Error(`Environment "${name}" not found.`);
2379
+ }
2380
+ environment = await this.in(createDefaultEnvironment(this.config.userConfig));
2381
+ this.log(LogLevelLabel.WARN, `No environment specified, and no default environment found. Using a temporary default environment: ${chalk5.bold.cyanBright(environment?.environment.name)}`);
2382
+ }
2383
+ return environment;
2384
+ }
2385
+ async getEnvironmentSafe(name) {
2386
+ try {
2387
+ return await this.getEnvironment(name);
2388
+ } catch {
2389
+ return void 0;
2390
+ }
2391
+ }
2392
+ };
2393
+ async function installPackage(context, packageName, dev = false) {
2394
+ const isListed = await isPackageListed(packageName, {
2395
+ cwd: context.config.projectRoot
2396
+ });
2397
+ if (!isListed) {
2398
+ if (context.config.skipInstalls !== true && !process.env.POWERLINES_LOCAL) {
2399
+ context.log(LogLevelLabel.WARN, `The package "${packageName}" is not installed. It will be installed automatically.`);
2400
+ const result = await install(packageName, {
2401
+ cwd: context.config.projectRoot,
2402
+ dev
2403
+ });
2404
+ if (isNumber(result.exitCode) && result.exitCode > 0) {
2405
+ context.log(LogLevelLabel.ERROR, result.stderr);
2406
+ throw new Error(`An error occurred while installing the package "${packageName}"`);
2407
+ }
2408
+ } else {
2409
+ context.log(LogLevelLabel.WARN, `The package "${packageName}" is not installed. Since the "skipInstalls" option is set to true, it will not be installed automatically.`);
2410
+ }
2411
+ } else if (hasPackageVersion(packageName) && !process.env.STORM_STACK_SKIP_VERSION_CHECK) {
2412
+ const isMatching = await doesPackageMatch(getPackageName(packageName), getPackageVersion(packageName), context.config.projectRoot);
2413
+ if (!isMatching) {
2414
+ const packageListing = await getPackageListing(getPackageName(packageName), {
2415
+ cwd: context.config.projectRoot
2416
+ });
2417
+ if (!packageListing?.version.startsWith("catalog:") && !packageListing?.version.startsWith("workspace:")) {
2418
+ context.log(LogLevelLabel.WARN, `The package "${getPackageName(packageName)}" is installed but does not match the expected version ${getPackageVersion(packageName)} (installed version: ${packageListing?.version || "<Unknown>"}). Please ensure this is intentional before proceeding. Note: You can skip this validation with the "STORM_STACK_SKIP_VERSION_CHECK" environment variable.`);
2419
+ }
2420
+ }
2421
+ }
2422
+ }
2423
+ __name(installPackage, "installPackage");
2424
+ async function installPackages(context, packages) {
2425
+ return Promise.all(packages.map(async (pkg) => installPackage(context, pkg.name, pkg.dev)));
2426
+ }
2427
+ __name(installPackages, "installPackages");
2428
+
2429
+ // ../powerlines/src/internal/helpers/eslint.ts
2430
+ var MessageSeverity;
2431
+ (function(MessageSeverity2) {
2432
+ MessageSeverity2[MessageSeverity2["Warning"] = 1] = "Warning";
2433
+ MessageSeverity2[MessageSeverity2["Error"] = 2] = "Error";
2434
+ })(MessageSeverity || (MessageSeverity = {}));
2435
+ function pluginCount(messages) {
2436
+ let nextPluginWarningCount = 0;
2437
+ let nextPluginErrorCount = 0;
2438
+ for (let i = 0; i < messages.length; i++) {
2439
+ const { severity, ruleId } = messages[i];
2440
+ if (ruleId?.includes("powerlines")) {
2441
+ if (severity === 1) {
2442
+ nextPluginWarningCount += 1;
2443
+ } else {
2444
+ nextPluginErrorCount += 1;
2445
+ }
2446
+ }
2447
+ }
2448
+ return {
2449
+ nextPluginErrorCount,
2450
+ nextPluginWarningCount
2451
+ };
2452
+ }
2453
+ __name(pluginCount, "pluginCount");
2454
+ function formatMessage(dir, messages, filePath) {
2455
+ let fileName = path.posix.normalize(path.relative(dir, filePath).replace(/\\/g, "/"));
2456
+ if (!fileName.startsWith(".")) {
2457
+ fileName = `./${fileName}`;
2458
+ }
2459
+ let output = `
2460
+ ${fileName}`;
2461
+ for (let i = 0; i < messages.length; i++) {
2462
+ const { message, severity, line, column, ruleId } = messages[i];
2463
+ output += "\n";
2464
+ if (line && column) {
2465
+ output = `${output + line.toString()}:${column.toString()} `;
2466
+ }
2467
+ if (severity === 1) {
2468
+ output += `Warning: `;
2469
+ } else {
2470
+ output += `Error: `;
2471
+ }
2472
+ output += message;
2473
+ if (ruleId) {
2474
+ output += ` ${ruleId}`;
2475
+ }
2476
+ }
2477
+ return output;
2478
+ }
2479
+ __name(formatMessage, "formatMessage");
2480
+ async function formatResults(baseDir, results, format3) {
2481
+ let totalPluginErrorCount = 0;
2482
+ let totalPluginWarningCount = 0;
2483
+ const resultsWithMessages = results.filter(({ messages }) => messages?.length);
2484
+ resultsWithMessages.forEach(({ messages }) => {
2485
+ const res = pluginCount(messages);
2486
+ totalPluginErrorCount += res.nextPluginErrorCount;
2487
+ totalPluginWarningCount += res.nextPluginWarningCount;
2488
+ });
2489
+ const output = format3 ? await format3(resultsWithMessages) : resultsWithMessages.map(({ messages, filePath }) => formatMessage(baseDir, messages, filePath)).join("\n");
2490
+ return {
2491
+ output,
2492
+ outputWithMessages: resultsWithMessages.length > 0 ? `${output}
2493
+
2494
+ Info - Need to disable some ESLint rules? Learn more here: https://nextjs.org/docs/app/api-reference/config/eslint#disabling-rules` : "",
2495
+ totalPluginErrorCount,
2496
+ totalPluginWarningCount
2497
+ };
2498
+ }
2499
+ __name(formatResults, "formatResults");
2500
+ async function writeDefaultEslintConfig(log, context, type = "recommended") {
2501
+ const eslintConfigFile = joinPaths(context.workspaceConfig.workspaceRoot, "eslint.config.js");
2502
+ const eslintConfig = `
2503
+ import { getConfig } from "eslint-config-powerlines";
2504
+
2505
+ Error.stackTraceLimit = Number.POSITIVE_INFINITY;
2506
+
2507
+ export default getConfig({
2508
+ repositoryName: "${context.workspaceConfig.name || context.config.name || "powerlines"}",
2509
+ "powerlines": "${type}",
2510
+ });
2511
+ `;
2512
+ log(LogLevelLabel.INFO, `Writing a default ESLint config file to ${eslintConfigFile}`);
2513
+ return writeFile2(log, eslintConfigFile, eslintConfig);
2514
+ }
2515
+ __name(writeDefaultEslintConfig, "writeDefaultEslintConfig");
2516
+ var VALID_SEVERITY = [
2517
+ "off",
2518
+ "warn",
2519
+ "error"
2520
+ ];
2521
+ function isValidSeverity(severity) {
2522
+ return VALID_SEVERITY.includes(severity);
2523
+ }
2524
+ __name(isValidSeverity, "isValidSeverity");
2525
+ async function writeOutputFile(log, outputFile, outputData) {
2526
+ const filePath = path.resolve(process.cwd(), outputFile);
2527
+ if (isDirectory(filePath)) {
2528
+ log(LogLevelLabel.ERROR, `Cannot write to output file path, it is a directory: ${filePath}`);
2529
+ } else {
2530
+ try {
2531
+ await writeFile2(log, filePath, outputData);
2532
+ log(LogLevelLabel.INFO, `The output file has been created: ${filePath}`);
2533
+ } catch (err) {
2534
+ log(LogLevelLabel.ERROR, `There was a problem writing the output file: ${filePath}`);
2535
+ console.error(err);
2536
+ }
2537
+ }
2538
+ }
2539
+ __name(writeOutputFile, "writeOutputFile");
2540
+ async function hasEslintConfiguration(eslintFile, packageJsonConfig) {
2541
+ const configObject = {
2542
+ exists: false,
2543
+ emptyEslint: false,
2544
+ emptyPkgJsonConfig: false
2545
+ };
2546
+ if (eslintFile) {
2547
+ const content = await readFile(eslintFile).then((txt) => txt.trim().replace(/\n/g, ""), () => null);
2548
+ if (content === "" || content === "{}" || content === "---" || content === "module.exports = {}") {
2549
+ configObject.emptyEslint = true;
2550
+ } else {
2551
+ configObject.exists = true;
2552
+ }
2553
+ } else if (packageJsonConfig?.eslintConfig) {
2554
+ if (Object.keys(packageJsonConfig.eslintConfig).length) {
2555
+ configObject.exists = true;
2556
+ } else {
2557
+ configObject.emptyPkgJsonConfig = true;
2558
+ }
2559
+ }
2560
+ return configObject;
2561
+ }
2562
+ __name(hasEslintConfiguration, "hasEslintConfiguration");
2563
+ var lint = /* @__PURE__ */ __name(async (log, context, eslintConfigPath, { lintDuringBuild = false, eslintOptions = null, reportErrorsOnly = false, maxWarnings = -1, formatter = null, outputFile = null }) => {
2564
+ try {
2565
+ await installPackages(context, [
2566
+ {
2567
+ name: "eslint",
2568
+ dev: true
2569
+ },
2570
+ {
2571
+ name: "eslint-config-powerlines",
2572
+ dev: true
2573
+ }
2574
+ ]);
2575
+ const isInstalled = isPackageExists("eslint", {
2576
+ paths: [
2577
+ context.workspaceConfig.workspaceRoot,
2578
+ context.config.projectRoot
2579
+ ]
2580
+ });
2581
+ if (!isInstalled) {
2582
+ log(LogLevelLabel.ERROR, `ESLint must be installed${lintDuringBuild ? " in order to run during builds:" : ":"} npm install --save-dev eslint`);
2583
+ return null;
2584
+ }
2585
+ const module = await context.resolver.import(context.resolver.esmResolve("eslint"));
2586
+ const useFlatConfig = eslintConfigPath ? findFileName(eslintConfigPath).startsWith("eslint.config.") : false;
2587
+ let ESLint;
2588
+ if ("loadESLint" in module) {
2589
+ ESLint = await module.loadESLint({
2590
+ useFlatConfig
2591
+ });
2592
+ }
2593
+ const eslintVersion = parseVersion(ESLint?.version);
2594
+ if (!eslintVersion || eslintVersion.compare("8.57.0") < 0) {
2595
+ return `Error - Your project has an older version of ESLint installed${eslintVersion ? ` (${eslintVersion.major}.${eslintVersion.minor}.${eslintVersion.patch})` : ""}. Please upgrade to ESLint version 8.57.0 or above`;
2596
+ }
2597
+ const options = {
2598
+ useEslintrc: true,
2599
+ baseConfig: {},
2600
+ errorOnUnmatchedPattern: false,
2601
+ extensions: [
2602
+ ".js",
2603
+ ".jsx",
2604
+ ".ts",
2605
+ ".tsx"
2606
+ ],
2607
+ cache: true,
2608
+ ...eslintOptions
2609
+ };
2610
+ if (eslintVersion?.compare("9.0.0") && useFlatConfig) {
2611
+ for (const option of [
2612
+ "useEslintrc",
2613
+ "extensions",
2614
+ "ignorePath",
2615
+ "reportUnusedDisableDirectives",
2616
+ "resolvePluginsRelativeTo",
2617
+ "rulePaths",
2618
+ "inlineConfig",
2619
+ "maxWarnings"
2620
+ ]) {
2621
+ if (option in options) {
2622
+ delete options[option];
2623
+ }
2624
+ }
2625
+ }
2626
+ let eslint2 = new ESLint(options);
2627
+ let stormStackEslintPluginIsEnabled = false;
2628
+ const stormStackRulesEnabled = /* @__PURE__ */ new Map();
2629
+ for (const configFile of [
2630
+ eslintConfigPath,
2631
+ joinPaths(context.config.projectRoot, "package.json")
2632
+ ]) {
2633
+ if (!configFile) continue;
2634
+ const completeConfig = await eslint2.calculateConfigForFile(configFile);
2635
+ if (!completeConfig) continue;
2636
+ const plugins2 = completeConfig.plugins;
2637
+ const hasStormStackPlugin = (
2638
+ // in ESLint < 9, `plugins` value is string[]
2639
+ Array.isArray(plugins2) ? plugins2.includes("powerlines") : "powerlines" in plugins2
2640
+ );
2641
+ if (hasStormStackPlugin) {
2642
+ stormStackEslintPluginIsEnabled = true;
2643
+ for (const [name, [severity]] of Object.entries(completeConfig.rules)) {
2644
+ if (!name.startsWith("powerlines/")) {
2645
+ continue;
2646
+ }
2647
+ if (typeof severity === "number" && severity >= 0 && severity < VALID_SEVERITY.length) {
2648
+ stormStackRulesEnabled.set(name, VALID_SEVERITY[severity]);
2649
+ } else if (typeof severity === "string" && isValidSeverity(severity)) {
2650
+ stormStackRulesEnabled.set(name, severity);
2651
+ }
2652
+ }
2653
+ break;
2654
+ }
2655
+ }
2656
+ if (stormStackEslintPluginIsEnabled) {
2657
+ eslint2 = new ESLint(options);
2658
+ } else {
2659
+ log(LogLevelLabel.WARN, "The Powerlines plugin was not detected in your ESLint configuration. See https://nextjs.org/docs/app/api-reference/config/eslint#migrating-existing-config");
2660
+ }
2661
+ const lintStart = process.hrtime();
2662
+ let results = await eslint2.lintFiles(context.tsconfig.fileNames.filter((fileName) => !fileName.includes(context.artifactsPath) && !fileName.includes("node_modules")));
2663
+ let selectedFormatter = null;
2664
+ if (options.fix) {
2665
+ await ESLint.outputFixes(results);
2666
+ }
2667
+ if (reportErrorsOnly) {
2668
+ results = ESLint.getErrorResults(results);
2669
+ }
2670
+ if (formatter) {
2671
+ selectedFormatter = await eslint2.loadFormatter(formatter);
2672
+ }
2673
+ const formattedResult = await formatResults(context.config.projectRoot, results, selectedFormatter?.format?.bind(selectedFormatter));
2674
+ const lintEnd = process.hrtime(lintStart);
2675
+ const totalWarnings = results.reduce((sum, file) => sum + file.warningCount, 0);
2676
+ if (outputFile) {
2677
+ await writeOutputFile(log, outputFile, formattedResult.output);
2678
+ }
2679
+ return {
2680
+ output: formattedResult.outputWithMessages,
2681
+ isError: ESLint.getErrorResults(results)?.length > 0 || maxWarnings >= 0 && totalWarnings > maxWarnings,
2682
+ eventInfo: {
2683
+ durationInSeconds: lintEnd[0],
2684
+ eslintVersion: eslintVersion.version,
2685
+ lintedFilesCount: results.length,
2686
+ lintFix: !!options.fix,
2687
+ eslintPluginErrorsCount: formattedResult.totalPluginErrorCount,
2688
+ eslintPluginWarningsCount: formattedResult.totalPluginWarningCount,
2689
+ stormStackRulesEnabled: Object.fromEntries(stormStackRulesEnabled)
2690
+ }
2691
+ };
2692
+ } catch (err) {
2693
+ if (lintDuringBuild) {
2694
+ log(LogLevelLabel.ERROR, `ESLint: ${isError(err) && err.message ? err.message.replace(/\n/g, " ") : String(err)}`);
2695
+ return null;
2696
+ } else {
2697
+ throw err;
2698
+ }
2699
+ }
2700
+ }, "lint");
2701
+ async function eslint(context, lintDuringBuild = false, opts = {}) {
2702
+ const { eslintOptions = null, reportErrorsOnly = false, maxWarnings = -1, formatter = null, outputFile = null, type = "recommended" } = opts;
2703
+ const eslintFile = getParentPath([
2704
+ // eslint v9
2705
+ "eslint.config.js",
2706
+ "eslint.config.mjs",
2707
+ "eslint.config.cjs",
2708
+ // TS extensions require to install a separate package `jiti`.
2709
+ // https://eslint.org/docs/latest/use/configure/configuration-files#typescript-configuration-files
2710
+ "eslint.config.ts",
2711
+ "eslint.config.mts",
2712
+ "eslint.config.cts",
2713
+ // eslint <= v8
2714
+ ".eslintrc.js",
2715
+ ".eslintrc.cjs",
2716
+ ".eslintrc.yaml",
2717
+ ".eslintrc.yml",
2718
+ ".eslintrc.json",
2719
+ ".eslintrc"
2720
+ ], context.config.projectRoot) ?? null;
2721
+ const config = await hasEslintConfiguration(eslintFile, context.packageJson);
2722
+ if (config.exists) {
2723
+ return lint(context.log, context, eslintFile, {
2724
+ lintDuringBuild,
2725
+ eslintOptions,
2726
+ reportErrorsOnly,
2727
+ maxWarnings,
2728
+ formatter,
2729
+ outputFile
2730
+ });
2731
+ }
2732
+ if (lintDuringBuild) {
2733
+ if (config.emptyPkgJsonConfig || config.emptyEslint) {
2734
+ context.log(LogLevelLabel.WARN, `No ESLint configuration detected. Run "storm lint" to begin setup`);
2735
+ }
2736
+ return null;
2737
+ } else {
2738
+ const isEslintInstalled = isPackageExists("eslint", {
2739
+ paths: [
2740
+ context.workspaceConfig.workspaceRoot,
2741
+ context.config.projectRoot
2742
+ ]
2743
+ });
2744
+ const isEslintPluginInstalled = isPackageExists("eslint-config-powerlines", {
2745
+ paths: [
2746
+ context.workspaceConfig.workspaceRoot,
2747
+ context.config.projectRoot
2748
+ ]
2749
+ });
2750
+ if (!isEslintInstalled || !isEslintPluginInstalled) {
2751
+ await installPackages(context, [
2752
+ {
2753
+ name: "eslint",
2754
+ dev: true
2755
+ },
2756
+ {
2757
+ name: "eslint-config-powerlines",
2758
+ dev: true
2759
+ }
2760
+ ]);
2761
+ }
2762
+ await writeDefaultEslintConfig(context.log, context, type);
2763
+ }
2764
+ context.log(LogLevelLabel.SUCCESS, `ESLint has successfully been configured. Run "storm lint" again to view warnings and errors.`);
2765
+ return null;
2766
+ }
2767
+ __name(eslint, "eslint");
2768
+ function getString(code) {
2769
+ if (!code) {
2770
+ return "";
2771
+ }
2772
+ if (isString(code)) {
2773
+ return code;
2774
+ }
2775
+ if (isSetObject(code) && "code" in code) {
2776
+ return code.code;
2777
+ }
2778
+ return code.toString();
2779
+ }
2780
+ __name(getString, "getString");
2781
+ function getMagicString(code) {
2782
+ if (isString(code)) {
2783
+ return new MagicString(code);
2784
+ }
2785
+ return code;
2786
+ }
2787
+ __name(getMagicString, "getMagicString");
2788
+ function getSourceFile(code, id) {
2789
+ const content = code ?? readFileIfExistingSync(id);
2790
+ return {
2791
+ id,
2792
+ code: getMagicString(content),
2793
+ env: []
2794
+ };
2795
+ }
2796
+ __name(getSourceFile, "getSourceFile");
2797
+ function resolveModulePath(nodePath, state) {
2798
+ if (!t.isStringLiteral(nodePath.node)) {
2799
+ return;
2800
+ }
2801
+ const sourcePath = nodePath.node.value;
2802
+ const resolvedPath = state.context?.fs.resolvePath(sourcePath);
2803
+ if (resolvedPath) {
2804
+ nodePath.replaceWith(t.stringLiteral(
2805
+ // Remove the file extension if it exists
2806
+ resolvedPath.replace(/\.(?:ts|mts|cts)x?$/, "")
2807
+ ));
2808
+ }
2809
+ }
2810
+ __name(resolveModulePath, "resolveModulePath");
2811
+ var TRANSFORM_FUNCTIONS = [
2812
+ "require",
2813
+ "require.resolve",
2814
+ "System.import",
2815
+ // Jest methods
2816
+ "jest.genMockFromModule",
2817
+ "jest.mock",
2818
+ "jest.unmock",
2819
+ "jest.doMock",
2820
+ // eslint-disable-next-line @cspell/spellchecker
2821
+ "jest.dontMock",
2822
+ "jest.setMock",
2823
+ "jest.requireActual",
2824
+ "jest.requireMock",
2825
+ // Older Jest methods
2826
+ "require.requireActual",
2827
+ "require.requireMock"
2828
+ ];
2829
+ function matchesPattern(state, calleePath, pattern) {
2830
+ const { node } = calleePath;
2831
+ if (t.isMemberExpression(node)) {
2832
+ return calleePath.matchesPattern(pattern);
2833
+ }
2834
+ if (!t.isIdentifier(node) || pattern.includes(".")) {
2835
+ return false;
2836
+ }
2837
+ const name = pattern.split(".")[0];
2838
+ return node.name === name;
2839
+ }
2840
+ __name(matchesPattern, "matchesPattern");
2841
+ var importVisitors = {
2842
+ CallExpression: /* @__PURE__ */ __name((nodePath, state) => {
2843
+ if (state.moduleResolverVisited.has(nodePath)) {
2844
+ return;
2845
+ }
2846
+ const calleePath = nodePath.get("callee");
2847
+ if (calleePath && TRANSFORM_FUNCTIONS.some((pattern) => matchesPattern(state, calleePath, pattern)) || t.isImport(nodePath.node.callee)) {
2848
+ state.moduleResolverVisited.add(nodePath);
2849
+ resolveModulePath(nodePath.get("arguments.0"), state);
2850
+ }
2851
+ }, "CallExpression"),
2852
+ // eslint-disable-next-line ts/naming-convention
2853
+ "ImportDeclaration|ExportDeclaration|ExportAllDeclaration": /* @__PURE__ */ __name((nodePath, state) => {
2854
+ if (!nodePath || !nodePath.get("source") || state.moduleResolverVisited.has(nodePath)) {
2855
+ return;
2856
+ }
2857
+ state.moduleResolverVisited.add(nodePath);
2858
+ resolveModulePath(nodePath.get("source"), state);
2859
+ }, "ImportDeclaration|ExportDeclaration|ExportAllDeclaration")
2860
+ };
2861
+ var moduleResolverBabelPlugin = /* @__PURE__ */ __name((context) => {
2862
+ return declare(/* @__PURE__ */ __name(function builder(api) {
2863
+ let moduleResolverVisited = /* @__PURE__ */ new Set();
2864
+ return {
2865
+ name: "powerlines:module-resolver",
2866
+ manipulateOptions(opts) {
2867
+ opts.filename ??= "unknown";
2868
+ },
2869
+ pre() {
2870
+ moduleResolverVisited = /* @__PURE__ */ new Set();
2871
+ },
2872
+ visitor: {
2873
+ Program: {
2874
+ enter(programPath, state) {
2875
+ programPath.traverse(importVisitors, {
2876
+ ...state,
2877
+ context,
2878
+ moduleResolverVisited,
2879
+ api
2880
+ });
2881
+ },
2882
+ exit(programPath, state) {
2883
+ programPath.traverse(importVisitors, {
2884
+ ...state,
2885
+ context,
2886
+ moduleResolverVisited,
2887
+ api
2888
+ });
2889
+ }
2890
+ }
2891
+ },
2892
+ post() {
2893
+ moduleResolverVisited.clear();
2894
+ }
2895
+ };
2896
+ }, "builder"));
2897
+ }, "moduleResolverBabelPlugin");
2898
+
2899
+ // ../powerlines/src/internal/helpers/generate-types.ts
2900
+ async function generateTypes(context) {
2901
+ context.log(LogLevelLabel.TRACE, `Preparing the TypeScript definitions for the Powerlines project.`);
2902
+ context.log(LogLevelLabel.TRACE, "Transforming built-ins runtime modules files.");
2903
+ const builtinFiles = await Promise.all((await context.fs.listBuiltinFiles()).filter((file) => !context.fs.isMatchingBuiltinId("index", file.id)).map(async (file) => {
2904
+ const result = await transformAsync(file.contents, {
2905
+ highlightCode: true,
2906
+ code: true,
2907
+ ast: false,
2908
+ cloneInputAst: false,
2909
+ comments: true,
2910
+ sourceType: "module",
2911
+ configFile: false,
2912
+ babelrc: false,
2913
+ envName: context.config.mode,
2914
+ caller: {
2915
+ name: "powerlines"
2916
+ },
2917
+ ...context.config.transform.babel,
2918
+ filename: file.path,
2919
+ plugins: [
2920
+ [
2921
+ "@babel/plugin-syntax-typescript"
2922
+ ],
2923
+ [
2924
+ moduleResolverBabelPlugin(context)
2925
+ ]
2926
+ ]
2927
+ });
2928
+ if (!result?.code) {
2929
+ throw new Error(`Powerlines - Generate Types failed to compile ${file.id}`);
2930
+ }
2931
+ context.log(LogLevelLabel.TRACE, `Writing transformed built-in runtime file ${file.id}.`);
2932
+ await context.fs.writeBuiltinFile(file.id, file.path, result.code);
2933
+ return file.path;
2934
+ }));
2935
+ const typescriptPath = await resolvePackage("typescript");
2936
+ if (!typescriptPath) {
2937
+ throw new Error("Could not resolve TypeScript package location. Please ensure TypeScript is installed.");
2938
+ }
2939
+ const files = builtinFiles.reduce((ret, fileName) => {
2940
+ const formatted = replacePath(fileName, context.workspaceConfig.workspaceRoot);
2941
+ if (!ret.includes(formatted)) {
2942
+ ret.push(formatted);
2943
+ }
2944
+ return ret;
2945
+ }, [
2946
+ joinPaths(typescriptPath, "lib", "lib.esnext.full.d.ts")
2947
+ ]);
2948
+ context.log(LogLevelLabel.TRACE, "Parsing TypeScript configuration for the Powerlines project.");
2949
+ const resolvedTsconfig = getParsedTypeScriptConfig(context.workspaceConfig.workspaceRoot, context.config.projectRoot, context.tsconfig.tsconfigFilePath, defu4({
2950
+ compilerOptions: {
2951
+ strict: false,
2952
+ noEmit: false,
2953
+ declaration: true,
2954
+ declarationMap: false,
2955
+ emitDeclarationOnly: true,
2956
+ skipLibCheck: true
2957
+ },
2958
+ exclude: [
2959
+ "node_modules",
2960
+ "dist"
2961
+ ],
2962
+ include: files
2963
+ }, context.config.tsconfigRaw ?? {}));
2964
+ resolvedTsconfig.options.configFilePath = joinPaths(context.workspaceConfig.workspaceRoot, context.tsconfig.tsconfigFilePath);
2965
+ resolvedTsconfig.options.pathsBasePath = context.workspaceConfig.workspaceRoot;
2966
+ resolvedTsconfig.options.suppressOutputPathCheck = true;
2967
+ context.log(LogLevelLabel.TRACE, "Creating the TypeScript compiler host");
2968
+ const program = createProgram(files, resolvedTsconfig.options, createCompilerHost(resolvedTsconfig.options));
2969
+ context.log(LogLevelLabel.TRACE, `Running TypeScript compiler on ${builtinFiles.length} built-in runtime files.`);
2970
+ let builtinModules = "";
2971
+ const emitResult = program.emit(void 0, (fileName, text, _, __, sourceFiles, _data) => {
2972
+ const sourceFile2 = sourceFiles?.[0];
2973
+ if (sourceFile2?.fileName && !fileName.endsWith(".map")) {
2974
+ if (context.fs.isBuiltinFile(sourceFile2.fileName)) {
2975
+ builtinModules += `
2976
+ declare module "${context.fs.resolveId(sourceFile2.fileName)}" {
2977
+ ${text.trim().replace(/^\s*export\s*declare\s*/gm, "export ").replace(/^\s*declare\s*/gm, "")}
2978
+ }
2979
+ `;
2980
+ }
2981
+ }
2982
+ }, void 0, true);
2983
+ const diagnostics = getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
2984
+ const diagnosticMessages = [];
2985
+ diagnostics.forEach((diagnostic) => {
2986
+ if (diagnostic.file) {
2987
+ const { line, character } = getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start);
2988
+ const message = flattenDiagnosticMessageText(diagnostic.messageText, "\n");
2989
+ diagnosticMessages.push(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
2990
+ } else {
2991
+ const message = flattenDiagnosticMessageText(diagnostic.messageText, "\n");
2992
+ diagnosticMessages.push(message);
2993
+ }
2994
+ });
2995
+ const diagnosticMessage = diagnosticMessages.join("\n");
2996
+ if (diagnosticMessage) {
2997
+ throw new Error(`TypeScript compilation failed:
2998
+
2999
+ ${diagnosticMessage.length > 5e3 ? `${diagnosticMessage.slice(0, 5e3)}...` : diagnosticMessage}`);
3000
+ }
3001
+ context.log(LogLevelLabel.TRACE, `Generating TypeScript declaration file in ${context.config.output.dts}.`);
3002
+ const sourceFile = getSourceFile(String(context.config.output.dts), `/// <reference types="powerlines/shared" />${context.config.build.platform !== "neutral" ? `
3003
+ /// <reference types="powerlines/${context.config.build.platform}" />` : ""}
3004
+
3005
+ ${getFileHeader(null, false)}
3006
+
3007
+ ${builtinModules}`.replace(
3008
+ // eslint-disable-next-line regexp/no-super-linear-backtracking
3009
+ /import\s*(?:type\s*)?\{?[\w,\s]*(?:\}\s*)?from\s*(?:'|")@?[a-zA-Z0-9-\\/.]*(?:'|");?/g,
3010
+ ""
3011
+ ).replaceAll("#private;", "").replace(/__Ω/g, ""));
3012
+ await context.fs.writeFileToDisk(sourceFile.id, getString(sourceFile.code));
3013
+ }
3014
+ __name(generateTypes, "generateTypes");
3015
+ async function installDependencies(context) {
3016
+ context.log(LogLevelLabel.TRACE, `Checking and installing missing project dependencies.`);
3017
+ context.dependencies ??= {};
3018
+ context.dependencies["powerlines"] = {
3019
+ type: "dependency"
3020
+ };
3021
+ if (context.config.projectType === "application") {
3022
+ context.dependencies.unstorage = {
3023
+ type: "dependency"
3024
+ };
3025
+ }
3026
+ context.log(LogLevelLabel.TRACE, `The following packages must be installed as dependencies:
3027
+ ${Object.keys(context.dependencies).map((key) => ` - ${getPackageName(key)}${hasPackageVersion(key) || isSetObject(context.dependencies[key]) && isSetString(context.dependencies[key].version) ? ` v${isSetObject(context.dependencies[key]) && isSetString(context.dependencies[key].version) ? context.dependencies[key].version : getPackageVersion(key)}` : ""} (${(isString(context.dependencies[key]) ? context.dependencies[key] : context.dependencies[key]?.type) || "dependency"})`).join("\n")}`);
3028
+ for (const [key, value] of Object.entries(context.dependencies)) {
3029
+ const version = isSetObject(value) && isValidRange(value.version) && value.version || getPackageVersion(key);
3030
+ await installPackage(context, version ? `${getPackageName(key)}@${String(version)}` : getPackageName(key), (isSetString(value) ? value : value.type) === "devDependency");
3031
+ }
3032
+ }
3033
+ __name(installDependencies, "installDependencies");
3034
+ async function resolveTsconfigChanges(context) {
3035
+ const tsconfig = getParsedTypeScriptConfig(context.workspaceConfig.workspaceRoot, context.config.projectRoot, context.config.tsconfig, context.config.tsconfigRaw);
3036
+ const tsconfigFilePath = getTsconfigFilePath(context.workspaceConfig.workspaceRoot, context.config.projectRoot, context.config.tsconfig);
3037
+ const tsconfigJson = await readJsonFile(tsconfigFilePath);
3038
+ tsconfigJson.compilerOptions ??= {};
3039
+ const extendedTsconfig = await loadTsConfig(tsconfigFilePath);
3040
+ extendedTsconfig.compilerOptions ??= {};
3041
+ if (tsconfigJson.reflection !== true) {
3042
+ tsconfigJson.reflection = true;
3043
+ }
3044
+ if (tsconfig.options.experimentalDecorators !== true) {
3045
+ tsconfigJson.compilerOptions.experimentalDecorators = true;
3046
+ }
3047
+ if (tsconfig.options.emitDecoratorMetadata !== true) {
3048
+ tsconfigJson.compilerOptions.emitDecoratorMetadata = true;
3049
+ }
3050
+ if (context.config.output.dts) {
3051
+ const dtsFilePath = context.config.output.dts ? context.config.output.dts.startsWith(context.workspaceConfig.workspaceRoot) ? context.config.output.dts : joinPaths(context.workspaceConfig.workspaceRoot, context.config.output.dts) : joinPaths(context.workspaceConfig.workspaceRoot, context.config.projectRoot, "storm.d.ts");
3052
+ const dtsRelativePath = joinPaths(relativePath(joinPaths(context.workspaceConfig.workspaceRoot, context.config.projectRoot), findFilePath(dtsFilePath)), findFileName(dtsFilePath));
3053
+ if (!tsconfigJson.include?.some((filePattern) => isIncludeMatchFound(filePattern, [
3054
+ dtsFilePath,
3055
+ dtsRelativePath,
3056
+ "storm.d.ts"
3057
+ ]))) {
3058
+ tsconfigJson.include ??= [];
3059
+ tsconfigJson.include.push(dtsRelativePath.startsWith("./") ? dtsRelativePath.slice(2) : dtsRelativePath);
3060
+ }
3061
+ }
3062
+ if (!tsconfig.options.lib?.some((lib) => [
3063
+ "lib.esnext.d.ts",
3064
+ "lib.es2021.d.ts",
3065
+ "lib.es2022.d.ts",
3066
+ "lib.es2023.d.ts"
3067
+ ].includes(lib.toLowerCase()))) {
3068
+ tsconfigJson.compilerOptions.lib ??= [];
3069
+ tsconfigJson.compilerOptions.lib.push("esnext");
3070
+ }
3071
+ if (tsconfig.options.module !== ts3.ModuleKind.ESNext) {
3072
+ tsconfigJson.compilerOptions.module = "ESNext";
3073
+ }
3074
+ if (!tsconfig.options.target || ![
3075
+ ts3.ScriptTarget.ESNext,
3076
+ ts3.ScriptTarget.ES2024,
3077
+ ts3.ScriptTarget.ES2023,
3078
+ ts3.ScriptTarget.ES2022,
3079
+ ts3.ScriptTarget.ES2021
3080
+ ].includes(tsconfig.options.target)) {
3081
+ tsconfigJson.compilerOptions.target = "ESNext";
3082
+ }
3083
+ if (tsconfig.options.moduleResolution !== ts3.ModuleResolutionKind.Bundler) {
3084
+ tsconfigJson.compilerOptions.moduleResolution = "Bundler";
3085
+ }
3086
+ if (tsconfig.options.moduleDetection !== ts3.ModuleDetectionKind.Force) {
3087
+ tsconfigJson.compilerOptions.moduleDetection = "force";
3088
+ }
3089
+ if (tsconfig.options.allowSyntheticDefaultImports !== true) {
3090
+ tsconfigJson.compilerOptions.allowSyntheticDefaultImports = true;
3091
+ }
3092
+ if (tsconfig.options.noImplicitOverride !== true) {
3093
+ tsconfigJson.compilerOptions.noImplicitOverride = true;
3094
+ }
3095
+ if (tsconfig.options.noUncheckedIndexedAccess !== true) {
3096
+ tsconfigJson.compilerOptions.noUncheckedIndexedAccess = true;
3097
+ }
3098
+ if (tsconfig.options.skipLibCheck !== true) {
3099
+ tsconfigJson.compilerOptions.skipLibCheck = true;
3100
+ }
3101
+ if (tsconfig.options.resolveJsonModule !== true) {
3102
+ tsconfigJson.compilerOptions.resolveJsonModule = true;
3103
+ }
3104
+ if (tsconfig.options.isolatedModules !== true) {
3105
+ tsconfigJson.compilerOptions.isolatedModules = true;
3106
+ }
3107
+ if (tsconfig.options.verbatimModuleSyntax !== false) {
3108
+ tsconfigJson.compilerOptions.verbatimModuleSyntax = false;
3109
+ }
3110
+ if (tsconfig.options.allowJs !== true) {
3111
+ tsconfigJson.compilerOptions.allowJs = true;
3112
+ }
3113
+ if (tsconfig.options.esModuleInterop !== true) {
3114
+ tsconfigJson.compilerOptions.esModuleInterop = true;
3115
+ }
3116
+ if (tsconfig.options.declaration !== true) {
3117
+ tsconfigJson.compilerOptions.declaration = true;
3118
+ }
3119
+ if (context.environment.consumer === "client") {
3120
+ if (tsconfig.options.jsx !== ts3.JsxEmit.ReactJSX) {
3121
+ tsconfigJson.compilerOptions.jsx = "react-jsx";
3122
+ }
3123
+ if (!tsconfig.options.lib?.some((lib) => lib.toLowerCase() !== "dom")) {
3124
+ tsconfigJson.compilerOptions.lib ??= [];
3125
+ tsconfigJson.compilerOptions.lib.push("dom");
3126
+ }
3127
+ if (!tsconfig.options.lib?.some((lib) => lib.toLowerCase() !== "dom.iterable")) {
3128
+ tsconfigJson.compilerOptions.lib ??= [];
3129
+ tsconfigJson.compilerOptions.lib.push("dom.iterable");
3130
+ }
3131
+ } else if (context.config.build.platform === "node") {
3132
+ if (!tsconfig.options.types?.some((type) => type.toLowerCase() === "node" || type.toLowerCase() === "@types/node")) {
3133
+ tsconfigJson.compilerOptions.types ??= [];
3134
+ tsconfigJson.compilerOptions.types.push("node");
3135
+ }
3136
+ }
3137
+ return tsconfigJson;
3138
+ }
3139
+ __name(resolveTsconfigChanges, "resolveTsconfigChanges");
3140
+ async function initializeTsconfig(context) {
3141
+ context.log(LogLevelLabel.TRACE, "Initializing TypeScript configuration (tsconfig.json) for the Powerlines project.");
3142
+ if (!isPackageExists("typescript")) {
3143
+ throw new Error('The TypeScript package is not installed. Please install the package using the command: "npm install typescript --save-dev"');
3144
+ }
3145
+ const tsconfigFilePath = getTsconfigFilePath(context.workspaceConfig.workspaceRoot, context.config.projectRoot, context.config.tsconfig);
3146
+ context.tsconfig.originalTsconfigJson = await readJsonFile(tsconfigFilePath);
3147
+ context.tsconfig.tsconfigJson = await resolveTsconfigChanges(context);
3148
+ await context.fs.writeFileToDisk(tsconfigFilePath, StormJSON.stringify(context.tsconfig.tsconfigJson));
3149
+ context.tsconfig = getParsedTypeScriptConfig(context.workspaceConfig.workspaceRoot, context.config.projectRoot, context.config.tsconfig, context.config.tsconfigRaw, context.tsconfig.originalTsconfigJson);
3150
+ }
3151
+ __name(initializeTsconfig, "initializeTsconfig");
3152
+ async function resolveTsconfig(context) {
3153
+ const updateTsconfigJson = await readJsonFile(context.tsconfig.tsconfigFilePath);
3154
+ if (updateTsconfigJson?.compilerOptions?.types && Array.isArray(updateTsconfigJson.compilerOptions.types) && !updateTsconfigJson.compilerOptions.types.length) {
3155
+ delete updateTsconfigJson.compilerOptions.types;
3156
+ }
3157
+ const result = getObjectDiff(context.tsconfig.originalTsconfigJson, updateTsconfigJson, {
3158
+ ignoreArrayOrder: true,
3159
+ showOnly: {
3160
+ statuses: [
3161
+ "added",
3162
+ "deleted",
3163
+ "updated"
3164
+ ],
3165
+ granularity: "deep"
3166
+ }
3167
+ });
3168
+ const changes = [];
3169
+ const getChanges = /* @__PURE__ */ __name((difference, property) => {
3170
+ if (difference.status === "added" || difference.status === "deleted" || difference.status === "updated") {
3171
+ if (difference.diff) {
3172
+ for (const diff of difference.diff) {
3173
+ getChanges(diff, property ? `${property}.${difference.property}` : difference.property);
3174
+ }
3175
+ } else {
3176
+ changes.push({
3177
+ field: property ? `${property}.${difference.property}` : difference.property,
3178
+ status: difference.status,
3179
+ previous: difference.status === "added" ? "---" : StormJSON.stringify(difference.previousValue),
3180
+ current: difference.status === "deleted" ? "---" : StormJSON.stringify(difference.currentValue)
3181
+ });
3182
+ }
3183
+ }
3184
+ }, "getChanges");
3185
+ for (const diff of result.diff) {
3186
+ getChanges(diff);
3187
+ }
3188
+ if (changes.length > 0) {
3189
+ context.log(LogLevelLabel.WARN, `Updating the following configuration values in "${context.tsconfig.tsconfigFilePath}" file:
3190
+
3191
+ ${changes.map((change, i) => `${chalk5.bold.whiteBright(`${i + 1}. ${titleCase(change.status)} the ${change.field} field: `)}
3192
+ ${chalk5.red(` - Previous: ${change.previous} `)}
3193
+ ${chalk5.green(` - Updated: ${change.current} `)}
3194
+ `).join("\n")}
3195
+ `);
3196
+ }
3197
+ await writeFile2(context.log, context.tsconfig.tsconfigFilePath, StormJSON.stringify(updateTsconfigJson));
3198
+ context.tsconfig = getParsedTypeScriptConfig(context.workspaceConfig.workspaceRoot, context.config.projectRoot, context.config.tsconfig);
3199
+ if (!context.tsconfig) {
3200
+ throw new Error("Failed to parse the TypeScript configuration file.");
3201
+ }
3202
+ context.tsconfig.tsconfigJson.compilerOptions ??= {};
3203
+ context.tsconfig.tsconfigJson.compilerOptions.strict = false;
3204
+ }
3205
+ __name(resolveTsconfig, "resolveTsconfig");
3206
+ function getDefaultCompilerOptions() {
3207
+ return {
3208
+ ...ts3.getDefaultCompilerOptions(),
3209
+ jsx: ts3.JsxEmit.React,
3210
+ strict: true,
3211
+ esModuleInterop: true,
3212
+ module: ts3.ModuleKind.ESNext,
3213
+ suppressOutputPathCheck: true,
3214
+ skipLibCheck: true,
3215
+ skipDefaultLibCheck: true,
3216
+ moduleResolution: ts3.ModuleResolutionKind.Node10
3217
+ };
3218
+ }
3219
+ __name(getDefaultCompilerOptions, "getDefaultCompilerOptions");
3220
+ async function createVirtualProgram(rootNames, context, compilerOptions = {}) {
3221
+ const options = defu4(compilerOptions, getDefaultCompilerOptions());
3222
+ const host = {
3223
+ name: "storm-vfs",
3224
+ root: context.workspaceConfig.workspaceRoot,
3225
+ ...ts3.sys,
3226
+ realpath: /* @__PURE__ */ __name((path2) => {
3227
+ if (context.fs.existsSync(path2)) {
3228
+ return context.fs.resolvePath(path2);
3229
+ }
3230
+ return ts3.sys.realpath?.(path2) ?? path2;
3231
+ }, "realpath"),
3232
+ getCurrentDirectory() {
3233
+ return context.workspaceConfig.workspaceRoot;
3234
+ },
3235
+ getCanonicalFileName(fileName) {
3236
+ return fileName;
3237
+ },
3238
+ getDefaultLibFileName(_options) {
3239
+ return ts3.getDefaultLibFileName(options);
3240
+ },
3241
+ getDefaultLibLocation() {
3242
+ return "/";
3243
+ },
3244
+ getNewLine() {
3245
+ return "\n";
3246
+ },
3247
+ useCaseSensitiveFileNames() {
3248
+ return true;
3249
+ },
3250
+ fileExists(fileName) {
3251
+ return context.fs.existsSync(fileName);
3252
+ },
3253
+ readFile(fileName) {
3254
+ if (context.fs.existsSync(fileName)) {
3255
+ return context.fs.readFileSync(fileName);
3256
+ }
3257
+ return void 0;
3258
+ },
3259
+ readDirectory: /* @__PURE__ */ __name((path2, extensions = [], exclude = [], include = []) => {
3260
+ let results = [];
3261
+ if (context.fs.existsSync(path2)) {
3262
+ results = context.fs.readdirSync(path2, {
3263
+ encoding: "utf8",
3264
+ recursive: true
3265
+ });
3266
+ if (extensions.length > 0) {
3267
+ results = results.filter((file) => extensions.some((ext) => file.endsWith(ext.startsWith(".") ? ext : `.${ext}`)));
3268
+ }
3269
+ if (exclude.length > 0) {
3270
+ results = results.filter((file) => !exclude.some((pattern) => minimatch(file, pattern)));
3271
+ }
3272
+ if (include.length > 0) {
3273
+ results = results.filter((file) => include.some((pattern) => minimatch(file, pattern)));
3274
+ }
3275
+ }
3276
+ return results;
3277
+ }, "readDirectory"),
3278
+ writeFile(fileName, data) {
3279
+ context.fs.writeFileSync(fileName, data);
3280
+ },
3281
+ resolvePath: /* @__PURE__ */ __name((fileName) => {
3282
+ if (context.fs.existsSync(fileName)) {
3283
+ return context.fs.resolvePath(fileName);
3284
+ }
3285
+ return ts3.sys.resolvePath(fileName);
3286
+ }, "resolvePath"),
3287
+ getSourceFile(fileName, languageVersionOrOptions, _, shouldCreateNewSourceFile) {
3288
+ if (context.fs.existsSync(fileName)) {
3289
+ return ts3.createSourceFile(fileName, context.fs.readFileSync(fileName), languageVersionOrOptions ?? compilerOptions.target ?? getDefaultCompilerOptions().target, false);
3290
+ } else if (shouldCreateNewSourceFile) {
3291
+ const sourceFile = ts3.createSourceFile(fileName, "", languageVersionOrOptions ?? compilerOptions.target ?? getDefaultCompilerOptions().target, false);
3292
+ context.fs.writeFileSync(fileName, sourceFile.text);
3293
+ return sourceFile;
3294
+ }
3295
+ return void 0;
3296
+ }
3297
+ };
3298
+ return ts3.createProgram(rootNames, options, host);
3299
+ }
3300
+ __name(createVirtualProgram, "createVirtualProgram");
3301
+
3302
+ // ../powerlines/src/internal/helpers/tsc.ts
3303
+ async function typeCheck(context, sources) {
3304
+ if (sources) {
3305
+ await Promise.all(sources.entries().map(async ([path2, content]) => context.fs.writeFile(path2, content)));
3306
+ }
3307
+ const program = await createVirtualProgram(sources ? Array.from(sources.keys()) : [], context, {
3308
+ noEmit: true,
3309
+ lib: [
3310
+ "lib.esnext.d.ts"
3311
+ ],
3312
+ types: []
3313
+ });
3314
+ const emitResult = program.emit();
3315
+ const allDiagnostics = ts3.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
3316
+ allDiagnostics.forEach((diagnostic) => {
3317
+ if (diagnostic.file) {
3318
+ const { line, character } = ts3.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start);
3319
+ const message = ts3.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
3320
+ context.log(LogLevelLabel.ERROR, `${diagnostic.file.fileName}:${line + 1}:${character + 1} : ${message}`);
3321
+ } else {
3322
+ context.log(LogLevelLabel.ERROR, ts3.flattenDiagnosticMessageText(diagnostic.messageText, "\n"));
3323
+ }
3324
+ });
3325
+ }
3326
+ __name(typeCheck, "typeCheck");
3327
+
3328
+ // ../powerlines/src/internal/api.ts
3329
+ var PowerlinesAPI = class _PowerlinesAPI {
3330
+ static {
3331
+ __name(this, "PowerlinesAPI");
3332
+ }
3333
+ /**
3334
+ * The Powerlines context
3335
+ */
3336
+ #context;
3337
+ /**
3338
+ * The Powerlines context
3339
+ */
3340
+ get context() {
3341
+ return this.#context;
3342
+ }
3343
+ /**
3344
+ * Create a new Powerlines API instance
3345
+ *
3346
+ * @param context - The Powerlines context
3347
+ */
3348
+ constructor(context) {
3349
+ this.#context = context;
3350
+ }
3351
+ /**
3352
+ * Initialize the Powerlines API
3353
+ */
3354
+ static async from(workspaceRoot, config) {
3355
+ const api = new _PowerlinesAPI(await PowerlinesAPIContext.from(workspaceRoot, config));
3356
+ for (const plugin of api.context.config.plugins ?? []) {
3357
+ await api.#addPlugin(plugin);
3358
+ }
3359
+ if (api.context.plugins.length === 0) {
3360
+ api.context.log(LogLevelLabel.WARN, "No Powerlines plugins were specified in the options. Please ensure this is correct, as it is generally not recommended.");
3361
+ }
3362
+ const pluginConfig = await callHook(await api.context.getEnvironment(), "config", {
3363
+ sequential: true,
3364
+ result: "merge"
3365
+ });
3366
+ await api.context.withUserConfig(pluginConfig, {
3367
+ isHighPriority: false
3368
+ });
3369
+ return api;
3370
+ }
3371
+ /**
3372
+ * Prepare the Powerlines API
3373
+ */
3374
+ async prepare(inlineConfig = {
3375
+ command: "prepare"
3376
+ }) {
3377
+ this.context.log(LogLevelLabel.TRACE, " \u{1F3D7}\uFE0F Preparing the Powerlines project");
3378
+ this.context.log(LogLevelLabel.TRACE, " \u2699\uFE0F Aggregating configuration options for the Powerlines project");
3379
+ await this.context.withInlineConfig(inlineConfig);
3380
+ await this.#executeEnvironments(async (context) => {
3381
+ context.log(LogLevelLabel.TRACE, `Initializing the processing options for the Powerlines project.`);
3382
+ await this.callPreHook(context, "configResolved");
3383
+ await initializeTsconfig(context);
3384
+ await this.callNormalHook(context, "configResolved");
3385
+ context.log(LogLevelLabel.DEBUG, `The configuration provided ${toArray(context.config.entry).length} entry point(s), Powerlines has found ${context.entry.length} entry files(s) for the ${context.config.title} project${context.entry.length > 0 && context.entry.length < 10 ? `:
3386
+ ${context.entry.map((entry) => `- ${entry.input.file || entry.file}${entry.output ? ` -> ${entry.output}` : ""}`).join(" \n")}` : ""}.`);
3387
+ await resolveTsconfig(context);
3388
+ await installDependencies(context);
3389
+ await this.callPostHook(context, "configResolved");
3390
+ context.log(LogLevelLabel.TRACE, `Powerlines configuration has been resolved:
3391
+
3392
+ ${formatLogMessage(context.config)}`);
3393
+ context.fs[__VFS_INIT__]();
3394
+ await writeMetaFile(context);
3395
+ context.persistedMeta = context.meta;
3396
+ if (!existsSync(context.cachePath)) {
3397
+ await createDirectory(context.cachePath);
3398
+ }
3399
+ if (!existsSync(context.dataPath)) {
3400
+ await createDirectory(context.dataPath);
3401
+ }
3402
+ await this.callPreHook(context, "prepare");
3403
+ if (context.config.projectType === "application") {
3404
+ context.log(LogLevelLabel.TRACE, "Generating built-in barrel file");
3405
+ await context.fs.writeBuiltinFile("index", joinPaths(context.builtinsPath, "index.ts"), `
3406
+ ${getFileHeader()}
3407
+
3408
+ ${(await context.fs.listBuiltinFiles()).filter((file) => !isParentPath(file.path, joinPaths(context.builtinsPath, "log")) && !isParentPath(file.path, joinPaths(context.builtinsPath, "storage"))).map((file) => `export * from "./${replacePath(file.path, context.builtinsPath).replace(`.${findFileExtensionSafe(file.path)}`, "")}";`).join("\n")}
3409
+ `);
3410
+ }
3411
+ if (context.config.output.dts !== false) {
3412
+ await generateTypes(context);
3413
+ }
3414
+ await this.callNormalHook(context, "prepare");
3415
+ context.tsconfig = getParsedTypeScriptConfig(context.workspaceConfig.workspaceRoot, context.config.projectRoot, context.config.tsconfig);
3416
+ if (!context.tsconfig) {
3417
+ throw new Error("Failed to parse the TypeScript configuration file.");
3418
+ }
3419
+ await this.callPostHook(context, "prepare");
3420
+ await writeMetaFile(context);
3421
+ context.fs[__VFS_REVERT__]();
3422
+ });
3423
+ this.context.log(LogLevelLabel.INFO, "Powerlines API has been prepared successfully");
3424
+ }
3425
+ /**
3426
+ * Create a new Powerlines project
3427
+ *
3428
+ * @remarks
3429
+ * This method will create a new Powerlines project in the current directory.
3430
+ *
3431
+ * @param inlineConfig - The inline configuration for the new command
3432
+ * @returns A promise that resolves when the project has been created
3433
+ */
3434
+ async new(inlineConfig) {
3435
+ this.context.log(LogLevelLabel.INFO, "\u{1F195} Creating a new Powerlines project");
3436
+ await this.prepare(inlineConfig);
3437
+ await this.#executeEnvironments(async (context) => {
3438
+ context.log(LogLevelLabel.TRACE, `Initializing the processing options for the Powerlines project.`);
3439
+ await this.callPreHook(context, "new");
3440
+ const files = await listFiles(joinPaths(context.corePackagePath, "files/common/**/*.hbs"));
3441
+ for (const file of files) {
3442
+ context.log(LogLevelLabel.TRACE, `Adding template file: ${file}`);
3443
+ const template = Handlebars.compile(file);
3444
+ await writeFile2(context.log, joinPaths(context.config.projectRoot, file.replace(".hbs", "")), template(context));
3445
+ }
3446
+ await this.callNormalHook(context, "new");
3447
+ if (context.config.projectType === "application") {
3448
+ const files2 = await listFiles(joinPaths(context.corePackagePath, "files/application/**/*.hbs"));
3449
+ for (const file of files2) {
3450
+ context.log(LogLevelLabel.TRACE, `Adding application template file: ${file}`);
3451
+ const template = Handlebars.compile(file);
3452
+ await writeFile2(context.log, joinPaths(context.config.projectRoot, file.replace(".hbs", "")), template(context));
3453
+ }
3454
+ } else {
3455
+ const files2 = await listFiles(joinPaths(context.corePackagePath, "files/library/**/*.hbs"));
3456
+ for (const file of files2) {
3457
+ context.log(LogLevelLabel.TRACE, `Adding library template file: ${file}`);
3458
+ const template = Handlebars.compile(file);
3459
+ await writeFile2(context.log, joinPaths(context.config.projectRoot, file.replace(".hbs", "")), template(context));
3460
+ }
3461
+ }
3462
+ await this.callPostHook(context, "new");
3463
+ });
3464
+ this.context.log(LogLevelLabel.TRACE, "Powerlines - New command completed");
3465
+ }
3466
+ /**
3467
+ * Clean any previously prepared artifacts
3468
+ *
3469
+ * @remarks
3470
+ * This method will remove the previous Powerlines artifacts from the project.
3471
+ *
3472
+ * @param inlineConfig - The inline configuration for the clean command
3473
+ * @returns A promise that resolves when the clean command has completed
3474
+ */
3475
+ async clean(inlineConfig = {
3476
+ command: "clean"
3477
+ }) {
3478
+ this.context.log(LogLevelLabel.INFO, "\u{1F9F9} Cleaning the previous Powerlines artifacts");
3479
+ await this.prepare(inlineConfig);
3480
+ await this.#executeEnvironments(async (context) => {
3481
+ await callHook(context, "clean", {
3482
+ sequential: true
3483
+ });
3484
+ });
3485
+ this.context.log(LogLevelLabel.TRACE, "Powerlines - Clean command completed");
3486
+ }
3487
+ /**
3488
+ * Lint the project
3489
+ *
3490
+ * @param inlineConfig - The inline configuration for the lint command
3491
+ * @returns A promise that resolves when the lint command has completed
3492
+ */
3493
+ async lint(inlineConfig = {
3494
+ command: "lint"
3495
+ }) {
3496
+ this.context.log(LogLevelLabel.INFO, "\u{1F4CB} Linting the Powerlines project");
3497
+ await this.prepare(inlineConfig);
3498
+ await this.#executeEnvironments(async (context) => {
3499
+ if (context.config.lint !== false) {
3500
+ await this.callPreHook(context, "lint");
3501
+ await typeCheck(context);
3502
+ await eslint(context, true, context.config.lint.eslint);
3503
+ await this.callNormalHook(context, "lint");
3504
+ await this.callPostHook(context, "lint");
3505
+ }
3506
+ });
3507
+ this.context.log(LogLevelLabel.TRACE, "Powerlines linting completed");
3508
+ }
3509
+ /**
3510
+ * Build the project
3511
+ *
3512
+ * @remarks
3513
+ * This method will build the Powerlines project, generating the necessary artifacts.
3514
+ *
3515
+ * @param inlineConfig - The inline configuration for the build command
3516
+ * @returns A promise that resolves when the build command has completed
3517
+ */
3518
+ async build(inlineConfig = {
3519
+ command: "build"
3520
+ }) {
3521
+ this.context.log(LogLevelLabel.INFO, "\u{1F4E6} Building the Powerlines project");
3522
+ await this.prepare(inlineConfig);
3523
+ await this.#executeEnvironments(async (context) => {
3524
+ await this.callPreHook(context, "build");
3525
+ await this.callNormalHook(context, "build");
3526
+ await Promise.all(context.config.output.assets.map(async (asset) => {
3527
+ context.log(LogLevelLabel.DEBUG, `Copying asset(s): ${chalk5.redBright(context.workspaceConfig.workspaceRoot === asset.input ? asset.glob : joinPaths(replacePath(asset.input, context.workspaceConfig.workspaceRoot), asset.glob))} -> ${chalk5.greenBright(joinPaths(replacePath(asset.output, context.workspaceConfig.workspaceRoot), asset.glob))} ${Array.isArray(asset.ignore) && asset.ignore.length > 0 ? ` (ignoring: ${asset.ignore.map((i) => chalk5.yellowBright(i)).join(", ")})` : ""}`);
3528
+ await copyFiles(asset, asset.output);
3529
+ }));
3530
+ await this.callPostHook(context, "build");
3531
+ });
3532
+ this.context.log(LogLevelLabel.TRACE, "Powerlines build completed");
3533
+ }
3534
+ /**
3535
+ * Prepare the documentation for the project
3536
+ *
3537
+ * @param inlineConfig - The inline configuration for the docs command
3538
+ * @returns A promise that resolves when the documentation generation has completed
3539
+ */
3540
+ async docs(inlineConfig = {
3541
+ command: "docs"
3542
+ }) {
3543
+ this.context.log(LogLevelLabel.INFO, "Generating documentation for the Powerlines project");
3544
+ await this.prepare(inlineConfig);
3545
+ await this.#executeEnvironments(async (context) => {
3546
+ context.log(LogLevelLabel.TRACE, "Writing API-Reference documentation for the Powerlines project artifacts.");
3547
+ await this.callPreHook(context, "docs");
3548
+ const outputPath = joinPaths(context.config.projectRoot, "docs", "generated", "api-reference");
3549
+ if (existsSync(outputPath)) {
3550
+ await removeDirectory(outputPath);
3551
+ }
3552
+ await createDirectory(outputPath);
3553
+ await this.callNormalHook(context, "docs");
3554
+ const { generateDocs, getReflections } = await initTypedoc(context, {
3555
+ outputPath
3556
+ });
3557
+ const project = await getReflections();
3558
+ if (project) {
3559
+ await generateDocs({
3560
+ project
3561
+ });
3562
+ }
3563
+ await this.callPostHook(context, "docs");
3564
+ });
3565
+ this.#context.log(LogLevelLabel.TRACE, "Powerlines documentation generation completed");
3566
+ }
3567
+ /**
3568
+ * Release the project
3569
+ *
3570
+ * @remarks
3571
+ * This method will prepare and build the Powerlines project, generating the necessary artifacts for release.
3572
+ *
3573
+ * @param inlineConfig - The inline configuration for the release command
3574
+ */
3575
+ async release(inlineConfig = {
3576
+ command: "release"
3577
+ }) {
3578
+ this.context.log(LogLevelLabel.INFO, "\u{1F4E6} Releasing the Powerlines project");
3579
+ await this.prepare(inlineConfig);
3580
+ await this.#executeEnvironments(async (context) => {
3581
+ await this.callHook(context, "release");
3582
+ });
3583
+ this.context.log(LogLevelLabel.TRACE, "Powerlines release completed");
3584
+ }
3585
+ /**
3586
+ * Finalization process
3587
+ *
3588
+ * @remarks
3589
+ * This step includes any final processes or clean up required by Powerlines. It will be run after each Powerlines command.
3590
+ *
3591
+ * @returns A promise that resolves when the finalization process has completed
3592
+ */
3593
+ async finalize() {
3594
+ this.context.log(LogLevelLabel.TRACE, "Powerlines finalize execution started");
3595
+ await this.#executeEnvironments(async (context) => {
3596
+ await this.callHook(context, "finalize");
3597
+ context.fs[__VFS_REVERT__]();
3598
+ });
3599
+ this.context.log(LogLevelLabel.TRACE, "Powerlines finalize execution completed");
3600
+ }
3601
+ /**
3602
+ * Calls a hook in parallel
3603
+ *
3604
+ * @param hook - The hook to call
3605
+ * @param options - Options for calling the hook
3606
+ * @param args - The arguments to pass to the hook
3607
+ * @returns The result of the hook call
3608
+ */
3609
+ async callHookParallel(hook, options, ...args) {
3610
+ return callHook(isSetObject(options?.environment) ? options.environment : await this.#context.getEnvironment(options?.environment), hook, {
3611
+ ...options,
3612
+ sequential: false
3613
+ }, ...args);
3614
+ }
3615
+ /**
3616
+ * Calls a hook in sequence
3617
+ *
3618
+ * @param hook - The hook to call
3619
+ * @param options - Options for calling the hook
3620
+ * @param args - The arguments to pass to the hook
3621
+ * @returns The result of the hook call
3622
+ */
3623
+ async callHookSequential(hook, options, ...args) {
3624
+ return callHook(isSetObject(options?.environment) ? options.environment : await this.#context.getEnvironment(options?.environment), hook, {
3625
+ ...options,
3626
+ sequential: true
3627
+ }, ...args);
3628
+ }
3629
+ /**
3630
+ * Calls the `"pre"` ordered hooks in sequence
3631
+ *
3632
+ * @param environment - The environment to use for the hook call
3633
+ * @param hook - The hook to call
3634
+ * @param args - The arguments to pass to the hook
3635
+ * @returns The result of the hook call
3636
+ */
3637
+ async callPreHook(environment, hook, ...args) {
3638
+ return this.callHookSequential(hook, {
3639
+ order: "pre",
3640
+ environment
3641
+ }, ...args);
3642
+ }
3643
+ /**
3644
+ * Calls the `"post"` ordered hooks in sequence
3645
+ *
3646
+ * @param environment - The environment to use for the hook call
3647
+ * @param hook - The hook to call
3648
+ * @param args - The arguments to pass to the hook
3649
+ * @returns The result of the hook call
3650
+ */
3651
+ async callPostHook(environment, hook, ...args) {
3652
+ return this.callHookSequential(hook, {
3653
+ order: "post",
3654
+ environment
3655
+ }, ...args);
3656
+ }
3657
+ /**
3658
+ * Calls a hook in sequence
3659
+ *
3660
+ * @param environment - The environment to use for the hook call
3661
+ * @param hook - The hook to call
3662
+ * @param args - The arguments to pass to the hook
3663
+ * @returns The result of the hook call
3664
+ */
3665
+ async callNormalHook(environment, hook, ...args) {
3666
+ return this.callHookSequential(hook, {
3667
+ order: "normal",
3668
+ environment
3669
+ }, ...args);
3670
+ }
3671
+ /**
3672
+ * Calls the `"pre"` and `"post"` ordered hooks, as well as the normal hooks in sequence
3673
+ *
3674
+ * @param environment - The environment to use for the hook call
3675
+ * @param hook - The hook to call
3676
+ * @param args - The arguments to pass to the hook
3677
+ * @returns The result of the hook call
3678
+ */
3679
+ async callHook(environment, hook, ...args) {
3680
+ return this.callHookSequential(hook, {
3681
+ environment
3682
+ }, ...args);
3683
+ }
3684
+ async [Symbol.asyncDispose]() {
3685
+ await this.finalize();
3686
+ }
3687
+ /**
3688
+ * Get the configured environments
3689
+ *
3690
+ * @returns The configured environments
3691
+ */
3692
+ async #getEnvironments() {
3693
+ if (!this.context.config.environments || Object.keys(this.context.config.environments).length <= 1) {
3694
+ this.context.log(LogLevelLabel.DEBUG, "No environments are configured for this Powerlines project. Using the default environment.");
3695
+ return [
3696
+ await this.context.getEnvironment()
3697
+ ];
3698
+ }
3699
+ this.context.log(LogLevelLabel.DEBUG, `Found ${Object.keys(this.context.config.environments).length} configured environment(s) for this Powerlines project.`);
3700
+ return (await Promise.all(Object.entries(this.context.config.environments).map(async ([name, config]) => {
3701
+ const environment = await this.context.getEnvironmentSafe(name);
3702
+ if (!environment) {
3703
+ const resolvedEnvironment = await this.callHookParallel("configEnvironment", {
3704
+ environment: name
3705
+ }, name, config);
3706
+ if (resolvedEnvironment) {
3707
+ this.context.environments[name] = await this.context.in(resolvedEnvironment);
3708
+ }
3709
+ }
3710
+ return this.context.environments[name];
3711
+ }))).filter((context) => isSet(context));
3712
+ }
3713
+ /**
3714
+ * Execute a handler function for each environment
3715
+ *
3716
+ * @param handle - The handler function to execute for each environment
3717
+ */
3718
+ async #executeEnvironments(handle) {
3719
+ await Promise.all((await this.#getEnvironments()).map(async (context) => {
3720
+ return Promise.resolve(handle(context));
3721
+ }));
3722
+ }
3723
+ /**
3724
+ * Add a Powerlines plugin used in the build process
3725
+ *
3726
+ * @param config - The import path of the plugin to add
3727
+ */
3728
+ async #addPlugin(config) {
3729
+ if (config) {
3730
+ const plugin = await this.#initPlugin(config);
3731
+ if (!plugin) {
3732
+ return;
3733
+ }
3734
+ if (plugin.dependsOn) {
3735
+ for (const required of plugin.dependsOn) {
3736
+ await this.#addPlugin(required);
3737
+ }
3738
+ }
3739
+ this.context.log(LogLevelLabel.DEBUG, `Successfully initialized the ${chalk5.bold.cyanBright(plugin.name)} plugin`);
3740
+ await this.context.addPlugin(plugin);
3741
+ }
3742
+ }
3743
+ /**
3744
+ * Initialize a Powerlines plugin
3745
+ *
3746
+ * @param config - The configuration for the plugin
3747
+ * @returns The initialized plugin instance, or null if the plugin was a duplicate
3748
+ * @throws Will throw an error if the plugin cannot be found or is invalid
3749
+ */
3750
+ async #initPlugin(config) {
3751
+ if (!isPluginConfig(config)) {
3752
+ throw new Error(`Invalid plugin specified in the configuration - ${JSON.stringify(config)}. Please ensure the value is a plugin name, an object with the \`plugin\` and \`props\` properties, or an instance of \`Plugin\`.`);
3753
+ }
3754
+ let plugin;
3755
+ if (isPlugin(config)) {
3756
+ plugin = config;
3757
+ } else if (isFunction(config)) {
3758
+ plugin = await Promise.resolve(config());
3759
+ } else if (isSetString(config)) {
3760
+ const resolved = await this.#resolvePlugin(config);
3761
+ if (isFunction(resolved)) {
3762
+ plugin = await Promise.resolve(resolved());
3763
+ } else {
3764
+ plugin = resolved;
3765
+ }
3766
+ } else if (isPluginConfigTuple(config) || isPluginConfigObject(config)) {
3767
+ let pluginConfig;
3768
+ let pluginOptions;
3769
+ if (isPluginConfigTuple(config)) {
3770
+ pluginConfig = config[0];
3771
+ pluginOptions = config?.length === 2 ? config[1] : void 0;
3772
+ } else {
3773
+ pluginConfig = config.plugin;
3774
+ pluginOptions = config.options;
3775
+ }
3776
+ if (isSetString(pluginConfig)) {
3777
+ const resolved = await this.#resolvePlugin(pluginConfig);
3778
+ if (isFunction(resolved)) {
3779
+ plugin = await Promise.resolve(pluginOptions ? resolved(pluginOptions) : resolved());
3780
+ } else {
3781
+ plugin = resolved;
3782
+ }
3783
+ } else if (isFunction(pluginConfig)) {
3784
+ plugin = await Promise.resolve(pluginConfig(pluginOptions));
3785
+ } else if (isPlugin(pluginConfig)) {
3786
+ plugin = pluginConfig;
3787
+ }
3788
+ }
3789
+ if (!plugin) {
3790
+ throw new Error(`The plugin configuration ${JSON.stringify(config)} is invalid. This configuration must point to a valid Powerlines plugin module.`);
3791
+ }
3792
+ if (!isPlugin(plugin)) {
3793
+ throw new Error(`The plugin option ${JSON.stringify(plugin)} does not export a valid module. This configuration must point to a valid Powerlines plugin module.`);
3794
+ }
3795
+ if (checkDedupe(plugin, this.context.plugins)) {
3796
+ this.context.log(LogLevelLabel.TRACE, `Duplicate ${chalk5.bold.cyanBright(plugin.name)} plugin dependency detected - Skipping initialization.`);
3797
+ return null;
3798
+ }
3799
+ this.context.log(LogLevelLabel.TRACE, `Initializing the ${chalk5.bold.cyanBright(plugin.name)} plugin...`);
3800
+ return plugin;
3801
+ }
3802
+ async #resolvePlugin(pluginPath) {
3803
+ if (pluginPath.startsWith("@") && pluginPath.split("/").filter(Boolean).length > 2) {
3804
+ const splits = pluginPath.split("/").filter(Boolean);
3805
+ pluginPath = `${splits[0]}/${splits[1]}`;
3806
+ }
3807
+ const isInstalled = isPackageExists(pluginPath, {
3808
+ paths: [
3809
+ this.context.workspaceConfig.workspaceRoot,
3810
+ this.context.config.projectRoot
3811
+ ]
3812
+ });
3813
+ if (!isInstalled && this.context.config.skipInstalls !== true) {
3814
+ this.#context.log(LogLevelLabel.WARN, `The plugin package "${pluginPath}" is not installed. It will be installed automatically.`);
3815
+ const result = await install(pluginPath, {
3816
+ cwd: this.context.config.projectRoot
3817
+ });
3818
+ if (isNumber(result.exitCode) && result.exitCode > 0) {
3819
+ this.#context.log(LogLevelLabel.ERROR, result.stderr);
3820
+ throw new Error(`An error occurred while installing the build plugin package "${pluginPath}" `);
3821
+ }
3822
+ }
3823
+ try {
3824
+ const module = await this.context.resolver.plugin.import(this.context.resolver.plugin.esmResolve(joinPaths(pluginPath, "plugin")));
3825
+ const result = module.plugin ?? module.default;
3826
+ if (!result) {
3827
+ throw new Error(`The plugin package "${pluginPath}" does not export a valid module.`);
3828
+ }
3829
+ return result;
3830
+ } catch (error) {
3831
+ try {
3832
+ const module = await this.context.resolver.plugin.import(this.context.resolver.plugin.esmResolve(pluginPath));
3833
+ const result = module.plugin ?? module.default;
3834
+ if (!result) {
3835
+ throw new Error(`The plugin package "${pluginPath}" does not export a valid module.`);
3836
+ }
3837
+ return result;
3838
+ } catch {
3839
+ if (!isInstalled) {
3840
+ throw new Error(`The plugin package "${pluginPath}" is not installed. Please install the package using the command: "npm install ${pluginPath} --save-dev"`);
3841
+ } else {
3842
+ throw new Error(`An error occurred while importing the build plugin package "${pluginPath}":
3843
+ ${isError(error) ? error.message : String(error)}
3844
+
3845
+ Note: Please ensure the plugin package's default export is a class that extends \`Plugin\` with a constructor that excepts a single arguments of type \`PluginOptions\`.`);
3846
+ }
3847
+ }
3848
+ }
3849
+ }
3850
+ };
3851
+ var DEFAULT_ESBUILD_CONFIG = {
3852
+ target: "esnext",
3853
+ platform: "neutral",
3854
+ format: "esm",
3855
+ write: true,
3856
+ minify: true,
3857
+ sourcemap: false,
3858
+ bundle: true,
3859
+ treeShaking: true,
3860
+ keepNames: true,
3861
+ splitting: true,
3862
+ logLevel: "silent"
3863
+ };
3864
+ function extractESBuildConfig(context) {
3865
+ return defu4({
3866
+ alias: context.fs.builtinIdMap.keys().reduce((ret, id) => {
3867
+ const path2 = context.fs.builtinIdMap.get(id);
3868
+ if (path2) {
3869
+ ret[id] = path2;
3870
+ }
3871
+ return ret;
3872
+ }, {})
3873
+ }, context.config.build.variant === "esbuild" ? context.config.override : {}, {
3874
+ format: Array.isArray(context.config.output.format) ? context.config.output.format[0] : context.config.output.format,
3875
+ platform: context.config.build.platform,
3876
+ treeShaking: Boolean(context.config.build?.treeshake) || context.config.build?.treeShaking,
3877
+ outdir: context.config.output.outputPath,
3878
+ tsconfig: context.tsconfig.tsconfigFilePath,
3879
+ tsconfigRaw: context.tsconfig.tsconfigJson
3880
+ }, context.config.build.variant === "esbuild" ? context.config.build : {}, {
3881
+ minify: context.config.mode !== "development",
3882
+ metafile: context.config.mode === "development",
3883
+ sourcemap: context.config.mode === "development"
3884
+ }, DEFAULT_ESBUILD_CONFIG);
3885
+ }
3886
+ __name(extractESBuildConfig, "extractESBuildConfig");
3887
+
3888
+ // ../powerlines/src/lib/build/vite.ts
3889
+ var DEFAULT_VITE_CONFIG = {
3890
+ resolve: {
3891
+ extensions: [
3892
+ ".mjs",
3893
+ ".js",
3894
+ ".mts",
3895
+ ".ts",
3896
+ ".jsx",
3897
+ ".tsx",
3898
+ ".json"
3899
+ ]
3900
+ },
3901
+ json: {
3902
+ stringify: true
3903
+ },
3904
+ logLevel: "silent",
3905
+ clearScreen: true
3906
+ };
3907
+ function extractViteConfig(context) {
3908
+ return defu4({
3909
+ resolve: {
3910
+ alias: context.fs.builtinIdMap.keys().reduce((ret, id) => {
3911
+ const path2 = context.fs.builtinIdMap.get(id);
3912
+ if (path2) {
3913
+ ret[id] = path2;
3914
+ }
3915
+ return ret;
3916
+ }, {})
3917
+ }
3918
+ }, context.config.build.variant === "vite" ? context.config.override : {}, {
3919
+ external: context.config.build.external,
3920
+ noExternal: context.config.build.noExternal,
3921
+ skipNodeModulesBundle: context.config.build.skipNodeModulesBundle
3922
+ }, {
3923
+ rootDir: context.config.sourceRoot,
3924
+ platform: context.config.build.platform,
3925
+ mode: context.config.mode === "development" ? "development" : "production",
3926
+ cacheDir: joinPaths(context.cachePath, "vite"),
3927
+ build: {
3928
+ outDir: context.config.output.outputPath,
3929
+ tsconfig: context.tsconfig.tsconfigFilePath,
3930
+ tsconfigRaw: context.tsconfig.tsconfigJson
3931
+ },
3932
+ esbuild: extractESBuildConfig(context),
3933
+ logLevel: context.config.logLevel ?? void 0,
3934
+ envDir: context.config.projectRoot,
3935
+ noExternal: Array.from(context.fs.builtinIdMap.keys())
3936
+ }, context.config.build.variant === "vite" ? context.config.build : {}, {
3937
+ build: {
3938
+ minify: context.config.mode !== "development",
3939
+ metafile: context.config.mode === "development",
3940
+ sourcemap: context.config.mode === "development"
3941
+ }
3942
+ }, DEFAULT_VITE_CONFIG);
3943
+ }
3944
+ __name(extractViteConfig, "extractViteConfig");
3945
+ var NON_NODE_MODULE_REGEX = /^[A-Z]:[/\\]|^\.{0,2}\/|^\.{1,2}$/;
3946
+ async function handleResolveId(context, args, options = {}) {
3947
+ if (args.id) {
3948
+ if (context.fs.isVirtualFile(args.id) || args.importer && context.fs.isVirtualFile(args.id, {
3949
+ paths: [
3950
+ args.importer
3951
+ ]
3952
+ })) {
3953
+ const resolvedPath = args.importer ? context.fs.resolvePath(args.id, {
3954
+ paths: [
3955
+ args.importer
3956
+ ]
3957
+ }) : context.fs.resolvePath(args.id);
3958
+ if (resolvedPath) {
3959
+ return {
3960
+ id: resolvedPath,
3961
+ external: context.config.projectType !== "application"
3962
+ };
3963
+ }
3964
+ }
3965
+ if (context.fs.isTsconfigPath(args.id)) {
3966
+ const tsconfigPath = context.fs.resolveTsconfigPath(args.id);
3967
+ const tsconfigPathPackage = context.fs.resolveTsconfigPathPackage(args.id);
3968
+ if (tsconfigPath && tsconfigPathPackage) {
3969
+ return {
3970
+ id: tsconfigPath,
3971
+ external: Boolean(!options.noExternal?.includes(tsconfigPathPackage) && (options.external?.includes(tsconfigPathPackage) ?? context.config.projectType !== "application"))
3972
+ };
3973
+ }
3974
+ }
3975
+ if (options.skipResolve) {
3976
+ return void 0;
3977
+ }
3978
+ if (options.skipNodeModulesBundle) {
3979
+ if (match(args.id, options.resolvePatterns) || match(args.id, options.noExternal) || args.id.startsWith("internal:") || args.id.startsWith("virtual:")) {
3980
+ return void 0;
3981
+ }
3982
+ if (match(args.id, options.external) || args.id.startsWith("node:")) {
3983
+ return {
3984
+ id: args.id,
3985
+ external: true
3986
+ };
3987
+ }
3988
+ if (!NON_NODE_MODULE_REGEX.test(args.id)) {
3989
+ return {
3990
+ id: args.id,
3991
+ external: true
3992
+ };
3993
+ }
3994
+ } else {
3995
+ if (match(args.id, options.noExternal) || context.fs.isBuiltinFile(args.id) || args.importer && context.fs.isBuiltinFile(args.id, {
3996
+ paths: [
3997
+ args.importer
3998
+ ]
3999
+ })) {
4000
+ return void 0;
4001
+ }
4002
+ if (match(args.id, options.external) || args.id.startsWith("node:")) {
4003
+ return {
4004
+ id: args.id,
4005
+ external: true
4006
+ };
4007
+ }
4008
+ }
4009
+ }
4010
+ return void 0;
4011
+ }
4012
+ __name(handleResolveId, "handleResolveId");
4013
+
4014
+ // ../powerlines/src/lib/unplugin/factory.ts
4015
+ function createUnpluginFactory(variant, decorate) {
4016
+ return (config, meta) => {
4017
+ const log = createLog("unplugin", config);
4018
+ log(LogLevelLabel.DEBUG, "Initializing Unplugin");
4019
+ try {
4020
+ const userConfig = {
4021
+ ...config,
4022
+ variant,
4023
+ unplugin: meta
4024
+ };
4025
+ let api;
4026
+ let resolvePatterns = [];
4027
+ async function buildStart() {
4028
+ log(LogLevelLabel.DEBUG, "Powerlines build plugin starting...");
4029
+ const workspaceRoot = getWorkspaceRoot(process.cwd());
4030
+ api = await PowerlinesAPI.from(workspaceRoot, userConfig);
4031
+ if (api.context.config.build.skipNodeModulesBundle) {
4032
+ resolvePatterns = tsconfigPathsToRegExp(api.context.tsconfig.options.paths ?? []);
4033
+ }
4034
+ log(LogLevelLabel.DEBUG, "Preparing build artifacts for the Powerlines project...");
4035
+ await api.prepare({
4036
+ command: "build"
4037
+ });
4038
+ }
4039
+ __name(buildStart, "buildStart");
4040
+ async function resolveId(id, importer, options = {
4041
+ isEntry: false
4042
+ }) {
4043
+ return handleResolveId(api.context, {
4044
+ id,
4045
+ importer,
4046
+ options
4047
+ }, {
4048
+ skipNodeModulesBundle: api.context.config.build.skipNodeModulesBundle,
4049
+ external: api.context.config.build.external,
4050
+ noExternal: api.context.config.build.noExternal,
4051
+ resolvePatterns
4052
+ });
4053
+ }
4054
+ __name(resolveId, "resolveId");
4055
+ async function load(id) {
4056
+ const environment = await api.context.getEnvironment();
4057
+ if (id) {
4058
+ const resolvedPath = environment.fs.resolvePath(id, {
4059
+ type: "file"
4060
+ });
4061
+ if (resolvedPath) {
4062
+ return environment.fs.readFile(resolvedPath);
4063
+ }
4064
+ }
4065
+ let result2 = await api.callPreHook(environment, "load", id);
4066
+ if (result2) {
4067
+ return result2;
4068
+ }
4069
+ result2 = await api.callNormalHook(environment, "load", id);
4070
+ if (result2) {
4071
+ return result2;
4072
+ }
4073
+ return api.callPostHook(environment, "load", id);
4074
+ }
4075
+ __name(load, "load");
4076
+ async function transform(code, id) {
4077
+ const environment = await api.context.getEnvironment();
4078
+ let transformed = code;
4079
+ let result2 = await api.callPreHook(environment, "transform", getString(transformed), id);
4080
+ if (result2) {
4081
+ transformed = result2;
4082
+ }
4083
+ result2 = await api.callNormalHook(environment, "transform", getString(transformed), id);
4084
+ if (result2) {
4085
+ transformed = result2;
4086
+ }
4087
+ result2 = await api.callPostHook(environment, "transform", getString(transformed), id);
4088
+ if (result2) {
4089
+ transformed = result2;
4090
+ }
4091
+ return transformed;
4092
+ }
4093
+ __name(transform, "transform");
4094
+ async function writeBundle() {
4095
+ log(LogLevelLabel.DEBUG, "Finalizing Powerlines project output...");
4096
+ const environment = await api.context.getEnvironment();
4097
+ await api.callHook(environment, "writeBundle");
4098
+ }
4099
+ __name(writeBundle, "writeBundle");
4100
+ const result = {
4101
+ name: "powerlines",
4102
+ resolveId: {
4103
+ filter: {
4104
+ id: {
4105
+ include: [
4106
+ /.*/
4107
+ ]
4108
+ }
4109
+ },
4110
+ handler: resolveId
4111
+ },
4112
+ load: {
4113
+ filter: {
4114
+ id: {
4115
+ include: [
4116
+ /.*/,
4117
+ /^storm:/
4118
+ ]
4119
+ }
4120
+ },
4121
+ handler: load
4122
+ },
4123
+ transform,
4124
+ buildStart,
4125
+ writeBundle
4126
+ };
4127
+ return decorate ? decorate(api, result) : result;
4128
+ } catch (error) {
4129
+ log(LogLevelLabel.FATAL, error?.message);
4130
+ throw error;
4131
+ }
4132
+ };
4133
+ }
4134
+ __name(createUnpluginFactory, "createUnpluginFactory");
4135
+
4136
+ // ../powerlines/src/vite.ts
4137
+ var vite = createVitePlugin(createUnpluginFactory("vite", (api, plugin) => {
4138
+ return {
4139
+ ...plugin,
4140
+ vite: {
4141
+ sharedDuringBuild: true,
4142
+ async hotUpdate(options) {
4143
+ const environment = await api.context.getEnvironment();
4144
+ return api.callHook(environment, "vite:hotUpdate", options);
4145
+ },
4146
+ async config(config, env) {
4147
+ api.context.config.mode = isDevelopmentMode(env.mode) ? "development" : isTestMode(env.mode) ? "test" : "production";
4148
+ const environment = await api.context.getEnvironment();
4149
+ const result = await api.callHook(environment, "config");
4150
+ return defu4(extractViteConfig(api.context), result?.build ?? {}, config);
4151
+ },
4152
+ async configResolved(_config) {
4153
+ const environment = await api.context.getEnvironment();
4154
+ await api.callHook(environment, "configResolved");
4155
+ },
4156
+ async configureServer(server) {
4157
+ const environment = await api.context.getEnvironment();
4158
+ return api.callHook(environment, "vite:configureServer", server);
4159
+ },
4160
+ async configurePreviewServer(server) {
4161
+ const environment = await api.context.getEnvironment();
4162
+ return api.callHook(environment, "vite:configurePreviewServer", server);
4163
+ },
4164
+ async transformIndexHtml(html, ctx) {
4165
+ const environment = await api.context.getEnvironment();
4166
+ return api.callHook(environment, "vite:transformIndexHtml", html, ctx);
4167
+ },
4168
+ async handleHotUpdate(ctx) {
4169
+ const environment = await api.context.getEnvironment();
4170
+ return api.callHook(environment, "vite:handleHotUpdate", ctx);
4171
+ }
4172
+ }
4173
+ };
4174
+ }));
4175
+ var vite_default = vite;
4176
+ createEsbuildPlugin(createUnpluginFactory("esbuild", (api, plugin) => {
4177
+ return {
4178
+ ...plugin,
4179
+ esbuild: {
4180
+ config: /* @__PURE__ */ __name((options) => {
4181
+ options ??= {};
4182
+ const result = extractESBuildConfig(api.context);
4183
+ for (const key in result) {
4184
+ if (isUndefined(options[key]) && !isUndefined(result[key])) {
4185
+ options[key] = result[key];
4186
+ }
4187
+ }
4188
+ }, "config"),
4189
+ setup: /* @__PURE__ */ __name(async (build) => {
4190
+ const environment = await api.context.getEnvironment();
4191
+ return api.callHook(environment, "esbuild:setup", build);
4192
+ }, "setup")
4193
+ }
4194
+ };
4195
+ }));
4196
+ var webpack = createWebpackPlugin(createUnpluginFactory("webpack"));
4197
+ var webpack_default = webpack;
4198
+ defineNuxtModule({
4199
+ meta: {
4200
+ name: "powerlines",
4201
+ configKey: "storm"
4202
+ },
4203
+ defaults: {},
4204
+ setup(options, _nuxt) {
4205
+ addVitePlugin(() => vite_default(options));
4206
+ addWebpackPlugin(() => webpack_default(options));
4207
+ }
4208
+ });
4209
+ createRolldownPlugin(createUnpluginFactory("rolldown"));
4210
+ createRollupPlugin(createUnpluginFactory("rollup"));
4211
+ createRspackPlugin(createUnpluginFactory("rspack"));
4212
+ createUnloaderPlugin(createUnpluginFactory("unloader"));
4213
+
4214
+ // ../powerlines/src/index.ts
4215
+ var src_default = PowerlinesAPI;
4216
+
4217
+ // src/base/base-executor.ts
4218
+ function withExecutor(command, executorFn) {
4219
+ return withRunExecutor(`Powerlines ${command} command executor`, async (options, context, workspaceConfig) => {
4220
+ if (!context.projectName) {
4221
+ throw new Error("The executor requires `projectName` on the context object.");
4222
+ }
4223
+ if (!context.projectName || !context.projectsConfigurations?.projects || !context.projectsConfigurations.projects[context.projectName] || !context.projectsConfigurations.projects[context.projectName]?.root) {
4224
+ throw new Error("The executor requires `projectsConfigurations` on the context object.");
4225
+ }
4226
+ const projectConfig = context.projectsConfigurations.projects[context.projectName];
4227
+ const api = await src_default.from(workspaceConfig.workspaceRoot, defu4({
4228
+ root: projectConfig.root,
4229
+ type: projectConfig.projectType,
4230
+ sourceRoot: projectConfig.sourceRoot,
4231
+ tsconfig: options.tsconfig,
4232
+ logLevel: options.logLevel,
4233
+ mode: options.mode,
4234
+ skipCache: options.skipCache,
4235
+ output: {
4236
+ outputPath: options.outputPath ?? projectConfig.targets?.build?.options?.outputPath
4237
+ }
4238
+ }, options));
4239
+ try {
4240
+ return await Promise.resolve(executorFn(defu4({
4241
+ projectName: context.projectName,
4242
+ options,
4243
+ workspaceConfig,
4244
+ inlineConfig: {
4245
+ command,
4246
+ configFile: options.configFile
4247
+ },
4248
+ command
4249
+ }, context), api));
4250
+ } catch (error) {
4251
+ writeError(`An error occurred while executing the Powerlines ${command} command executor: ${isError(error) ? `${error.message}
4252
+
4253
+ ${error.stack}` : "Unknown error"}`);
4254
+ return {
4255
+ success: false
4256
+ };
4257
+ } finally {
4258
+ await api.finalize();
4259
+ }
4260
+ }, {
4261
+ skipReadingConfig: false,
4262
+ hooks: {
4263
+ applyDefaultOptions: /* @__PURE__ */ __name((options) => {
4264
+ if (options.mode !== "development" && options.mode !== "test") {
4265
+ options.mode = "production";
4266
+ }
4267
+ options.outputPath ??= "dist/{projectRoot}";
4268
+ options.configFile ??= "{projectRoot}/powerlines.config.ts";
4269
+ return options;
4270
+ }, "applyDefaultOptions")
4271
+ }
4272
+ });
4273
+ }
4274
+ __name(withExecutor, "withExecutor");
4275
+
4276
+ export { withExecutor };