@powerlines/plugin-webpack 0.5.240 → 0.5.241

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 (114) hide show
  1. package/dist/helpers/unplugin.cjs +3 -2
  2. package/dist/helpers/unplugin.mjs +2 -1
  3. package/dist/index.cjs +2 -2
  4. package/dist/index.d.cts +2 -3
  5. package/dist/index.d.mts +1 -1
  6. package/dist/index.mjs +3 -3
  7. package/dist/powerlines/package.cjs +11 -0
  8. package/dist/powerlines/package.mjs +5 -0
  9. package/dist/powerlines/schemas/fs.cjs +226 -0
  10. package/dist/powerlines/schemas/fs.mjs +224 -0
  11. package/dist/powerlines/src/api.cjs +568 -0
  12. package/dist/powerlines/src/api.mjs +566 -0
  13. package/dist/powerlines/src/internal/helpers/environment.cjs +50 -0
  14. package/dist/powerlines/src/internal/helpers/environment.mjs +47 -0
  15. package/dist/powerlines/src/internal/helpers/generate-types.cjs +78 -0
  16. package/dist/powerlines/src/internal/helpers/generate-types.mjs +76 -0
  17. package/dist/powerlines/src/internal/helpers/hooks.cjs +65 -0
  18. package/dist/powerlines/src/internal/helpers/hooks.d.cts +47 -0
  19. package/dist/powerlines/src/internal/helpers/hooks.d.mts +49 -0
  20. package/dist/powerlines/src/internal/helpers/hooks.mjs +63 -0
  21. package/dist/powerlines/src/internal/helpers/install-dependencies.cjs +24 -0
  22. package/dist/powerlines/src/internal/helpers/install-dependencies.mjs +23 -0
  23. package/dist/powerlines/src/internal/helpers/install.cjs +36 -0
  24. package/dist/powerlines/src/internal/helpers/install.mjs +35 -0
  25. package/dist/powerlines/src/internal/helpers/resolve-tsconfig.cjs +97 -0
  26. package/dist/powerlines/src/internal/helpers/resolve-tsconfig.mjs +93 -0
  27. package/dist/powerlines/src/internal/helpers/resolver.cjs +43 -0
  28. package/dist/powerlines/src/internal/helpers/resolver.mjs +41 -0
  29. package/dist/powerlines/src/lib/build/webpack.cjs +38 -0
  30. package/dist/powerlines/src/lib/build/webpack.mjs +36 -0
  31. package/dist/powerlines/src/lib/config-file.cjs +79 -0
  32. package/dist/powerlines/src/lib/config-file.mjs +76 -0
  33. package/dist/powerlines/src/lib/constants/environments.cjs +8 -0
  34. package/dist/powerlines/src/lib/constants/environments.mjs +6 -0
  35. package/dist/powerlines/src/lib/contexts/api-context.cjs +184 -0
  36. package/dist/powerlines/src/lib/contexts/api-context.mjs +182 -0
  37. package/dist/powerlines/src/lib/contexts/context.cjs +951 -0
  38. package/dist/powerlines/src/lib/contexts/context.mjs +949 -0
  39. package/dist/powerlines/src/lib/contexts/environment-context.cjs +195 -0
  40. package/dist/powerlines/src/lib/contexts/environment-context.mjs +194 -0
  41. package/dist/powerlines/src/lib/contexts/plugin-context.cjs +78 -0
  42. package/dist/powerlines/src/lib/contexts/plugin-context.mjs +77 -0
  43. package/dist/powerlines/src/lib/entry.cjs +85 -0
  44. package/dist/powerlines/src/lib/entry.mjs +83 -0
  45. package/dist/powerlines/src/lib/fs/helpers.cjs +85 -0
  46. package/dist/powerlines/src/lib/fs/helpers.mjs +81 -0
  47. package/dist/powerlines/src/lib/fs/storage/base.cjs +196 -0
  48. package/dist/powerlines/src/lib/fs/storage/base.mjs +195 -0
  49. package/dist/powerlines/src/lib/fs/storage/file-system.cjs +170 -0
  50. package/dist/powerlines/src/lib/fs/storage/file-system.mjs +169 -0
  51. package/dist/powerlines/src/lib/fs/storage/virtual.cjs +88 -0
  52. package/dist/powerlines/src/lib/fs/storage/virtual.mjs +87 -0
  53. package/dist/powerlines/src/lib/fs/vfs.cjs +912 -0
  54. package/dist/powerlines/src/lib/fs/vfs.mjs +910 -0
  55. package/dist/powerlines/src/lib/logger.cjs +58 -0
  56. package/dist/powerlines/src/lib/logger.mjs +55 -0
  57. package/dist/powerlines/src/lib/typescript/ts-morph.cjs +103 -0
  58. package/dist/powerlines/src/lib/typescript/ts-morph.mjs +101 -0
  59. package/dist/powerlines/src/lib/typescript/tsconfig.cjs +149 -0
  60. package/dist/powerlines/src/lib/typescript/tsconfig.mjs +144 -0
  61. package/dist/powerlines/src/lib/unplugin/factory.cjs +6 -0
  62. package/dist/powerlines/src/lib/unplugin/factory.mjs +8 -0
  63. package/dist/powerlines/src/lib/unplugin/helpers.cjs +29 -0
  64. package/dist/powerlines/src/lib/unplugin/helpers.mjs +27 -0
  65. package/dist/powerlines/src/lib/unplugin/index.cjs +4 -0
  66. package/dist/powerlines/src/lib/unplugin/index.mjs +6 -0
  67. package/dist/powerlines/src/lib/unplugin/module-resolution.cjs +66 -0
  68. package/dist/powerlines/src/lib/unplugin/module-resolution.mjs +65 -0
  69. package/dist/powerlines/src/lib/unplugin/plugin.cjs +71 -0
  70. package/dist/powerlines/src/lib/unplugin/plugin.mjs +70 -0
  71. package/dist/powerlines/src/lib/utilities/file-header.cjs +22 -0
  72. package/dist/powerlines/src/lib/utilities/file-header.mjs +21 -0
  73. package/dist/powerlines/src/lib/utilities/format.cjs +48 -0
  74. package/dist/powerlines/src/lib/utilities/format.mjs +46 -0
  75. package/dist/powerlines/src/lib/utilities/meta.cjs +44 -0
  76. package/dist/powerlines/src/lib/utilities/meta.mjs +40 -0
  77. package/dist/powerlines/src/lib/utilities/source-file.cjs +22 -0
  78. package/dist/powerlines/src/lib/utilities/source-file.mjs +21 -0
  79. package/dist/powerlines/src/plugin-utils/helpers.cjs +167 -0
  80. package/dist/powerlines/src/plugin-utils/helpers.mjs +156 -0
  81. package/dist/powerlines/src/plugin-utils/paths.cjs +36 -0
  82. package/dist/powerlines/src/plugin-utils/paths.mjs +35 -0
  83. package/dist/powerlines/src/types/api.d.cts +104 -0
  84. package/dist/powerlines/src/types/api.d.mts +104 -0
  85. package/dist/powerlines/src/types/babel.d.mts +2 -0
  86. package/dist/powerlines/src/types/build.cjs +23 -0
  87. package/dist/powerlines/src/types/build.d.cts +185 -0
  88. package/dist/powerlines/src/types/build.d.mts +185 -0
  89. package/dist/powerlines/src/types/build.mjs +21 -0
  90. package/dist/powerlines/src/types/commands.cjs +16 -0
  91. package/dist/powerlines/src/types/commands.d.cts +8 -0
  92. package/dist/powerlines/src/types/commands.d.mts +9 -0
  93. package/dist/powerlines/src/types/commands.mjs +15 -0
  94. package/dist/powerlines/src/types/config.d.cts +424 -0
  95. package/dist/powerlines/src/types/config.d.mts +425 -0
  96. package/dist/powerlines/src/types/context.d.cts +514 -0
  97. package/dist/powerlines/src/types/context.d.mts +514 -0
  98. package/dist/powerlines/src/types/fs.d.cts +486 -0
  99. package/dist/powerlines/src/types/fs.d.mts +486 -0
  100. package/dist/powerlines/src/types/hooks.d.cts +32 -0
  101. package/dist/powerlines/src/types/hooks.d.mts +32 -0
  102. package/dist/powerlines/src/types/plugin.cjs +33 -0
  103. package/dist/powerlines/src/types/plugin.d.cts +205 -0
  104. package/dist/powerlines/src/types/plugin.d.mts +205 -0
  105. package/dist/powerlines/src/types/plugin.mjs +32 -0
  106. package/dist/powerlines/src/types/resolved.d.cts +93 -0
  107. package/dist/powerlines/src/types/resolved.d.mts +93 -0
  108. package/dist/powerlines/src/types/tsconfig.d.cts +69 -0
  109. package/dist/powerlines/src/types/tsconfig.d.mts +69 -0
  110. package/dist/powerlines/src/types/unplugin.d.cts +22 -0
  111. package/dist/powerlines/src/types/unplugin.d.mts +23 -0
  112. package/dist/types/plugin.d.cts +3 -3
  113. package/dist/types/plugin.d.mts +3 -3
  114. package/package.json +4 -4
@@ -0,0 +1,566 @@
1
+ import { version } from "../package.mjs";
2
+ import { emitBuiltinTypes, formatTypes } from "./internal/helpers/generate-types.mjs";
3
+ import { callHook } from "./internal/helpers/hooks.mjs";
4
+ import { installDependencies } from "./internal/helpers/install-dependencies.mjs";
5
+ import { getParsedTypeScriptConfig, isIncludeMatchFound } from "./lib/typescript/tsconfig.mjs";
6
+ import { getTsconfigDtsPath, initializeTsconfig, resolveTsconfig } from "./internal/helpers/resolve-tsconfig.mjs";
7
+ import { checkDedupe, findInvalidPluginConfig, isPlugin, isPluginConfig, isPluginConfigObject, isPluginConfigTuple } from "./plugin-utils/helpers.mjs";
8
+ import { formatFolder } from "./lib/utilities/format.mjs";
9
+ import { writeMetaFile } from "./lib/utilities/meta.mjs";
10
+ import { PowerlinesAPIContext } from "./lib/contexts/api-context.mjs";
11
+ import { getFileHeader } from "./lib/utilities/file-header.mjs";
12
+ import { omit } from "@stryke/helpers/omit";
13
+ import { joinPaths } from "@stryke/path/join-paths";
14
+ import { formatLogMessage } from "@storm-software/config-tools/logger/console";
15
+ import { toArray } from "@stryke/convert/to-array";
16
+ import { createDirectory } from "@stryke/fs/helpers";
17
+ import { install } from "@stryke/fs/install";
18
+ import { listFiles } from "@stryke/fs/list-files";
19
+ import { isPackageExists } from "@stryke/fs/package-fns";
20
+ import { resolvePackage } from "@stryke/fs/resolve";
21
+ import { appendPath } from "@stryke/path/append";
22
+ import { replacePath } from "@stryke/path/replace";
23
+ import { isError } from "@stryke/type-checks/is-error";
24
+ import { isFunction } from "@stryke/type-checks/is-function";
25
+ import { isNumber } from "@stryke/type-checks/is-number";
26
+ import { isObject } from "@stryke/type-checks/is-object";
27
+ import { isPromiseLike } from "@stryke/type-checks/is-promise";
28
+ import { isSet } from "@stryke/type-checks/is-set";
29
+ import { isSetObject } from "@stryke/type-checks/is-set-object";
30
+ import { isSetString } from "@stryke/type-checks/is-set-string";
31
+ import { isString } from "@stryke/type-checks/is-string";
32
+ import chalk from "chalk";
33
+ import Handlebars from "handlebars";
34
+
35
+ //#region ../powerlines/src/api.ts
36
+ /**
37
+ * The Powerlines API class
38
+ *
39
+ * @remarks
40
+ * This class is responsible for managing the Powerlines project lifecycle, including initialization, building, and finalization.
41
+ *
42
+ * @public
43
+ */
44
+ var PowerlinesAPI = class PowerlinesAPI {
45
+ /**
46
+ * The Powerlines context
47
+ */
48
+ #context;
49
+ /**
50
+ * The Powerlines context
51
+ */
52
+ get context() {
53
+ return this.#context;
54
+ }
55
+ /**
56
+ * Create a new Powerlines API instance
57
+ *
58
+ * @param context - The Powerlines context
59
+ */
60
+ constructor(context) {
61
+ this.#context = context;
62
+ }
63
+ /**
64
+ * Initialize a Powerlines API instance
65
+ *
66
+ * @param workspaceRoot - The directory of the underlying workspace the Powerlines project exists in
67
+ * @param config - An object containing the configuration required to run Powerlines tasks.
68
+ * @returns A new instance of the Powerlines API
69
+ */
70
+ static async from(workspaceRoot, config) {
71
+ const api = new PowerlinesAPI(await PowerlinesAPIContext.from(workspaceRoot, config));
72
+ api.#context.$$internal = {
73
+ api,
74
+ addPlugin: api.#addPlugin.bind(api)
75
+ };
76
+ api.context.info(`🔌 The Powerlines Engine v${version} has started`);
77
+ for (const plugin of api.context.config.plugins ?? []) await api.#addPlugin(plugin);
78
+ if (api.context.plugins.length === 0) api.context.warn("No Powerlines plugins were specified in the options. Please ensure this is correct, as it is generally not recommended.");
79
+ const pluginConfig = await api.callHook("config", {
80
+ environment: await api.context.getEnvironment(),
81
+ sequential: true,
82
+ result: "merge"
83
+ });
84
+ await api.context.withUserConfig(pluginConfig, { isHighPriority: false });
85
+ return api;
86
+ }
87
+ /**
88
+ * Prepare the Powerlines API
89
+ *
90
+ * @remarks
91
+ * This method will prepare the Powerlines API for use, initializing any necessary resources.
92
+ *
93
+ * @param inlineConfig - The inline configuration for the prepare command
94
+ */
95
+ async prepare(inlineConfig = { command: "prepare" }) {
96
+ this.context.info(" 🏗️ Preparing the Powerlines project");
97
+ this.context.debug(" Aggregating configuration options for the Powerlines project");
98
+ await this.context.withInlineConfig(inlineConfig);
99
+ await this.#executeEnvironments(async (context) => {
100
+ context.debug(`Initializing the processing options for the Powerlines project.`);
101
+ await this.callHook("configResolved", {
102
+ environment: context,
103
+ order: "pre"
104
+ });
105
+ await initializeTsconfig(context);
106
+ await this.callHook("configResolved", {
107
+ environment: context,
108
+ order: "normal"
109
+ });
110
+ if (context.entry.length > 0) context.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 ? `: \n${context.entry.map((entry) => `- ${entry.file}${entry.output ? ` -> ${entry.output}` : ""}`).join(" \n")}` : ""}`);
111
+ else context.warn(`No entry files were found for the ${context.config.title} project. Please ensure this is correct. Powerlines plugins generally require at least one entry point to function properly.`);
112
+ await resolveTsconfig(context);
113
+ await installDependencies(context);
114
+ await this.callHook("configResolved", {
115
+ environment: context,
116
+ order: "post"
117
+ });
118
+ context.trace(`Powerlines configuration has been resolved: \n\n${formatLogMessage({
119
+ ...context.config,
120
+ userConfig: isSetObject(context.config.userConfig) ? omit(context.config.userConfig, ["plugins"]) : void 0,
121
+ inlineConfig: isSetObject(context.config.inlineConfig) ? omit(context.config.inlineConfig, ["plugins"]) : void 0,
122
+ plugins: context.plugins.map((plugin) => plugin.plugin.name)
123
+ })}`);
124
+ if (!context.fs.existsSync(context.cachePath)) await createDirectory(context.cachePath);
125
+ if (!context.fs.existsSync(context.dataPath)) await createDirectory(context.dataPath);
126
+ await this.callHook("prepare", {
127
+ environment: context,
128
+ order: "pre"
129
+ });
130
+ await this.callHook("prepare", {
131
+ environment: context,
132
+ order: "normal"
133
+ });
134
+ if (context.config.output.dts !== false) {
135
+ context.debug(`Preparing the TypeScript definitions for the Powerlines project.`);
136
+ if (context.fs.existsSync(context.dtsPath)) await context.fs.remove(context.dtsPath);
137
+ if (!await resolvePackage("typescript")) throw new Error("Could not resolve TypeScript package location. Please ensure TypeScript is installed.");
138
+ context.debug("Running TypeScript compiler for built-in runtime module files.");
139
+ let types = await emitBuiltinTypes(context, (await context.getBuiltins()).reduce((ret, builtin) => {
140
+ const formatted = replacePath(builtin.path, context.workspaceConfig.workspaceRoot);
141
+ if (!ret.includes(formatted)) ret.push(formatted);
142
+ return ret;
143
+ }, []));
144
+ context.debug(`Generating TypeScript declaration file ${context.dtsPath}.`);
145
+ const directives = [];
146
+ const asNextParam = (previousResult) => isObject(previousResult) ? previousResult.code : previousResult;
147
+ let result = await this.callHook("types", {
148
+ environment: context,
149
+ sequential: true,
150
+ order: "pre",
151
+ result: "merge",
152
+ asNextParam
153
+ }, types);
154
+ if (result) {
155
+ if (isSetObject(result)) {
156
+ types = result.code;
157
+ if (Array.isArray(result.directives) && result.directives.length > 0) directives.push(...result.directives);
158
+ } else if (isSetString(result)) types = result;
159
+ }
160
+ result = await this.callHook("types", {
161
+ environment: context,
162
+ sequential: true,
163
+ order: "normal",
164
+ result: "merge",
165
+ asNextParam
166
+ }, types);
167
+ if (result) {
168
+ if (isSetObject(result)) {
169
+ types = result.code;
170
+ if (Array.isArray(result.directives) && result.directives.length > 0) directives.push(...result.directives);
171
+ } else if (isSetString(result)) types = result;
172
+ }
173
+ result = await this.callHook("types", {
174
+ environment: context,
175
+ sequential: true,
176
+ order: "post",
177
+ result: "merge",
178
+ asNextParam
179
+ }, types);
180
+ if (result) {
181
+ if (isSetObject(result)) {
182
+ types = result.code;
183
+ if (Array.isArray(result.directives) && result.directives.length > 0) directives.push(...result.directives);
184
+ } else if (isSetString(result)) types = result;
185
+ }
186
+ if (isSetString(types?.trim()) || directives.length > 0) await context.fs.write(context.dtsPath, `${directives.length > 0 ? `${directives.map((directive) => `/// <reference types="${directive}" />`).join("\n")}
187
+
188
+ ` : ""}${getFileHeader(context, {
189
+ directive: null,
190
+ prettierIgnore: false
191
+ })}
192
+
193
+ ${formatTypes(types)}
194
+ `);
195
+ else {
196
+ const dtsRelativePath = getTsconfigDtsPath(context);
197
+ if (context.tsconfig.tsconfigJson.include && isIncludeMatchFound(dtsRelativePath, context.tsconfig.tsconfigJson.include)) {
198
+ const normalizedDtsRelativePath = dtsRelativePath.startsWith("./") ? dtsRelativePath.slice(2) : dtsRelativePath;
199
+ context.tsconfig.tsconfigJson.include = context.tsconfig.tsconfigJson.include.filter((includeValue) => includeValue?.toString() !== normalizedDtsRelativePath);
200
+ await context.fs.write(context.tsconfig.tsconfigFilePath, JSON.stringify(context.tsconfig.tsconfigJson, null, 2));
201
+ }
202
+ }
203
+ }
204
+ context.tsconfig = getParsedTypeScriptConfig(context.workspaceConfig.workspaceRoot, context.config.projectRoot, context.config.tsconfig);
205
+ if (!context.tsconfig) throw new Error("Failed to parse the TypeScript configuration file.");
206
+ this.context.debug("Formatting files generated during the prepare step.");
207
+ await Promise.all([formatFolder(context, context.builtinsPath), formatFolder(context, context.entryPath)]);
208
+ await this.callHook("prepare", {
209
+ environment: context,
210
+ order: "post"
211
+ });
212
+ await writeMetaFile(context);
213
+ context.persistedMeta = context.meta;
214
+ });
215
+ this.context.debug("✔ Powerlines preparation has completed successfully");
216
+ }
217
+ /**
218
+ * Create a new Powerlines project
219
+ *
220
+ * @remarks
221
+ * This method will create a new Powerlines project in the current directory.
222
+ *
223
+ * @param inlineConfig - The inline configuration for the new command
224
+ * @returns A promise that resolves when the project has been created
225
+ */
226
+ async new(inlineConfig) {
227
+ this.context.info("🆕 Creating a new Powerlines project");
228
+ await this.prepare(inlineConfig);
229
+ await this.#executeEnvironments(async (context) => {
230
+ context.debug("Initializing the processing options for the Powerlines project.");
231
+ await this.callHook("new", {
232
+ environment: context,
233
+ order: "pre"
234
+ });
235
+ const files = await listFiles(joinPaths(context.powerlinesPath, "files/common/**/*.hbs"));
236
+ for (const file of files) {
237
+ context.trace(`Adding template file to project: ${file}`);
238
+ const template = Handlebars.compile(file);
239
+ await context.fs.write(joinPaths(context.config.projectRoot, file.replace(".hbs", "")), template(context));
240
+ }
241
+ await this.callHook("new", {
242
+ environment: context,
243
+ order: "normal"
244
+ });
245
+ if (context.config.projectType === "application") {
246
+ const files$1 = await listFiles(joinPaths(context.powerlinesPath, "files/application/**/*.hbs"));
247
+ for (const file of files$1) {
248
+ context.trace(`Adding application template file: ${file}`);
249
+ const template = Handlebars.compile(file);
250
+ await context.fs.write(joinPaths(context.config.projectRoot, file.replace(".hbs", "")), template(context));
251
+ }
252
+ } else {
253
+ const files$1 = await listFiles(joinPaths(context.powerlinesPath, "files/library/**/*.hbs"));
254
+ for (const file of files$1) {
255
+ context.trace(`Adding library template file: ${file}`);
256
+ const template = Handlebars.compile(file);
257
+ await context.fs.write(joinPaths(context.config.projectRoot, file.replace(".hbs", "")), template(context));
258
+ }
259
+ }
260
+ await this.callHook("new", {
261
+ environment: context,
262
+ order: "post"
263
+ });
264
+ });
265
+ this.context.debug("✔ Powerlines new command completed successfully");
266
+ }
267
+ /**
268
+ * Clean any previously prepared artifacts
269
+ *
270
+ * @remarks
271
+ * This method will remove the previous Powerlines artifacts from the project.
272
+ *
273
+ * @param inlineConfig - The inline configuration for the clean command
274
+ * @returns A promise that resolves when the clean command has completed
275
+ */
276
+ async clean(inlineConfig = { command: "clean" }) {
277
+ this.context.info("🧹 Cleaning the previous Powerlines artifacts");
278
+ await this.prepare(inlineConfig);
279
+ await this.#executeEnvironments(async (context) => {
280
+ context.debug("Cleaning the project's dist and artifacts directories.");
281
+ await context.fs.remove(joinPaths(context.workspaceConfig.workspaceRoot, context.config.output.buildPath));
282
+ await context.fs.remove(joinPaths(context.workspaceConfig.workspaceRoot, context.config.projectRoot, context.config.output.artifactsPath));
283
+ await this.callHook("clean", {
284
+ environment: context,
285
+ sequential: false
286
+ });
287
+ });
288
+ this.context.debug("✔ Powerlines cleaning completed successfully");
289
+ }
290
+ /**
291
+ * Lint the project
292
+ *
293
+ * @param inlineConfig - The inline configuration for the lint command
294
+ * @returns A promise that resolves when the lint command has completed
295
+ */
296
+ async lint(inlineConfig = { command: "lint" }) {
297
+ this.context.info("📋 Linting the Powerlines project");
298
+ await this.prepare(inlineConfig);
299
+ await this.#executeEnvironments(async (context) => {
300
+ if (context.config.lint !== false) await this.callHook("lint", {
301
+ environment: context,
302
+ sequential: false
303
+ });
304
+ });
305
+ this.context.debug("✔ Powerlines linting completed successfully");
306
+ }
307
+ /**
308
+ * Build the project
309
+ *
310
+ * @remarks
311
+ * This method will build the Powerlines project, generating the necessary artifacts.
312
+ *
313
+ * @param inlineConfig - The inline configuration for the build command
314
+ * @returns A promise that resolves when the build command has completed
315
+ */
316
+ async build(inlineConfig = { command: "build" }) {
317
+ this.context.info("📦 Building the Powerlines project");
318
+ await this.prepare(inlineConfig);
319
+ if (this.context.config.singleBuild) await this.#handleBuild(await this.#context.toEnvironment());
320
+ else await this.#executeEnvironments(async (context) => {
321
+ await this.#handleBuild(context);
322
+ });
323
+ this.context.debug("✔ Powerlines build completed successfully");
324
+ }
325
+ /**
326
+ * Prepare the documentation for the project
327
+ *
328
+ * @param inlineConfig - The inline configuration for the docs command
329
+ * @returns A promise that resolves when the documentation generation has completed
330
+ */
331
+ async docs(inlineConfig = { command: "docs" }) {
332
+ this.context.info("📓 Generating documentation for the Powerlines project");
333
+ await this.prepare(inlineConfig);
334
+ await this.#executeEnvironments(async (context) => {
335
+ context.debug("Writing documentation for the Powerlines project artifacts.");
336
+ await this.prepare(inlineConfig);
337
+ await this.#executeEnvironments(async (context$1) => {
338
+ await this.callHook("docs", { environment: context$1 });
339
+ });
340
+ });
341
+ this.context.debug("✔ Powerlines documentation generation completed successfully");
342
+ }
343
+ /**
344
+ * Deploy the project source code
345
+ *
346
+ * @remarks
347
+ * This method will prepare and build the Powerlines project, generating the necessary artifacts for the deployment.
348
+ *
349
+ * @param inlineConfig - The inline configuration for the deploy command
350
+ */
351
+ async deploy(inlineConfig = { command: "deploy" }) {
352
+ this.context.info("🚀 Deploying the Powerlines project");
353
+ await this.prepare(inlineConfig);
354
+ await this.#executeEnvironments(async (context) => {
355
+ await this.callHook("deploy", { environment: context });
356
+ });
357
+ this.context.debug("✔ Powerlines deploy completed successfully");
358
+ }
359
+ /**
360
+ * Finalization process
361
+ *
362
+ * @remarks
363
+ * This step includes any final processes or clean up required by Powerlines. It will be run after each Powerlines command.
364
+ *
365
+ * @returns A promise that resolves when the finalization process has completed
366
+ */
367
+ async finalize() {
368
+ this.context.info("🏁 Powerlines finalization processes started");
369
+ await this.#executeEnvironments(async (context) => {
370
+ await this.callHook("finalize", { environment: context });
371
+ await context.fs.dispose();
372
+ });
373
+ this.context.debug("✔ Powerlines finalization completed successfully");
374
+ }
375
+ /**
376
+ * Invokes the configured plugin hooks
377
+ *
378
+ * @remarks
379
+ * By default, it will call the `"pre"`, `"normal"`, and `"post"` ordered hooks in sequence
380
+ *
381
+ * @param hook - The hook to call
382
+ * @param options - The options to provide to the hook
383
+ * @param args - The arguments to pass to the hook
384
+ * @returns The result of the hook call
385
+ */
386
+ async callHook(hook, options, ...args) {
387
+ return callHook(isSetObject(options?.environment) ? options.environment : await this.#context.getEnvironment(options?.environment), hook, {
388
+ sequential: true,
389
+ ...options
390
+ }, ...args);
391
+ }
392
+ /**
393
+ * Dispose of the Powerlines API instance
394
+ *
395
+ * @remarks
396
+ * This method will finalize the Powerlines API instance, cleaning up any resources used.
397
+ */
398
+ async [Symbol.asyncDispose]() {
399
+ await this.finalize();
400
+ }
401
+ async #handleBuild(context) {
402
+ await this.callHook("build", {
403
+ environment: context,
404
+ order: "pre"
405
+ });
406
+ context.debug("Formatting the generated entry files before the build process starts.");
407
+ await formatFolder(context, context.entryPath);
408
+ await this.callHook("build", {
409
+ environment: context,
410
+ order: "normal"
411
+ });
412
+ if (context.config.output.buildPath !== context.config.output.outputPath) {
413
+ const sourcePath = appendPath(context.config.output.buildPath, context.workspaceConfig.workspaceRoot);
414
+ const destinationPath = joinPaths(appendPath(context.config.output.outputPath, context.workspaceConfig.workspaceRoot), "dist");
415
+ if (context.fs.existsSync(sourcePath) && sourcePath !== destinationPath) {
416
+ context.debug(`Copying build output files from project's build directory (${context.config.output.buildPath}) to the workspace's output directory (${context.config.output.outputPath}).`);
417
+ await context.fs.copy(sourcePath, destinationPath);
418
+ }
419
+ }
420
+ await Promise.all(context.config.output.assets.map(async (asset) => {
421
+ context.trace(`Copying asset(s): ${chalk.redBright(context.workspaceConfig.workspaceRoot === asset.input ? asset.glob : joinPaths(replacePath(asset.input, context.workspaceConfig.workspaceRoot), asset.glob))} -> ${chalk.greenBright(joinPaths(replacePath(asset.output, context.workspaceConfig.workspaceRoot), asset.glob))} ${Array.isArray(asset.ignore) && asset.ignore.length > 0 ? ` (ignoring: ${asset.ignore.map((i) => chalk.yellowBright(i)).join(", ")})` : ""}`);
422
+ await context.fs.copy(asset, asset.output);
423
+ }));
424
+ await this.callHook("build", {
425
+ environment: context,
426
+ order: "post"
427
+ });
428
+ }
429
+ /**
430
+ * Get the configured environments
431
+ *
432
+ * @returns The configured environments
433
+ */
434
+ async #getEnvironments() {
435
+ if (!this.context.config.environments || Object.keys(this.context.config.environments).length <= 1) {
436
+ this.context.debug("No environments are configured for this Powerlines project. Using the default environment.");
437
+ return [await this.context.getEnvironment()];
438
+ }
439
+ this.context.debug(`Found ${Object.keys(this.context.config.environments).length} configured environment(s) for this Powerlines project.`);
440
+ return (await Promise.all(Object.entries(this.context.config.environments).map(async ([name, config]) => {
441
+ if (!await this.context.getEnvironmentSafe(name)) {
442
+ const resolvedEnvironment = await this.callHook("configEnvironment", { environment: name }, name, config);
443
+ if (resolvedEnvironment) this.context.environments[name] = await this.context.in(resolvedEnvironment);
444
+ }
445
+ return this.context.environments[name];
446
+ }))).filter((context) => isSet(context));
447
+ }
448
+ /**
449
+ * Execute a handler function for each environment
450
+ *
451
+ * @param handle - The handler function to execute for each environment
452
+ */
453
+ async #executeEnvironments(handle) {
454
+ await Promise.all((await this.#getEnvironments()).map(async (context) => {
455
+ return Promise.resolve(handle(context));
456
+ }));
457
+ }
458
+ /**
459
+ * Add a Powerlines plugin used in the build process
460
+ *
461
+ * @param config - The import path of the plugin to add
462
+ */
463
+ async #addPlugin(config) {
464
+ if (config) {
465
+ const result = await this.#initPlugin(config);
466
+ if (!result) return;
467
+ for (const plugin of result) {
468
+ this.context.debug(`Successfully initialized the ${chalk.bold.cyanBright(plugin.name)} plugin`);
469
+ await this.context.addPlugin(plugin);
470
+ }
471
+ }
472
+ }
473
+ /**
474
+ * Initialize a Powerlines plugin
475
+ *
476
+ * @param config - The configuration for the plugin
477
+ * @returns The initialized plugin instance, or null if the plugin was a duplicate
478
+ * @throws Will throw an error if the plugin cannot be found or is invalid
479
+ */
480
+ async #initPlugin(config) {
481
+ let awaited = config;
482
+ if (isPromiseLike(config)) awaited = await Promise.resolve(config);
483
+ if (!isPluginConfig(awaited)) {
484
+ const invalid = findInvalidPluginConfig(awaited);
485
+ throw new Error(`Invalid ${invalid && invalid.length > 1 ? "plugins" : "plugin"} specified in the configuration - ${invalid && invalid.length > 0 ? JSON.stringify(awaited) : invalid?.join("\n\n")} \n\nPlease ensure the value is one of the following: \n - an instance of \`Plugin\` \n - a plugin name \n - an object with the \`plugin\` and \`options\` properties \n - a tuple array with the plugin and options \n - a factory function that returns a plugin or array of plugins \n - an array of plugins or plugin configurations`);
486
+ }
487
+ let plugins;
488
+ if (isPlugin(awaited)) plugins = [awaited];
489
+ else if (isFunction(awaited)) plugins = toArray(await Promise.resolve(awaited()));
490
+ else if (isString(awaited)) {
491
+ const resolved = await this.#resolvePlugin(awaited);
492
+ if (isFunction(resolved)) plugins = toArray(await Promise.resolve(resolved()));
493
+ else plugins = toArray(resolved);
494
+ } else if (Array.isArray(awaited) && awaited.every(isPlugin)) plugins = awaited;
495
+ else if (Array.isArray(awaited) && awaited.every(isPluginConfig)) {
496
+ plugins = [];
497
+ for (const pluginConfig of awaited) {
498
+ const initialized = await this.#initPlugin(pluginConfig);
499
+ if (initialized) plugins.push(...initialized);
500
+ }
501
+ } else if (isPluginConfigTuple(awaited) || isPluginConfigObject(awaited)) {
502
+ let pluginConfig;
503
+ let pluginOptions;
504
+ if (isPluginConfigTuple(awaited)) {
505
+ pluginConfig = awaited[0];
506
+ pluginOptions = awaited?.length === 2 ? awaited[1] : void 0;
507
+ } else {
508
+ pluginConfig = awaited.plugin;
509
+ pluginOptions = awaited.options;
510
+ }
511
+ if (isSetString(pluginConfig)) {
512
+ const resolved = await this.#resolvePlugin(pluginConfig);
513
+ if (isFunction(resolved)) plugins = toArray(await Promise.resolve(pluginOptions ? resolved(pluginOptions) : resolved()));
514
+ else plugins = toArray(resolved);
515
+ } else if (isFunction(pluginConfig)) plugins = toArray(await Promise.resolve(pluginConfig(pluginOptions)));
516
+ else if (Array.isArray(pluginConfig) && pluginConfig.every(isPlugin)) plugins = pluginConfig;
517
+ else if (isPlugin(pluginConfig)) plugins = toArray(pluginConfig);
518
+ }
519
+ if (!plugins) throw new Error(`The plugin configuration ${JSON.stringify(awaited)} is invalid. This configuration must point to a valid Powerlines plugin module.`);
520
+ if (plugins.length > 0 && !plugins.every(isPlugin)) throw new Error(`The plugin option ${JSON.stringify(plugins)} does not export a valid module. This configuration must point to a valid Powerlines plugin module.`);
521
+ const result = [];
522
+ for (const plugin of plugins) if (checkDedupe(plugin, this.context.plugins)) this.context.trace(`Duplicate ${chalk.bold.cyanBright(plugin.name)} plugin dependency detected - Skipping initialization.`);
523
+ else {
524
+ result.push(plugin);
525
+ this.context.trace(`Initializing the ${chalk.bold.cyanBright(plugin.name)} plugin...`);
526
+ }
527
+ return result;
528
+ }
529
+ async #resolvePlugin(pluginPath) {
530
+ if (pluginPath.startsWith("@") && pluginPath.split("/").filter(Boolean).length > 2) {
531
+ const splits = pluginPath.split("/").filter(Boolean);
532
+ pluginPath = `${splits[0]}/${splits[1]}`;
533
+ }
534
+ const isInstalled = isPackageExists(pluginPath, { paths: [this.context.workspaceConfig.workspaceRoot, this.context.config.projectRoot] });
535
+ if (!isInstalled && this.context.config.autoInstall) {
536
+ this.#context.warn(`The plugin package "${pluginPath}" is not installed. It will be installed automatically.`);
537
+ const result = await install(pluginPath, { cwd: this.context.config.projectRoot });
538
+ if (isNumber(result.exitCode) && result.exitCode > 0) {
539
+ this.#context.error(result.stderr);
540
+ throw new Error(`An error occurred while installing the build plugin package "${pluginPath}" `);
541
+ }
542
+ }
543
+ try {
544
+ const module = await this.context.resolver.plugin.import(this.context.resolver.plugin.esmResolve(joinPaths(pluginPath, "plugin")));
545
+ const result = module.plugin ?? module.default;
546
+ if (!result) throw new Error(`The plugin package "${pluginPath}" does not export a valid module.`);
547
+ return result;
548
+ } catch (error) {
549
+ try {
550
+ const module = await this.context.resolver.plugin.import(this.context.resolver.plugin.esmResolve(pluginPath));
551
+ const result = module.plugin ?? module.default;
552
+ if (!result) throw new Error(`The plugin package "${pluginPath}" does not export a valid module.`);
553
+ return result;
554
+ } catch {
555
+ if (!isInstalled) throw new Error(`The plugin package "${pluginPath}" is not installed. Please install the package using the command: "npm install ${pluginPath} --save-dev"`);
556
+ else throw new Error(`An error occurred while importing the build plugin package "${pluginPath}":
557
+ ${isError(error) ? error.message : String(error)}
558
+
559
+ 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\`.`);
560
+ }
561
+ }
562
+ }
563
+ };
564
+
565
+ //#endregion
566
+ export { };
@@ -0,0 +1,50 @@
1
+ const require_rolldown_runtime = require('../../../../_virtual/rolldown_runtime.cjs');
2
+ const require_environments = require('../../lib/constants/environments.cjs');
3
+ let defu = require("defu");
4
+ defu = require_rolldown_runtime.__toESM(defu);
5
+ let __stryke_string_format_title_case = require("@stryke/string-format/title-case");
6
+
7
+ //#region ../powerlines/src/internal/helpers/environment.ts
8
+ function createEnvironment(name, userConfig) {
9
+ return (0, defu.default)(userConfig.environments?.[name] ?? {}, {
10
+ name,
11
+ title: userConfig.title || (0, __stryke_string_format_title_case.titleCase)(userConfig.name),
12
+ ssr: false,
13
+ mainFields: userConfig.build?.platform === "browser" ? [
14
+ "browser",
15
+ "module",
16
+ "jsnext:main",
17
+ "jsnext"
18
+ ] : [
19
+ "module",
20
+ "jsnext:main",
21
+ "jsnext"
22
+ ],
23
+ extensions: [
24
+ ".mjs",
25
+ ".js",
26
+ ".mts",
27
+ ".ts",
28
+ ".jsx",
29
+ ".tsx",
30
+ ".json"
31
+ ],
32
+ consumer: userConfig.build?.platform === "browser" ? "client" : "server",
33
+ preview: userConfig.build?.platform === "browser" ? {
34
+ port: 5173,
35
+ open: true,
36
+ strictPort: false,
37
+ host: "localhost",
38
+ allowedHosts: ["."],
39
+ cors: true,
40
+ headers: {}
41
+ } : void 0
42
+ }, userConfig);
43
+ }
44
+ function createDefaultEnvironment(userConfig) {
45
+ return createEnvironment(require_environments.DEFAULT_ENVIRONMENT, userConfig);
46
+ }
47
+
48
+ //#endregion
49
+ exports.createDefaultEnvironment = createDefaultEnvironment;
50
+ exports.createEnvironment = createEnvironment;
@@ -0,0 +1,47 @@
1
+ import { DEFAULT_ENVIRONMENT } from "../../lib/constants/environments.mjs";
2
+ import defu$1 from "defu";
3
+ import { titleCase } from "@stryke/string-format/title-case";
4
+
5
+ //#region ../powerlines/src/internal/helpers/environment.ts
6
+ function createEnvironment(name, userConfig) {
7
+ return defu$1(userConfig.environments?.[name] ?? {}, {
8
+ name,
9
+ title: userConfig.title || titleCase(userConfig.name),
10
+ ssr: false,
11
+ mainFields: userConfig.build?.platform === "browser" ? [
12
+ "browser",
13
+ "module",
14
+ "jsnext:main",
15
+ "jsnext"
16
+ ] : [
17
+ "module",
18
+ "jsnext:main",
19
+ "jsnext"
20
+ ],
21
+ extensions: [
22
+ ".mjs",
23
+ ".js",
24
+ ".mts",
25
+ ".ts",
26
+ ".jsx",
27
+ ".tsx",
28
+ ".json"
29
+ ],
30
+ consumer: userConfig.build?.platform === "browser" ? "client" : "server",
31
+ preview: userConfig.build?.platform === "browser" ? {
32
+ port: 5173,
33
+ open: true,
34
+ strictPort: false,
35
+ host: "localhost",
36
+ allowedHosts: ["."],
37
+ cors: true,
38
+ headers: {}
39
+ } : void 0
40
+ }, userConfig);
41
+ }
42
+ function createDefaultEnvironment(userConfig) {
43
+ return createEnvironment(DEFAULT_ENVIRONMENT, userConfig);
44
+ }
45
+
46
+ //#endregion
47
+ export { createDefaultEnvironment, createEnvironment };