@powerlines/engine 0.15.0 → 0.15.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (195) hide show
  1. package/dist/api/build.cjs +66 -0
  2. package/dist/api/build.d.cts +14 -0
  3. package/dist/api/build.d.cts.map +1 -0
  4. package/dist/api/build.d.mts +14 -0
  5. package/dist/api/build.d.mts.map +1 -0
  6. package/dist/api/build.mjs +64 -0
  7. package/dist/api/build.mjs.map +1 -0
  8. package/dist/api/clean.cjs +29 -0
  9. package/dist/api/clean.d.cts +14 -0
  10. package/dist/api/clean.d.cts.map +1 -0
  11. package/dist/api/clean.d.mts +14 -0
  12. package/dist/api/clean.d.mts.map +1 -0
  13. package/dist/api/clean.mjs +28 -0
  14. package/dist/api/clean.mjs.map +1 -0
  15. package/dist/api/create.cjs +59 -0
  16. package/dist/api/create.d.cts +14 -0
  17. package/dist/api/create.d.cts.map +1 -0
  18. package/dist/api/create.d.mts +14 -0
  19. package/dist/api/create.d.mts.map +1 -0
  20. package/dist/api/create.mjs +57 -0
  21. package/dist/api/create.mjs.map +1 -0
  22. package/dist/api/deploy.cjs +25 -0
  23. package/dist/api/deploy.d.cts +17 -0
  24. package/dist/api/deploy.d.cts.map +1 -0
  25. package/dist/api/deploy.d.mts +17 -0
  26. package/dist/api/deploy.d.mts.map +1 -0
  27. package/dist/api/deploy.mjs +24 -0
  28. package/dist/api/deploy.mjs.map +1 -0
  29. package/dist/api/docs.cjs +25 -0
  30. package/dist/api/docs.d.cts +17 -0
  31. package/dist/api/docs.d.cts.map +1 -0
  32. package/dist/api/docs.d.mts +17 -0
  33. package/dist/api/docs.d.mts.map +1 -0
  34. package/dist/api/docs.mjs +24 -0
  35. package/dist/api/docs.mjs.map +1 -0
  36. package/dist/api/lint.cjs +28 -0
  37. package/dist/api/lint.d.cts +17 -0
  38. package/dist/api/lint.d.cts.map +1 -0
  39. package/dist/api/lint.d.mts +17 -0
  40. package/dist/api/lint.d.mts.map +1 -0
  41. package/dist/api/lint.mjs +27 -0
  42. package/dist/api/lint.mjs.map +1 -0
  43. package/dist/api/prepare.cjs +69 -0
  44. package/dist/api/prepare.d.cts +14 -0
  45. package/dist/api/prepare.d.cts.map +1 -0
  46. package/dist/api/prepare.d.mts +14 -0
  47. package/dist/api/prepare.d.mts.map +1 -0
  48. package/dist/api/prepare.mjs +68 -0
  49. package/dist/api/prepare.mjs.map +1 -0
  50. package/dist/api/test.cjs +28 -0
  51. package/dist/api/test.d.cts +17 -0
  52. package/dist/api/test.d.cts.map +1 -0
  53. package/dist/api/test.d.mts +17 -0
  54. package/dist/api/test.d.mts.map +1 -0
  55. package/dist/api/test.mjs +27 -0
  56. package/dist/api/test.mjs.map +1 -0
  57. package/dist/api/types.cjs +71 -0
  58. package/dist/api/types.d.cts +17 -0
  59. package/dist/api/types.d.cts.map +1 -0
  60. package/dist/api/types.d.mts +17 -0
  61. package/dist/api/types.d.mts.map +1 -0
  62. package/dist/api/types.mjs +70 -0
  63. package/dist/api/types.mjs.map +1 -0
  64. package/dist/api-6w4hZL6n.d.cts +135 -0
  65. package/dist/api-6w4hZL6n.d.cts.map +1 -0
  66. package/dist/api-Cdw4v0W4.d.mts +135 -0
  67. package/dist/api-Cdw4v0W4.d.mts.map +1 -0
  68. package/dist/config-BNe23XHx.d.mts +204 -0
  69. package/dist/config-BNe23XHx.d.mts.map +1 -0
  70. package/dist/config-D6xUniHh.d.cts +204 -0
  71. package/dist/config-D6xUniHh.d.cts.map +1 -0
  72. package/dist/context/engine-context.cjs +175 -0
  73. package/dist/context/engine-context.d.cts +2 -0
  74. package/dist/context/engine-context.d.mts +2 -0
  75. package/dist/context/engine-context.mjs +173 -0
  76. package/dist/context/engine-context.mjs.map +1 -0
  77. package/dist/context/index.cjs +2 -9
  78. package/dist/context/index.d.cts +2 -660
  79. package/dist/context/index.d.mts +2 -660
  80. package/dist/context/index.mjs +2 -4
  81. package/dist/context-DzgsMSWr.d.mts +149 -0
  82. package/dist/context-DzgsMSWr.d.mts.map +1 -0
  83. package/dist/context-epL7NPvL.d.cts +149 -0
  84. package/dist/context-epL7NPvL.d.cts.map +1 -0
  85. package/dist/engine-context-DEotmVzB.d.mts +54 -0
  86. package/dist/engine-context-DEotmVzB.d.mts.map +1 -0
  87. package/dist/engine-context-Dw8odBCo.d.cts +54 -0
  88. package/dist/engine-context-Dw8odBCo.d.cts.map +1 -0
  89. package/dist/engine.cjs +268 -0
  90. package/dist/engine.d.cts +143 -0
  91. package/dist/engine.d.cts.map +1 -0
  92. package/dist/engine.d.mts +143 -0
  93. package/dist/engine.d.mts.map +1 -0
  94. package/dist/engine.mjs +264 -0
  95. package/dist/engine.mjs.map +1 -0
  96. package/dist/execution-host.cjs +44 -0
  97. package/dist/execution-host.d.cts +14 -0
  98. package/dist/execution-host.d.cts.map +1 -0
  99. package/dist/execution-host.d.mts +14 -0
  100. package/dist/execution-host.d.mts.map +1 -0
  101. package/dist/execution-host.mjs +36 -0
  102. package/dist/execution-host.mjs.map +1 -0
  103. package/dist/helpers/create-execution-host.cjs +51 -0
  104. package/dist/helpers/create-execution-host.d.cts +21 -0
  105. package/dist/helpers/create-execution-host.d.cts.map +1 -0
  106. package/dist/helpers/create-execution-host.d.mts +21 -0
  107. package/dist/helpers/create-execution-host.d.mts.map +1 -0
  108. package/dist/helpers/create-execution-host.mjs +50 -0
  109. package/dist/helpers/create-execution-host.mjs.map +1 -0
  110. package/dist/helpers/execution-host-worker.cjs +308 -0
  111. package/dist/helpers/execution-host-worker.d.cts +68 -0
  112. package/dist/helpers/execution-host-worker.d.cts.map +1 -0
  113. package/dist/helpers/execution-host-worker.d.mts +68 -0
  114. package/dist/helpers/execution-host-worker.d.mts.map +1 -0
  115. package/dist/helpers/execution-host-worker.mjs +307 -0
  116. package/dist/helpers/execution-host-worker.mjs.map +1 -0
  117. package/dist/helpers/finalize.cjs +27 -0
  118. package/dist/helpers/finalize.d.cts +12 -0
  119. package/dist/helpers/finalize.d.cts.map +1 -0
  120. package/dist/helpers/finalize.d.mts +12 -0
  121. package/dist/helpers/finalize.d.mts.map +1 -0
  122. package/dist/helpers/finalize.mjs +26 -0
  123. package/dist/helpers/finalize.mjs.map +1 -0
  124. package/dist/helpers/rpc.cjs +140 -0
  125. package/dist/helpers/rpc.d.cts +7 -0
  126. package/dist/helpers/rpc.d.cts.map +1 -0
  127. package/dist/helpers/rpc.d.mts +7 -0
  128. package/dist/helpers/rpc.d.mts.map +1 -0
  129. package/dist/helpers/rpc.mjs +139 -0
  130. package/dist/helpers/rpc.mjs.map +1 -0
  131. package/dist/index-CNgSR_kt.d.mts +1 -0
  132. package/dist/index-D_TYgLX3.d.cts +1 -0
  133. package/dist/index.cjs +13 -567
  134. package/dist/index.d.cts +9 -126
  135. package/dist/index.d.mts +9 -126
  136. package/dist/index.mjs +7 -567
  137. package/package.json +101 -129
  138. package/dist/_internal/worker.cjs +0 -4843
  139. package/dist/_internal/worker.d.cts +0 -38
  140. package/dist/_internal/worker.d.cts.map +0 -1
  141. package/dist/_internal/worker.d.mts +0 -38
  142. package/dist/_internal/worker.d.mts.map +0 -1
  143. package/dist/_internal/worker.mjs +0 -4804
  144. package/dist/_internal/worker.mjs.map +0 -1
  145. package/dist/api.cjs +0 -1274
  146. package/dist/api.d.cts +0 -265
  147. package/dist/api.d.cts.map +0 -1
  148. package/dist/api.d.mts +0 -265
  149. package/dist/api.d.mts.map +0 -1
  150. package/dist/api.mjs +0 -1270
  151. package/dist/api.mjs.map +0 -1
  152. package/dist/base-context-BUnL_9z8.mjs +0 -241
  153. package/dist/base-context-BUnL_9z8.mjs.map +0 -1
  154. package/dist/base-context-CFvO2N9I.cjs +0 -248
  155. package/dist/context/index.d.cts.map +0 -1
  156. package/dist/context/index.d.mts.map +0 -1
  157. package/dist/engine-context-BuD9AGfd.mjs +0 -41
  158. package/dist/engine-context-BuD9AGfd.mjs.map +0 -1
  159. package/dist/engine-context-NO6enYev.cjs +0 -45
  160. package/dist/execution-context-BgGV4xyW.cjs +0 -2614
  161. package/dist/execution-context-D_CXpe9I.mjs +0 -2570
  162. package/dist/execution-context-D_CXpe9I.mjs.map +0 -1
  163. package/dist/fs-D1nIP45P.mjs +0 -226
  164. package/dist/fs-D1nIP45P.mjs.map +0 -1
  165. package/dist/fs-XogSgMqT.cjs +0 -262
  166. package/dist/index.d.cts.map +0 -1
  167. package/dist/index.d.mts.map +0 -1
  168. package/dist/index.mjs.map +0 -1
  169. package/dist/schemas.cjs +0 -9
  170. package/dist/schemas.d.cts +0 -127
  171. package/dist/schemas.d.cts.map +0 -1
  172. package/dist/schemas.d.mts +0 -127
  173. package/dist/schemas.d.mts.map +0 -1
  174. package/dist/schemas.mjs +0 -3
  175. package/dist/storage/index.cjs +0 -9
  176. package/dist/storage/index.d.cts +0 -419
  177. package/dist/storage/index.d.cts.map +0 -1
  178. package/dist/storage/index.d.mts +0 -419
  179. package/dist/storage/index.d.mts.map +0 -1
  180. package/dist/storage/index.mjs +0 -3
  181. package/dist/ts-morph-BaLPVAdB.cjs +0 -114
  182. package/dist/ts-morph-D0CaA37w.mjs +0 -102
  183. package/dist/ts-morph-D0CaA37w.mjs.map +0 -1
  184. package/dist/tsconfig-Cstsoprg.mjs +0 -155
  185. package/dist/tsconfig-Cstsoprg.mjs.map +0 -1
  186. package/dist/tsconfig-DeyWQC2N.cjs +0 -198
  187. package/dist/typescript/index.cjs +0 -13
  188. package/dist/typescript/index.d.cts +0 -106
  189. package/dist/typescript/index.d.cts.map +0 -1
  190. package/dist/typescript/index.d.mts +0 -106
  191. package/dist/typescript/index.d.mts.map +0 -1
  192. package/dist/typescript/index.mjs +0 -4
  193. package/dist/virtual-BNdKVkRw.cjs +0 -548
  194. package/dist/virtual-gIlTc3Lj.mjs +0 -513
  195. package/dist/virtual-gIlTc3Lj.mjs.map +0 -1
@@ -1,4843 +0,0 @@
1
- #!/usr/bin/env -S NODE_OPTIONS=--enable-source-maps node
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
3
- //#region \0rolldown/runtime.js
4
- var __create = Object.create;
5
- var __defProp = Object.defineProperty;
6
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7
- var __getOwnPropNames = Object.getOwnPropertyNames;
8
- var __getProtoOf = Object.getPrototypeOf;
9
- var __hasOwnProp = Object.prototype.hasOwnProperty;
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
13
- key = keys[i];
14
- if (!__hasOwnProp.call(to, key) && key !== except) {
15
- __defProp(to, key, {
16
- get: ((k) => from[k]).bind(null, key),
17
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
18
- });
19
- }
20
- }
21
- }
22
- return to;
23
- };
24
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
25
- value: mod,
26
- enumerable: true
27
- }) : target, mod));
28
-
29
- //#endregion
30
- let _powerlines_core_lib_logger = require("@powerlines/core/lib/logger");
31
- let _powerlines_core_lib_utilities_file_header = require("@powerlines/core/lib/utilities/file-header");
32
- let _powerlines_core_lib_utilities_format = require("@powerlines/core/lib/utilities/format");
33
- let _powerlines_core_plugin_utils = require("@powerlines/core/plugin-utils");
34
- let _storm_software_config_tools_logger_console = require("@storm-software/config-tools/logger/console");
35
- let _stryke_convert_to_array = require("@stryke/convert/to-array");
36
- let _stryke_fs_copy_file = require("@stryke/fs/copy-file");
37
- let _stryke_fs_exists = require("@stryke/fs/exists");
38
- let _stryke_fs_helpers = require("@stryke/fs/helpers");
39
- let _stryke_fs_install = require("@stryke/fs/install");
40
- let _stryke_fs_list_files = require("@stryke/fs/list-files");
41
- let _stryke_fs_package_fns = require("@stryke/fs/package-fns");
42
- let _stryke_fs_resolve = require("@stryke/fs/resolve");
43
- let _stryke_helpers_get_unique = require("@stryke/helpers/get-unique");
44
- let _stryke_helpers_omit = require("@stryke/helpers/omit");
45
- let _stryke_path_append = require("@stryke/path/append");
46
- let _stryke_path_file_path_fns = require("@stryke/path/file-path-fns");
47
- let _stryke_path_is_parent_path = require("@stryke/path/is-parent-path");
48
- let _stryke_path_join_paths = require("@stryke/path/join-paths");
49
- let _stryke_path_replace = require("@stryke/path/replace");
50
- let _stryke_string_format_title_case = require("@stryke/string-format/title-case");
51
- let _stryke_type_checks_is_error = require("@stryke/type-checks/is-error");
52
- let _stryke_type_checks_is_function = require("@stryke/type-checks/is-function");
53
- let _stryke_type_checks_is_number = require("@stryke/type-checks/is-number");
54
- let _stryke_type_checks_is_object = require("@stryke/type-checks/is-object");
55
- let _stryke_type_checks_is_promise = require("@stryke/type-checks/is-promise");
56
- let _stryke_type_checks_is_set = require("@stryke/type-checks/is-set");
57
- let _stryke_type_checks_is_set_object = require("@stryke/type-checks/is-set-object");
58
- let _stryke_type_checks_is_set_string = require("@stryke/type-checks/is-set-string");
59
- let _stryke_type_checks_is_string = require("@stryke/type-checks/is-string");
60
- let chalk = require("chalk");
61
- chalk = __toESM(chalk, 1);
62
- let defu = require("defu");
63
- defu = __toESM(defu, 1);
64
- let handlebars = require("handlebars");
65
- handlebars = __toESM(handlebars, 1);
66
- let _powerlines_core_constants = require("@powerlines/core/constants");
67
- let _stryke_fs_json = require("@stryke/fs/json");
68
- let _stryke_helpers_deep_clone = require("@stryke/helpers/deep-clone");
69
- let _stryke_path_join = require("@stryke/path/join");
70
- let _powerlines_core_lib_entry = require("@powerlines/core/lib/entry");
71
- let _stryke_fs_get_workspace_root = require("@stryke/fs/get-workspace-root");
72
- let _stryke_hash = require("@stryke/hash");
73
- let _stryke_hash_node = require("@stryke/hash/node");
74
- let _stryke_http_fetch = require("@stryke/http/fetch");
75
- let _stryke_string_format_kebab_case = require("@stryke/string-format/kebab-case");
76
- let _stryke_type_checks_is_null = require("@stryke/type-checks/is-null");
77
- let _stryke_unique_id_uuid = require("@stryke/unique-id/uuid");
78
- let bundle_require = require("bundle-require");
79
- let compatx = require("compatx");
80
- let flat_cache = require("flat-cache");
81
- let oxc_parser = require("oxc-parser");
82
- let undici = require("undici");
83
- require("@stryke/fs/remove-file");
84
- let _storm_software_config_tools_types = require("@storm-software/config-tools/types");
85
- let _stryke_capnp = require("@stryke/capnp");
86
- _stryke_capnp = __toESM(_stryke_capnp, 1);
87
- let _stryke_fs_buffer = require("@stryke/fs/buffer");
88
- let _stryke_path_correct_path = require("@stryke/path/correct-path");
89
- let _stryke_path_glob_to_regex = require("@stryke/path/glob-to-regex");
90
- let _stryke_path_is_type = require("@stryke/path/is-type");
91
- let _stryke_path_slash = require("@stryke/path/slash");
92
- let _stryke_string_format_pretty_bytes = require("@stryke/string-format/pretty-bytes");
93
- let _stryke_type_checks_is_regexp = require("@stryke/type-checks/is-regexp");
94
- let node_buffer = require("node:buffer");
95
- let node_url = require("node:url");
96
- let _stryke_fs_is_file = require("@stryke/fs/is-file");
97
- let _stryke_fs_read_file = require("@stryke/fs/read-file");
98
- let _stryke_fs_write_file = require("@stryke/fs/write-file");
99
- let node_fs = require("node:fs");
100
- let node_fs_promises = require("node:fs/promises");
101
- let node_path = require("node:path");
102
- let typescript = require("typescript");
103
- typescript = __toESM(typescript, 1);
104
- let _powerlines_core_lib_config = require("@powerlines/core/lib/config");
105
- let _stryke_env_get_env_paths = require("@stryke/env/get-env-paths");
106
- let _stryke_json_storm_json = require("@stryke/json/storm-json");
107
- let date_fns_formatDistanceToNowStrict = require("date-fns/formatDistanceToNowStrict");
108
- let jiti = require("jiti");
109
- let _stryke_helpers_get_field = require("@stryke/helpers/get-field");
110
- let ts_morph = require("ts-morph");
111
- let _stryke_string_format_package = require("@stryke/string-format/package");
112
- let _donedeal0_superdiff = require("@donedeal0/superdiff");
113
-
114
- //#region src/_internal/helpers/environment.ts
115
- function createEnvironment(name, config = {}) {
116
- return (0, defu.default)(config.environments?.[name] ?? {}, {
117
- name,
118
- title: config.title ?? (0, _stryke_string_format_title_case.titleCase)(config.name),
119
- ssr: false,
120
- resolve: { mainFields: config.platform === "browser" ? [
121
- "browser",
122
- "module",
123
- "jsnext:main",
124
- "jsnext"
125
- ] : [
126
- "module",
127
- "jsnext:main",
128
- "jsnext"
129
- ] },
130
- consumer: config.platform === "browser" ? "client" : "server",
131
- preview: config.platform === "browser" ? {
132
- port: 5173,
133
- open: true,
134
- strictPort: false,
135
- host: "localhost",
136
- allowedHosts: ["."],
137
- cors: true,
138
- headers: {}
139
- } : void 0
140
- });
141
- }
142
- function createDefaultEnvironment(config = {}) {
143
- return createEnvironment(_powerlines_core_constants.DEFAULT_ENVIRONMENT, config);
144
- }
145
-
146
- //#endregion
147
- //#region src/_internal/helpers/context.ts
148
- function getConfigProps(config = {}, root, cwd) {
149
- return {
150
- name: config.name,
151
- title: config.title,
152
- description: config.description,
153
- projectType: config.projectType,
154
- customLogger: config.customLogger,
155
- logLevel: config.logLevel,
156
- tsconfig: config.tsconfig,
157
- tsconfigRaw: config.tsconfigRaw,
158
- skipCache: config.skipCache,
159
- autoInstall: config.autoInstall,
160
- input: (0, _stryke_type_checks_is_set_string.isSetString)(config.input) ? [config.input] : config.input,
161
- plugins: config.plugins,
162
- mode: config.mode,
163
- resolve: config.resolve,
164
- framework: config.framework,
165
- ...config,
166
- output: {
167
- ...config.output ?? {},
168
- path: config.output?.path ? (0, _stryke_path_append.appendPath)(config.output.path, (0, _stryke_path_append.appendPath)(root, cwd)) : void 0
169
- }
170
- };
171
- }
172
-
173
- //#endregion
174
- //#region src/_internal/helpers/meta.ts
175
- /**
176
- * Generates a prefixed project root hash object.
177
- *
178
- * @remarks
179
- * This function returns a string where the project root hash is prefixed with the project name plus a hyphen. If the total length of this string combination exceeds 45 characters, it will truncate the hash.
180
- *
181
- * @param name - The name of the project.
182
- * @param rootHash - The hash of the project root.
183
- * @returns An object containing the name and project root hash.
184
- */
185
- function getPrefixedRootHash(name, rootHash) {
186
- const combined = `${(0, _stryke_string_format_kebab_case.kebabCase)(name)}_${rootHash}`;
187
- return combined.length > _powerlines_core_constants.ROOT_HASH_LENGTH ? combined.slice(0, _powerlines_core_constants.ROOT_HASH_LENGTH) : combined;
188
- }
189
- /**
190
- * Writes the meta file for the context.
191
- *
192
- * @param context - The context to write the meta file for.
193
- * @returns A promise that resolves when the meta file has been written.
194
- */
195
- async function writeMetaFile(context) {
196
- const metaFilePath = (0, _stryke_path_join_paths.joinPaths)(context.dataPath, "meta.json");
197
- context.debug(`Writing runtime metadata to ${metaFilePath}`);
198
- await context.fs.write(metaFilePath, JSON.stringify(context.meta, null, 2));
199
- }
200
-
201
- //#endregion
202
- //#region schemas/fs.ts
203
- var FileMetadata_KeyValuePair = class extends _stryke_capnp.Struct {
204
- static _capnp = {
205
- displayName: "KeyValuePair",
206
- id: "eabb26cf58b2a14c",
207
- size: new _stryke_capnp.ObjectSize(0, 2)
208
- };
209
- get key() {
210
- return _stryke_capnp.utils.getText(0, this);
211
- }
212
- set key(value) {
213
- _stryke_capnp.utils.setText(0, value, this);
214
- }
215
- get value() {
216
- return _stryke_capnp.utils.getText(1, this);
217
- }
218
- set value(value) {
219
- _stryke_capnp.utils.setText(1, value, this);
220
- }
221
- toString() {
222
- return "FileMetadata_KeyValuePair_" + super.toString();
223
- }
224
- };
225
- /**
226
- * The identifier for the file data.
227
- *
228
- */
229
- var FileMetadata = class FileMetadata extends _stryke_capnp.Struct {
230
- static KeyValuePair = FileMetadata_KeyValuePair;
231
- static _capnp = {
232
- displayName: "FileMetadata",
233
- id: "8e2cab5d7e28c7b3",
234
- size: new _stryke_capnp.ObjectSize(8, 3),
235
- defaultType: "normal"
236
- };
237
- static _Properties;
238
- /**
239
- * The type of the file.
240
- *
241
- */
242
- get id() {
243
- return _stryke_capnp.utils.getText(0, this);
244
- }
245
- set id(value) {
246
- _stryke_capnp.utils.setText(0, value, this);
247
- }
248
- /**
249
- * The timestamp representing the file's creation date.
250
- *
251
- */
252
- get type() {
253
- return _stryke_capnp.utils.getText(1, this, FileMetadata._capnp.defaultType);
254
- }
255
- set type(value) {
256
- _stryke_capnp.utils.setText(1, value, this);
257
- }
258
- /**
259
- * Additional metadata associated with the file.
260
- *
261
- */
262
- get timestamp() {
263
- return _stryke_capnp.utils.getUint32(0, this);
264
- }
265
- set timestamp(value) {
266
- _stryke_capnp.utils.setUint32(0, value, this);
267
- }
268
- _adoptProperties(value) {
269
- _stryke_capnp.utils.adopt(value, _stryke_capnp.utils.getPointer(2, this));
270
- }
271
- _disownProperties() {
272
- return _stryke_capnp.utils.disown(this.properties);
273
- }
274
- get properties() {
275
- return _stryke_capnp.utils.getList(2, FileMetadata._Properties, this);
276
- }
277
- _hasProperties() {
278
- return !_stryke_capnp.utils.isNull(_stryke_capnp.utils.getPointer(2, this));
279
- }
280
- _initProperties(length) {
281
- return _stryke_capnp.utils.initList(2, FileMetadata._Properties, length, this);
282
- }
283
- set properties(value) {
284
- _stryke_capnp.utils.copyFrom(value, _stryke_capnp.utils.getPointer(2, this));
285
- }
286
- toString() {
287
- return "FileMetadata_" + super.toString();
288
- }
289
- };
290
- /**
291
- * An identifier for the file.
292
- *
293
- */
294
- var FileId = class extends _stryke_capnp.Struct {
295
- static _capnp = {
296
- displayName: "FileId",
297
- id: "990d6a471072f997",
298
- size: new _stryke_capnp.ObjectSize(0, 2)
299
- };
300
- /**
301
- * A virtual (or actual) path to the file in the file system.
302
- *
303
- */
304
- get id() {
305
- return _stryke_capnp.utils.getText(0, this);
306
- }
307
- set id(value) {
308
- _stryke_capnp.utils.setText(0, value, this);
309
- }
310
- get path() {
311
- return _stryke_capnp.utils.getText(1, this);
312
- }
313
- set path(value) {
314
- _stryke_capnp.utils.setText(1, value, this);
315
- }
316
- toString() {
317
- return "FileId_" + super.toString();
318
- }
319
- };
320
- /**
321
- * An identifier for the file.
322
- *
323
- */
324
- var FileStorage = class extends _stryke_capnp.Struct {
325
- static _capnp = {
326
- displayName: "FileStorage",
327
- id: "9dca66ac858c9ebe",
328
- size: new _stryke_capnp.ObjectSize(0, 2)
329
- };
330
- /**
331
- * A virtual (or actual) path to the file in the file system.
332
- *
333
- */
334
- get path() {
335
- return _stryke_capnp.utils.getText(0, this);
336
- }
337
- set path(value) {
338
- _stryke_capnp.utils.setText(0, value, this);
339
- }
340
- get code() {
341
- return _stryke_capnp.utils.getText(1, this);
342
- }
343
- set code(value) {
344
- _stryke_capnp.utils.setText(1, value, this);
345
- }
346
- toString() {
347
- return "FileStorage_" + super.toString();
348
- }
349
- };
350
- var FileSystem = class FileSystem extends _stryke_capnp.Struct {
351
- static _capnp = {
352
- displayName: "FileSystem",
353
- id: "ae0c23d43e56abcf",
354
- size: new _stryke_capnp.ObjectSize(0, 3)
355
- };
356
- static _Ids;
357
- static _Storage;
358
- static _Metadata;
359
- _adoptIds(value) {
360
- _stryke_capnp.utils.adopt(value, _stryke_capnp.utils.getPointer(0, this));
361
- }
362
- _disownIds() {
363
- return _stryke_capnp.utils.disown(this.ids);
364
- }
365
- get ids() {
366
- return _stryke_capnp.utils.getList(0, FileSystem._Ids, this);
367
- }
368
- _hasIds() {
369
- return !_stryke_capnp.utils.isNull(_stryke_capnp.utils.getPointer(0, this));
370
- }
371
- _initIds(length) {
372
- return _stryke_capnp.utils.initList(0, FileSystem._Ids, length, this);
373
- }
374
- set ids(value) {
375
- _stryke_capnp.utils.copyFrom(value, _stryke_capnp.utils.getPointer(0, this));
376
- }
377
- _adoptStorage(value) {
378
- _stryke_capnp.utils.adopt(value, _stryke_capnp.utils.getPointer(1, this));
379
- }
380
- _disownStorage() {
381
- return _stryke_capnp.utils.disown(this.storage);
382
- }
383
- get storage() {
384
- return _stryke_capnp.utils.getList(1, FileSystem._Storage, this);
385
- }
386
- _hasStorage() {
387
- return !_stryke_capnp.utils.isNull(_stryke_capnp.utils.getPointer(1, this));
388
- }
389
- _initStorage(length) {
390
- return _stryke_capnp.utils.initList(1, FileSystem._Storage, length, this);
391
- }
392
- set storage(value) {
393
- _stryke_capnp.utils.copyFrom(value, _stryke_capnp.utils.getPointer(1, this));
394
- }
395
- _adoptMetadata(value) {
396
- _stryke_capnp.utils.adopt(value, _stryke_capnp.utils.getPointer(2, this));
397
- }
398
- _disownMetadata() {
399
- return _stryke_capnp.utils.disown(this.metadata);
400
- }
401
- get metadata() {
402
- return _stryke_capnp.utils.getList(2, FileSystem._Metadata, this);
403
- }
404
- _hasMetadata() {
405
- return !_stryke_capnp.utils.isNull(_stryke_capnp.utils.getPointer(2, this));
406
- }
407
- _initMetadata(length) {
408
- return _stryke_capnp.utils.initList(2, FileSystem._Metadata, length, this);
409
- }
410
- set metadata(value) {
411
- _stryke_capnp.utils.copyFrom(value, _stryke_capnp.utils.getPointer(2, this));
412
- }
413
- toString() {
414
- return "FileSystem_" + super.toString();
415
- }
416
- };
417
- FileMetadata._Properties = _stryke_capnp.CompositeList(FileMetadata_KeyValuePair);
418
- FileSystem._Ids = _stryke_capnp.CompositeList(FileId);
419
- FileSystem._Storage = _stryke_capnp.CompositeList(FileStorage);
420
- FileSystem._Metadata = _stryke_capnp.CompositeList(FileMetadata);
421
-
422
- //#endregion
423
- //#region src/storage/base.ts
424
- /**
425
- * Abstract base class for storage adapters, providing a template for storage operations.
426
- */
427
- var BaseStorageAdapter = class {
428
- /**
429
- * Indicates whether the storage adapter has been disposed.
430
- */
431
- #isDisposed = false;
432
- /**
433
- * Configuration options for the storage adapter.
434
- */
435
- options;
436
- /**
437
- * The storage preset for the adapter.
438
- *
439
- * @remarks
440
- * This can be used as an alternate way to identify the type of storage being used.
441
- */
442
- preset = null;
443
- /**
444
- * Constructor for the BaseStorageAdapter.
445
- *
446
- * @param context - The Powerlines context.
447
- * @param options - Configuration options for the storage adapter.
448
- */
449
- constructor(context, options = { base: "/" }) {
450
- this.context = context;
451
- this.options = options;
452
- this.options.base = (0, node_path.resolve)(options.base);
453
- this.options.isReadOnly = !!options.isReadOnly;
454
- }
455
- /**
456
- * Asynchronously checks if a key exists in the storage.
457
- *
458
- * @param key - The key to check for existence.
459
- * @returns A promise that resolves to `true` if the key exists, otherwise `false`.
460
- */
461
- async exists(key) {
462
- return this.existsSync(key);
463
- }
464
- /**
465
- * Asynchronously retrieves the value associated with a given key.
466
- *
467
- * @param key - The key whose value is to be retrieved.
468
- * @returns A promise that resolves to the value associated with the key, or `null` if the key does not exist.
469
- */
470
- async get(key) {
471
- return this.getSync(key);
472
- }
473
- /**
474
- * Asynchronously sets the value for a given key.
475
- *
476
- * @param key - The key to set the value for.
477
- * @param value - The value to set.
478
- */
479
- async set(key, value) {
480
- if (!this.isReadOnly && (!this.existsSync(key) || this.overwrite)) this.setSync(key, value);
481
- }
482
- /**
483
- * Synchronously creates a directory at the specified path.
484
- *
485
- * @param _ - The path of the directory to create.
486
- */
487
- mkdirSync(_) {}
488
- /**
489
- * Creates a directory at the specified path.
490
- *
491
- * @param dirPath - The path of the directory to create.
492
- */
493
- async mkdir(dirPath) {
494
- return Promise.resolve(this.mkdirSync(dirPath));
495
- }
496
- /**
497
- * Asynchronously removes a key from the storage.
498
- *
499
- * @param key - The key to remove.
500
- */
501
- async remove(key) {
502
- if (!this.isReadOnly && this.overwrite) this.removeSync(key);
503
- }
504
- /**
505
- * Synchronously removes all entries from the storage that match the provided base path.
506
- *
507
- * @param base - The base path to clear keys from.
508
- */
509
- clearSync(base) {
510
- if (!this.isReadOnly && this.overwrite) {
511
- const keys = this.listSync(base || this.options.base);
512
- if (!keys.length) return;
513
- keys.map((key) => this.removeSync(base && !key.startsWith(base) ? (0, _stryke_path_join.joinPaths)(base, key) : key));
514
- }
515
- }
516
- /**
517
- * Asynchronously removes all entries from the storage that match the provided base path.
518
- *
519
- * @param base - The base path to clear keys from.
520
- * @returns A promise that resolves when the operation is complete.
521
- */
522
- async clear(base) {
523
- if (!this.isReadOnly && this.overwrite) {
524
- const keys = await this.list(base || this.options.base);
525
- if (!keys.length) return;
526
- await Promise.all(keys.map(async (key) => this.remove(base && !key.startsWith(base) ? (0, _stryke_path_join.joinPaths)(base, key) : key)));
527
- }
528
- }
529
- /**
530
- * Asynchronously lists all keys under a given base path.
531
- *
532
- * @param base - The base path to list keys from.
533
- * @returns A promise that resolves to an array of keys under the specified base path.
534
- */
535
- async list(base) {
536
- return this.listSync(base);
537
- }
538
- /**
539
- * Synchronously checks if the given key is a directory.
540
- *
541
- * @param _ - The key to check.
542
- * @returns `true` if the key is a directory, otherwise `false`.
543
- */
544
- isDirectorySync(_) {
545
- return false;
546
- }
547
- /**
548
- * Checks if the given key is a directory.
549
- *
550
- * @param key - The key to check.
551
- * @returns A promise that resolves to `true` if the key is a directory, otherwise `false`.
552
- */
553
- async isDirectory(key) {
554
- return Promise.resolve(this.isDirectorySync(key));
555
- }
556
- /**
557
- * Synchronously checks if the given key is a file.
558
- *
559
- * @param key - The key to check.
560
- * @returns `true` if the key is a file, otherwise `false`.
561
- */
562
- isFileSync(key) {
563
- return this.existsSync(key) && !this.isDirectorySync(key);
564
- }
565
- /**
566
- * Checks if the given key is a file.
567
- *
568
- * @param key - The key to check.
569
- * @returns A promise that resolves to `true` if the key is a file, otherwise `false`.
570
- */
571
- async isFile(key) {
572
- return Promise.resolve(this.isFileSync(key));
573
- }
574
- /**
575
- * Disposes of the storage adapter, releasing any held resources.
576
- *
577
- * @returns A promise that resolves when the disposal is complete.
578
- */
579
- dispose() {
580
- return Promise.resolve();
581
- }
582
- /**
583
- * Async dispose method to clean up resources.
584
- *
585
- * @returns A promise that resolves when disposal is complete.
586
- */
587
- async [Symbol.asyncDispose]() {
588
- return this._dispose();
589
- }
590
- /**
591
- * Determines if the storage adapter is read-only based on the provided options.
592
- *
593
- * @returns `true` if the storage adapter is read-only, otherwise `false`.
594
- */
595
- get isReadOnly() {
596
- return !!this.options.isReadOnly;
597
- }
598
- /**
599
- * Determines if the storage adapter should overwrite existing keys based on the provided options and context configuration.
600
- *
601
- * @returns `true` if the storage adapter should overwrite existing keys, otherwise `false`.
602
- */
603
- get overwrite() {
604
- return !this.isReadOnly && this.context.config.output.overwrite !== false;
605
- }
606
- /**
607
- * Resolves a given key to its full path within the storage adapter.
608
- *
609
- * @param key - The key to resolve.
610
- * @returns The resolved full path for the key.
611
- */
612
- resolve(key) {
613
- if (!key) return this.options.base;
614
- if (/\.\.:|\.\.$/.test(key)) throw new Error(`[${this.name}]: Invalid key: ${JSON.stringify(key)} provided to storage adapter.`);
615
- return (0, _stryke_path_append.appendPath)((0, _stryke_path_correct_path.correctPath)(key).replace(/:/g, "/"), this.options.base);
616
- }
617
- /**
618
- * Disposes of the storage adapter, releasing any held resources.
619
- *
620
- * @returns A promise that resolves when the disposal is complete.
621
- */
622
- async _dispose() {
623
- if (!this.#isDisposed) {
624
- await Promise.resolve(this.dispose());
625
- this.#isDisposed = true;
626
- }
627
- }
628
- };
629
-
630
- //#endregion
631
- //#region src/storage/helpers.ts
632
- /**
633
- * Checks if an error is a file system error.
634
- *
635
- * @param err - The error to check.
636
- * @returns `true` if the error is a file system error, otherwise `false`.
637
- */
638
- function isFileError(err) {
639
- return (0, _stryke_type_checks_is_error.isError)(err) && "code" in err && err.code;
640
- }
641
- /**
642
- * Ignores file not found errors.
643
- *
644
- * @param err - The error to check.
645
- * @returns `null` if the error is a file not found error, otherwise returns the error.
646
- */
647
- function ignoreNotfound(err) {
648
- return isFileError(err) && (err.code === "ENOENT" || err.code === "EISDIR" ? null : err);
649
- }
650
-
651
- //#endregion
652
- //#region src/storage/file-system.ts
653
- /**
654
- * File system storage adapter implementation.
655
- */
656
- var FileSystemStorageAdapter = class extends BaseStorageAdapter {
657
- /**
658
- * A name identifying the storage adapter type.
659
- */
660
- name = "file-system";
661
- /**
662
- * The storage preset for the adapter.
663
- *
664
- * @remarks
665
- * This can be used as an alternate way to identify the type of storage being used.
666
- */
667
- preset = "fs";
668
- /**
669
- * Constructor for the FileSystemStorageAdapter.
670
- *
671
- * @param context - The Powerlines context.
672
- * @param options - Configuration options for the storage adapter.
673
- */
674
- constructor(context, options) {
675
- super(context, options);
676
- }
677
- /**
678
- * Synchronously checks if a key exists in the storage.
679
- *
680
- * @param key - The key to check for existence.
681
- * @returns Returns `true` if the key exists, otherwise `false`.
682
- */
683
- existsSync(key) {
684
- return (0, _stryke_fs_exists.existsSync)(this.resolve(key));
685
- }
686
- /**
687
- * Asynchronously checks if a key exists in the storage.
688
- *
689
- * @param key - The key to check for existence.
690
- * @returns A promise that resolves to `true` if the key exists, otherwise `false`.
691
- */
692
- async exists(key) {
693
- return (0, _stryke_fs_exists.exists)(this.resolve(key));
694
- }
695
- /**
696
- * Synchronously retrieves the value associated with a given key.
697
- *
698
- * @param key - The key whose value is to be retrieved.
699
- * @returns The value associated with the key, or `null` if the key does not exist.
700
- */
701
- getSync(key) {
702
- return (0, _stryke_fs_read_file.readFileSync)(this.resolve(key));
703
- }
704
- /**
705
- * Asynchronously retrieves the value associated with a given key.
706
- *
707
- * @param key - The key whose value is to be retrieved.
708
- * @returns A promise that resolves to the value associated with the key, or `null` if the key does not exist.
709
- */
710
- async get(key) {
711
- return (0, _stryke_fs_read_file.readFile)(this.resolve(key));
712
- }
713
- /**
714
- * Synchronously sets the value for a given key.
715
- *
716
- * @param key - The key to set the value for.
717
- * @param value - The value to set.
718
- */
719
- setSync(key, value) {
720
- if (!this.isReadOnly) if (this.existsSync(this.resolve(key)) && !this.overwrite) {
721
- const existingValue = this.getSync(this.resolve(key));
722
- if ((0, _stryke_type_checks_is_set_string.isSetString)(existingValue) && existingValue.includes((0, _powerlines_core_lib_utilities_file_header.getFileHeaderWarningText)(true, this.context.config.framework))) return (0, _stryke_fs_write_file.writeFileSync)(this.resolve(key), existingValue.replace((0, _powerlines_core_lib_utilities_file_header.getFileHeaderWarningText)(true, this.context.config.framework), (0, _powerlines_core_lib_utilities_file_header.getFileHeaderWarningText)(false, this.context.config.framework)));
723
- } else return (0, _stryke_fs_write_file.writeFileSync)(this.resolve(key), value);
724
- }
725
- /**
726
- * Asynchronously sets the value for a given key.
727
- *
728
- * @param key - The key to set the value for.
729
- * @param value - The value to set.
730
- */
731
- async set(key, value) {
732
- if (!this.isReadOnly) if (this.existsSync(this.resolve(key)) && !this.overwrite) {
733
- const existingValue = await this.get(this.resolve(key));
734
- if ((0, _stryke_type_checks_is_set_string.isSetString)(existingValue) && existingValue.includes((0, _powerlines_core_lib_utilities_file_header.getFileHeaderWarningText)(true, this.context.config.framework))) return (0, _stryke_fs_write_file.writeFile)(this.resolve(key), existingValue.replace((0, _powerlines_core_lib_utilities_file_header.getFileHeaderWarningText)(true, this.context.config.framework), (0, _powerlines_core_lib_utilities_file_header.getFileHeaderWarningText)(false, this.context.config.framework)));
735
- } else return (0, _stryke_fs_write_file.writeFile)(this.resolve(key), value);
736
- }
737
- /**
738
- * Synchronously removes a key from the storage.
739
- *
740
- * @param key - The key to remove.
741
- */
742
- removeSync(key) {
743
- if (!this.isReadOnly && this.overwrite) try {
744
- return (0, node_fs.unlinkSync)(this.resolve(key));
745
- } catch (err) {
746
- return ignoreNotfound(err);
747
- }
748
- }
749
- /**
750
- * Asynchronously removes a key from the storage.
751
- *
752
- * @param key - The key to remove.
753
- */
754
- async remove(key) {
755
- if (!this.isReadOnly && this.overwrite) return (0, node_fs_promises.unlink)(this.resolve(key)).catch(ignoreNotfound);
756
- }
757
- /**
758
- * Synchronously creates a directory at the specified path.
759
- *
760
- * @param dirPath - The path of the directory to create.
761
- */
762
- mkdirSync(dirPath) {
763
- (0, _stryke_fs_helpers.createDirectorySync)(this.resolve(dirPath));
764
- }
765
- /**
766
- * Creates a directory at the specified path.
767
- *
768
- * @param dirPath - The path of the directory to create.
769
- */
770
- async mkdir(dirPath) {
771
- await (0, _stryke_fs_helpers.createDirectory)(this.resolve(dirPath));
772
- }
773
- /**
774
- * Lists all keys under a given base path synchronously.
775
- *
776
- * @param base - The base path to list keys from.
777
- * @returns An array of keys under the specified base path.
778
- */
779
- listSync(base) {
780
- try {
781
- return (0, _stryke_fs_list_files.listFilesSync)(this.resolve(base), { ignore: this.options.ignore });
782
- } catch (err) {
783
- return ignoreNotfound(err) ?? [];
784
- }
785
- }
786
- /**
787
- * Asynchronously lists all keys under a given base path.
788
- *
789
- * @param base - The base path to list keys from.
790
- * @returns A promise that resolves to an array of keys under the specified base path.
791
- */
792
- async list(base) {
793
- return (0, _stryke_fs_list_files.listFiles)(this.resolve(base), { ignore: this.options.ignore }).catch(ignoreNotfound).then((r) => r || []);
794
- }
795
- /**
796
- * Synchronously checks if the given key is a directory.
797
- *
798
- * @param key - The key to check.
799
- * @returns `true` if the key is a directory, otherwise `false`.
800
- */
801
- isDirectorySync(key) {
802
- return (0, _stryke_fs_is_file.isDirectory)(this.resolve(key));
803
- }
804
- /**
805
- * Synchronously checks if the given key is a file.
806
- *
807
- * @param key - The key to check.
808
- * @returns `true` if the key is a file, otherwise `false`.
809
- */
810
- isFileSync(key) {
811
- return (0, _stryke_fs_is_file.isFile)(this.resolve(key));
812
- }
813
- };
814
-
815
- //#endregion
816
- //#region src/storage/virtual.ts
817
- /**
818
- * Virtual/in-memory storage adapter implementation.
819
- */
820
- var VirtualStorageAdapter = class extends BaseStorageAdapter {
821
- /**
822
- * A name identifying the storage adapter type.
823
- */
824
- name = "virtual";
825
- /**
826
- * The storage preset for the adapter.
827
- *
828
- * @remarks
829
- * This can be used as an alternate way to identify the type of storage being used.
830
- */
831
- preset = "virtual";
832
- /**
833
- * In-memory data storage.
834
- */
835
- data = /* @__PURE__ */ new Map();
836
- /**
837
- * Constructor for the VirtualStorageAdapter.
838
- *
839
- * @param context - The Powerlines context.
840
- * @param options - Configuration options for the storage adapter.
841
- */
842
- constructor(context, options) {
843
- super(context, options);
844
- }
845
- /**
846
- * Synchronously checks if a key exists in the storage.
847
- *
848
- * @param key - The key to check for existence.
849
- * @returns Returns `true` if the key exists, otherwise `false`.
850
- */
851
- existsSync(key) {
852
- return this.data.has(this.resolve(key));
853
- }
854
- /**
855
- * Synchronously retrieves the value associated with a given key.
856
- *
857
- * @param key - The key whose value is to be retrieved.
858
- * @returns The value associated with the key, or `null` if the key does not exist.
859
- */
860
- getSync(key) {
861
- return this.data.get(this.resolve(key)) ?? null;
862
- }
863
- /**
864
- * Synchronously sets the value for a given key.
865
- *
866
- * @param key - The key to set the value for.
867
- * @param value - The value to set.
868
- */
869
- setSync(key, value) {
870
- if (!this.isReadOnly && (!this.existsSync(key) || this.overwrite)) this.data.set(this.resolve(key), value);
871
- }
872
- /**
873
- * Synchronously removes a key from the storage.
874
- *
875
- * @param key - The key to remove.
876
- */
877
- removeSync(key) {
878
- if (!this.isReadOnly && this.overwrite) this.data.delete(this.resolve(key));
879
- }
880
- /**
881
- * Lists all keys under a given base path synchronously.
882
- *
883
- * @param base - The base path to list keys from.
884
- * @returns An array of keys under the specified base path.
885
- */
886
- listSync(base) {
887
- return [...this.data.keys().filter((key) => !base ? true : (0, _stryke_path_is_parent_path.isParentPath)(key, this.resolve(base)))];
888
- }
889
- /**
890
- * Disposes of the storage adapter, releasing any held resources.
891
- *
892
- * @returns A promise that resolves when the disposal is complete.
893
- */
894
- async dispose() {
895
- return this.clear();
896
- }
897
- /**
898
- * Determines if the storage adapter should overwrite existing keys based on the provided options and context configuration.
899
- *
900
- * @returns `true` if the storage adapter should overwrite existing keys, otherwise `false`.
901
- */
902
- get overwrite() {
903
- return !this.isReadOnly;
904
- }
905
- };
906
-
907
- //#endregion
908
- //#region src/_internal/helpers/constants.ts
909
- const DEFAULT_EXTENSIONS = [
910
- "js",
911
- "ts",
912
- "cjs",
913
- "cts",
914
- "mjs",
915
- "mts",
916
- "tsx",
917
- "jsx",
918
- "json",
919
- "json5",
920
- "jsonc",
921
- "md",
922
- "mdx"
923
- ];
924
-
925
- //#endregion
926
- //#region src/_internal/vfs.ts
927
- function toFilePath(path) {
928
- return (0, _stryke_path_correct_path.correctPath)((0, _stryke_path_slash.slash)(path?.toString() || ".").replace(/^file:\/\//, ""));
929
- }
930
- /**
931
- * Checks if a given file id is valid based on the specified prefix.
932
- *
933
- * @param id - The file ID to check.
934
- * @param prefix - The prefix to use for built-in files. Default is "powerlines".
935
- * @returns `true` if the file ID is valid, otherwise `false`.
936
- */
937
- function isValidId(id, prefix = "powerlines") {
938
- return id.replace(/^\\0/, "").startsWith(`${prefix.replace(/:$/, "")}`);
939
- }
940
- /**
941
- * Formats a file id by removing the file extension and prepended runtime prefix.
942
- *
943
- * @param id - The file ID to format.
944
- * @param prefix - The prefix to use for built-in files. Default is "powerlines".
945
- * @returns The formatted file ID.
946
- */
947
- function normalizeId(id, prefix = "powerlines") {
948
- return (0, _stryke_path_replace.replaceExtension)(toFilePath(id)).replace(/^\\0/, "").replace(/^powerlines:/, "").replace(new RegExp(`^${prefix.replace(/:$/, "")}:`), "");
949
- }
950
- /**
951
- * Normalizes a given path by resolving it against the project root, workspace root, and built-ins path.
952
- *
953
- * @param path - The path to normalize.
954
- * @param builtinsPath - The path to built-in files.
955
- * @param prefix - The prefix to use for built-in files. Default is "powerlines".
956
- * @returns The normalized path.
957
- */
958
- function normalizePath(path, builtinsPath, prefix = "powerlines") {
959
- if (!(0, _stryke_type_checks_is_set_string.isSetString)(path)) {
960
- if (!(0, _stryke_type_checks_is_string.isString)(path)) throw new Error("Path type must be a string or a file descriptor");
961
- throw new Error("Path cannot be empty");
962
- }
963
- return (0, _stryke_path_is_type.isAbsolutePath)(path) ? path : isValidId(toFilePath(path), prefix) ? normalizeId(toFilePath(path), prefix).replace(new RegExp(`^${prefix.replace(/:$/, "")}:`), builtinsPath) : toFilePath(path);
964
- }
965
- /**
966
- * Normalizes glob patterns by resolving them against the workspace root.
967
- *
968
- * @param workspaceRoot - The root directory of the workspace.
969
- * @param patterns - The glob patterns to normalize.
970
- * @returns An array of normalized glob patterns.
971
- */
972
- function normalizeGlobPatterns(workspaceRoot, patterns) {
973
- return (0, _stryke_helpers_get_unique.getUnique)((0, _stryke_convert_to_array.toArray)(patterns).map((pattern) => {
974
- if ((0, _stryke_type_checks_is_set_object.isSetObject)(pattern) && ((0, _stryke_type_checks_is_set_string.isSetString)(pattern.input) || (0, _stryke_type_checks_is_set_string.isSetString)(pattern.glob))) return (0, _stryke_path_join.joinPaths)(pattern.input || workspaceRoot, pattern.glob || "**/*");
975
- else if (!(0, _stryke_type_checks_is_set_string.isSetString)(pattern)) return;
976
- return pattern;
977
- }).filter(_stryke_type_checks_is_set_string.isSetString));
978
- }
979
- /**
980
- * Represents a virtual file system (VFS) that stores files and their associated metadata in virtual memory.
981
- *
982
- * @remarks
983
- * This class provides methods to manage virtual files, check their existence, retrieve their content, and manipulate the virtual file system. It allows for efficient file management and retrieval without relying on the actual file system.
984
- */
985
- var VirtualFileSystem = class VirtualFileSystem {
986
- /**
987
- * A map of virtual file IDs to their associated metadata.
988
- */
989
- #metadata;
990
- /**
991
- * A map of underlying file paths to their virtual file IDs.
992
- */
993
- #ids;
994
- /**
995
- * A map of virtual file IDs to their underlying file paths.
996
- */
997
- #paths;
998
- /**
999
- * The unified volume that combines the virtual file system with the real file system.
1000
- *
1001
- * @remarks
1002
- * This volume allows for seamless access to both virtual and real files.
1003
- */
1004
- #storage;
1005
- /**
1006
- * A cache for module resolution results.
1007
- */
1008
- #resolverCache;
1009
- /**
1010
- * Indicator specifying if the virtual file system (VFS) is disposed
1011
- */
1012
- #isDisposed = false;
1013
- /**
1014
- * The context of the virtual file system.
1015
- */
1016
- #context;
1017
- /**
1018
- * The file system's logging function.
1019
- */
1020
- #log;
1021
- /**
1022
- * Normalizes a given module id by resolving it against the built-ins path.
1023
- *
1024
- * @param id - The module id to normalize.
1025
- * @returns The normalized module id.
1026
- */
1027
- #normalizeId(id) {
1028
- let normalized = id;
1029
- if ((0, _stryke_path_is_parent_path.isParentPath)(normalized, this.#context.builtinsPath)) normalized = (0, _stryke_path_replace.replacePath)(normalized, this.#context.builtinsPath);
1030
- return normalizeId(normalized, this.#context.config.framework);
1031
- }
1032
- /**
1033
- * Normalizes a given path by resolving it against the project root, workspace root, and built-ins path.
1034
- *
1035
- * @param path - The path to normalize.
1036
- * @returns The normalized path.
1037
- */
1038
- #normalizePath(path) {
1039
- if (!(0, _stryke_type_checks_is_set_string.isSetString)(path)) {
1040
- if (!(0, _stryke_type_checks_is_string.isString)(path)) throw new Error("Path type must be a string or a file descriptor");
1041
- throw new Error("Path cannot be empty");
1042
- }
1043
- return normalizePath(path.includes("{") || path.includes("}") ? (0, _powerlines_core_plugin_utils.replacePathTokens)(this.#context, path) : path, this.#context.builtinsPath, this.#context.config.framework);
1044
- }
1045
- /**
1046
- * Gets the storage adapter and relative key for a given key.
1047
- *
1048
- * @remarks
1049
- * The `key` can be either a path or a storage adapter name.
1050
- *
1051
- * @param key - The key to get the storage adapter for.
1052
- * @returns The storage adapter and relative key for the given key.
1053
- */
1054
- #getStorage(key, preset) {
1055
- const path = this.resolveSync(this.#normalizePath(key)) || key;
1056
- for (const base of Object.keys(this.#storage).filter(Boolean).sort().reverse()) if ((path === base || (0, _stryke_path_is_parent_path.isParentPath)(path, base)) && (!preset || this.#storage[base]?.preset?.toLowerCase() === preset.toLowerCase())) return {
1057
- base,
1058
- relativeKey: (0, _stryke_path_replace.replacePath)(path, base),
1059
- adapter: this.#storage[base]
1060
- };
1061
- if (!preset || this.#storage[""]?.preset?.toLowerCase() === preset.toLowerCase()) return {
1062
- base: "",
1063
- relativeKey: path,
1064
- adapter: this.#storage[""]
1065
- };
1066
- this.#storage[path] = preset === "virtual" ? new VirtualStorageAdapter(this.#context, { base: path }) : new FileSystemStorageAdapter(this.#context, { base: path });
1067
- return {
1068
- base: path,
1069
- relativeKey: "",
1070
- adapter: this.#storage[path]
1071
- };
1072
- }
1073
- /**
1074
- * Gets all storage adapters that match a given base key.
1075
- *
1076
- * @param base - The base key to match storage adapters against.
1077
- * @param includeParent - Whether to include parent storage adapters.
1078
- * @returns An array of storage adapters that match the given base key.
1079
- */
1080
- #getStorages(base = "", includeParent = false) {
1081
- const baseKey = this.resolveSync(base) || base;
1082
- return Object.keys(this.#storage).sort().reverse().filter((key) => (0, _stryke_path_is_parent_path.isParentPath)(key, baseKey) || includeParent && (0, _stryke_path_is_parent_path.isParentPath)(baseKey, key) || baseKey.includes("*") && ((0, _stryke_path_is_parent_path.isParentPath)((0, _stryke_path_correct_path.stripStars)(baseKey), key) || (0, _stryke_path_glob_to_regex.globToRegex)((0, _stryke_path_replace.replaceExtension)(baseKey)).test(key))).map((key) => ({
1083
- relativeBase: baseKey.length > key.length ? baseKey.slice(key.length) : void 0,
1084
- base: key,
1085
- adapter: this.#storage[key]
1086
- }));
1087
- }
1088
- /**
1089
- * A helper function to resolve modules in the virtual file system (VFS).
1090
- *
1091
- * @remarks
1092
- * This function can be used to resolve modules relative to the project root directory.
1093
- *
1094
- * @example
1095
- * ```ts
1096
- * const resolved = await context.resolvePath("some-module", "/path/to/importer");
1097
- * ```
1098
- *
1099
- * @param id - The module to resolve.
1100
- * @param importer - An optional path to the importer module.
1101
- * @param options - Additional resolution options.
1102
- * @returns A promise that resolves to the resolved module path.
1103
- */
1104
- #innerResolve = async (id, importer, options = {}) => {
1105
- let path = id;
1106
- if (path.includes("{") || path.includes("}")) path = (0, _powerlines_core_plugin_utils.replacePathTokens)(this.#context, path);
1107
- if (options.skipAlias !== true) path = this.resolveAlias(path);
1108
- if ((0, _stryke_path_is_type.isAbsolutePath)(path) && (!options.isFile || !await this.isDirectory(path))) return path;
1109
- const resolverCacheKey = (0, _stryke_hash.murmurhash)({
1110
- path: this.#normalizeId(path),
1111
- importer,
1112
- options
1113
- });
1114
- let result;
1115
- if (!this.#context.config.skipCache) {
1116
- result = this.resolverCache.get(resolverCacheKey);
1117
- if (result) return result;
1118
- }
1119
- result = this.paths[this.#normalizeId(path)];
1120
- if (!(0, _stryke_type_checks_is_set_string.isSetString)(result)) {
1121
- const paths = options.paths ?? [];
1122
- if (importer && !paths.includes(importer)) paths.push(importer);
1123
- if (!importer) {
1124
- paths.push(this.#context.config.cwd);
1125
- paths.push((0, _stryke_path_append.appendPath)(this.#context.config.root, this.#context.config.cwd));
1126
- paths.push((0, _stryke_path_append.appendPath)((0, _stryke_path_join.joinPaths)(this.#context.config.root, "src"), this.#context.config.cwd));
1127
- }
1128
- paths.push(...Object.keys(this.#context.tsconfig?.options?.paths ?? {}).filter((tsconfigPath) => path.startsWith(tsconfigPath.replace(/\*$/, ""))).map((tsconfigPath) => this.#context.tsconfig?.options?.paths?.[tsconfigPath]).flat().filter(Boolean).map((tsconfigPath) => (0, _stryke_path_append.appendPath)(tsconfigPath, this.#context.config.cwd)));
1129
- for (const combination of (0, _stryke_fs_resolve.getResolutionCombinations)(path, { paths: (0, _stryke_helpers_get_unique.getUnique)(paths) })) {
1130
- const { relativeKey, adapter } = this.#getStorage(combination);
1131
- if (await adapter.exists(relativeKey)) {
1132
- result = combination;
1133
- break;
1134
- }
1135
- }
1136
- if (!(0, _stryke_type_checks_is_set_string.isSetString)(result)) try {
1137
- result = await (0, _stryke_fs_resolve.resolve)(path, {
1138
- ...options,
1139
- paths
1140
- });
1141
- } catch {}
1142
- }
1143
- if ((0, _stryke_type_checks_is_set_string.isSetString)(result)) {
1144
- if (!this.#context.config.skipCache) this.resolverCache.set(this.#normalizeId(path), result);
1145
- return result;
1146
- }
1147
- };
1148
- /**
1149
- * A synchronous helper function to resolve modules using the Jiti resolver
1150
- *
1151
- * @remarks
1152
- * This function can be used to resolve modules relative to the project root directory.
1153
- *
1154
- * @example
1155
- * ```ts
1156
- * const resolvedPath = context.resolveSync("some-module", "/path/to/importer");
1157
- * ```
1158
- *
1159
- * @param id - The module to resolve.
1160
- * @param importer - An optional path to the importer module.
1161
- * @param options - Additional resolution options.
1162
- * @returns The resolved module path.
1163
- */
1164
- #innerResolveSync = (id, importer, options = {}) => {
1165
- let path = id;
1166
- if (path.includes("{") || path.includes("}")) path = (0, _powerlines_core_plugin_utils.replacePathTokens)(this.#context, path);
1167
- if (options.skipAlias !== true) path = this.resolveAlias(path);
1168
- if ((0, _stryke_path_is_type.isAbsolutePath)(path) && (!options.isFile || !this.isDirectorySync(path))) return path;
1169
- let result;
1170
- if (!this.#context.config.skipCache) {
1171
- result = this.resolverCache.get(this.#normalizeId(path));
1172
- if ((0, _stryke_type_checks_is_set_string.isSetString)(result)) return result;
1173
- }
1174
- result = this.paths[this.#normalizeId(path)];
1175
- if (!(0, _stryke_type_checks_is_set_string.isSetString)(result)) {
1176
- const paths = options.paths ?? [];
1177
- if (importer && !paths.includes(importer)) paths.push(importer);
1178
- if (!importer) {
1179
- paths.push(this.#context.config.cwd);
1180
- paths.push((0, _stryke_path_append.appendPath)(this.#context.config.root, this.#context.config.cwd));
1181
- paths.push((0, _stryke_path_append.appendPath)((0, _stryke_path_join.joinPaths)(this.#context.config.root, "src"), this.#context.config.cwd));
1182
- }
1183
- paths.push(...Object.keys(this.#context.tsconfig?.options?.paths ?? {}).filter((tsconfigPath) => path.startsWith(tsconfigPath.replace(/\*$/, ""))).map((tsconfigPath) => this.#context.tsconfig?.options?.paths?.[tsconfigPath]).flat().filter(Boolean).map((tsconfigPath) => (0, _stryke_path_append.appendPath)(tsconfigPath, this.#context.config.cwd)));
1184
- for (const combination of (0, _stryke_fs_resolve.getResolutionCombinations)(path, { paths })) {
1185
- const { relativeKey, adapter } = this.#getStorage(combination);
1186
- if (adapter.existsSync(relativeKey)) {
1187
- result = combination;
1188
- break;
1189
- }
1190
- }
1191
- if (!(0, _stryke_type_checks_is_set_string.isSetString)(result)) try {
1192
- result = (0, _stryke_fs_resolve.resolveSync)(path, {
1193
- ...options,
1194
- paths
1195
- });
1196
- } catch {}
1197
- }
1198
- if ((0, _stryke_type_checks_is_set_string.isSetString)(result)) {
1199
- if (!this.#context.config.skipCache) this.resolverCache.set(this.#normalizeId(path), result);
1200
- return result;
1201
- }
1202
- };
1203
- /**
1204
- * Creates a virtual file system (VFS) that is backed up to a Cap'n Proto message buffer.
1205
- *
1206
- * @param context - The context of the virtual file system, typically containing options and logging functions.
1207
- * @returns A promise that resolves to a new virtual file system instance.
1208
- */
1209
- static async create(context) {
1210
- context.debug("Starting virtual file system (VFS) initialization processes...");
1211
- let result;
1212
- if (!context.config.skipCache && (0, _stryke_fs_exists.existsSync)((0, _stryke_path_join.joinPaths)(context.dataPath, "fs.bin"))) {
1213
- const buffer = await (0, _stryke_fs_buffer.readFileBuffer)((0, _stryke_path_join.joinPaths)(context.dataPath, "fs.bin"));
1214
- const fs = new _stryke_capnp.Message(buffer, false).getRoot(FileSystem);
1215
- result = new VirtualFileSystem(context, fs);
1216
- if (fs._hasStorage() && fs.storage.length > 0) await Promise.all(fs.storage.values().map(async (file) => {
1217
- if (file.path && file.code) {
1218
- let id;
1219
- if (fs._hasIds()) id = fs.ids.find((fileId) => fileId.path === file.path);
1220
- let metadata;
1221
- if (fs._hasMetadata()) metadata = fs.metadata.find((meta) => meta.id === result.#normalizeId(id?.id ?? file.path));
1222
- await result.write(file.path, file.code, { meta: {
1223
- id: result.#normalizeId(id?.id ?? metadata?.id ?? file.path),
1224
- type: metadata?.type || "normal",
1225
- properties: metadata?._hasProperties() ? metadata?.properties.values().reduce((ret, kvp) => {
1226
- ret[kvp.key] = kvp.value;
1227
- return ret;
1228
- }, {}) : void 0,
1229
- timestamp: metadata?.timestamp
1230
- } });
1231
- }
1232
- }));
1233
- } else result = new VirtualFileSystem(context, new _stryke_capnp.Message().initRoot(FileSystem));
1234
- result.#log(_storm_software_config_tools_types.LogLevelLabel.DEBUG, "Successfully completed virtual file system (VFS) initialization.");
1235
- return result;
1236
- }
1237
- /**
1238
- * Synchronously creates a virtual file system (VFS) that is backed up to a Cap'n Proto message buffer.
1239
- *
1240
- * @param context - The context of the virtual file system, typically containing options and logging functions.
1241
- * @returns A new virtual file system instance.
1242
- */
1243
- static createSync(context) {
1244
- context.debug("Starting virtual file system (VFS) initialization processes...");
1245
- let result;
1246
- if (!context.config.skipCache && (0, _stryke_fs_exists.existsSync)((0, _stryke_path_join.joinPaths)(context.dataPath, "fs.bin"))) {
1247
- const buffer = (0, _stryke_fs_buffer.readFileBufferSync)((0, _stryke_path_join.joinPaths)(context.dataPath, "fs.bin"));
1248
- const fs = new _stryke_capnp.Message(buffer, false).getRoot(FileSystem);
1249
- result = new VirtualFileSystem(context, fs);
1250
- if (fs._hasStorage() && fs.storage.length > 0) fs.storage.values().forEach((file) => {
1251
- if (file.path && file.code) {
1252
- let id;
1253
- if (fs._hasIds()) id = fs.ids.find((fileId) => fileId.path === file.path);
1254
- let metadata;
1255
- if (fs._hasMetadata()) metadata = fs.metadata.find((meta) => meta.id === result.#normalizeId(id?.id ?? file.path));
1256
- result.writeSync(file.path, file.code, { meta: {
1257
- id: result.#normalizeId(id?.id ?? metadata?.id ?? file.path),
1258
- type: metadata?.type,
1259
- properties: metadata?._hasProperties() ? metadata?.properties.values().reduce((ret, kvp) => {
1260
- ret[kvp.key] = kvp.value;
1261
- return ret;
1262
- }, {}) : void 0,
1263
- timestamp: metadata?.timestamp
1264
- } });
1265
- }
1266
- });
1267
- } else result = new VirtualFileSystem(context, new _stryke_capnp.Message().initRoot(FileSystem));
1268
- result.#log(_storm_software_config_tools_types.LogLevelLabel.DEBUG, "Successfully completed virtual file system (VFS) initialization.");
1269
- return result;
1270
- }
1271
- /**
1272
- * A map of file ids to their metadata.
1273
- */
1274
- get metadata() {
1275
- return new Proxy(this.#metadata, {
1276
- get: (target, prop) => {
1277
- return target[this.#normalizeId(prop)];
1278
- },
1279
- set: (target, prop, value) => {
1280
- target[this.#normalizeId(prop)] = value;
1281
- return true;
1282
- },
1283
- deleteProperty: (target, prop) => {
1284
- delete target[this.#normalizeId(prop)];
1285
- return true;
1286
- },
1287
- has: (target, prop) => {
1288
- return this.#normalizeId(prop) in target;
1289
- },
1290
- ownKeys: (target) => {
1291
- return (0, _stryke_helpers_get_unique.getUnique)(Reflect.ownKeys(target).map((key) => this.#normalizeId(key)));
1292
- }
1293
- });
1294
- }
1295
- /**
1296
- * A map of file paths to their module ids.
1297
- */
1298
- get ids() {
1299
- return new Proxy(this.#ids, {
1300
- get: (target, prop) => {
1301
- return target[this.#normalizePath(prop)];
1302
- },
1303
- set: (target, prop, value) => {
1304
- target[this.#normalizePath(prop)] = value;
1305
- return true;
1306
- },
1307
- deleteProperty: (target, prop) => {
1308
- delete target[this.#normalizePath(prop)];
1309
- return true;
1310
- },
1311
- has: (target, prop) => {
1312
- return this.#normalizePath(prop) in target;
1313
- },
1314
- ownKeys: (target) => {
1315
- return (0, _stryke_helpers_get_unique.getUnique)(Reflect.ownKeys(target).map((key) => this.#normalizePath(key)));
1316
- }
1317
- });
1318
- }
1319
- /**
1320
- * A map of module ids to their file paths.
1321
- */
1322
- get paths() {
1323
- return new Proxy(this.#paths, {
1324
- get: (target, prop) => {
1325
- return this.#normalizeId(prop) in target ? target[this.#normalizeId(prop)] : void 0;
1326
- },
1327
- set: (target, prop, value) => {
1328
- target[this.#normalizeId(prop)] = value;
1329
- return true;
1330
- },
1331
- deleteProperty: (target, prop) => {
1332
- delete target[this.#normalizeId(prop)];
1333
- return true;
1334
- },
1335
- has: (target, prop) => {
1336
- return this.#normalizeId(prop) in target;
1337
- },
1338
- ownKeys: (target) => {
1339
- return (0, _stryke_helpers_get_unique.getUnique)(Reflect.ownKeys(target).map((key) => this.#normalizeId(key)));
1340
- }
1341
- });
1342
- }
1343
- /**
1344
- * Gets the resolver cache.
1345
- */
1346
- get resolverCache() {
1347
- if (!this.#resolverCache) this.#resolverCache = (0, flat_cache.create)({
1348
- cacheId: "module-resolution",
1349
- cacheDir: this.#context.cachePath,
1350
- ttl: 2.5 * 60 * 1e3,
1351
- lruSize: 8e3,
1352
- persistInterval: 100
1353
- });
1354
- return this.#resolverCache;
1355
- }
1356
- /**
1357
- * Creates a new instance of the {@link VirtualFileSystem}.
1358
- *
1359
- * @param context - The context of the virtual file system, typically containing options and logging functions.
1360
- * @param fs - A buffer containing the serialized virtual file system data.
1361
- */
1362
- constructor(context, fs) {
1363
- this.#context = context;
1364
- this.#storage = { "": new FileSystemStorageAdapter(context) };
1365
- if ((0, _stryke_type_checks_is_set_object.isSetObject)(this.#context.config.output.storage)) this.#storage = {
1366
- ...this.#storage,
1367
- ...this.#context.config.output.storage
1368
- };
1369
- this.#storage.virtual ??= new VirtualStorageAdapter(context, { base: "/_virtual" });
1370
- this.#storage[this.#context.config.output.path] ??= new FileSystemStorageAdapter(context, { base: this.#context.config.output.path });
1371
- if (this.#context.config.output.copy && this.#context.config.output.copy.path) this.#storage[this.#context.config.output.copy.path] ??= new FileSystemStorageAdapter(context, { base: this.#context.config.output.copy.path });
1372
- if (this.#context.config.output.storage !== "fs") {
1373
- this.#storage[this.#context.artifactsPath] ??= new VirtualStorageAdapter(context, { base: this.#context.artifactsPath });
1374
- this.#storage[this.#context.builtinsPath] ??= new VirtualStorageAdapter(context, { base: this.#context.builtinsPath });
1375
- this.#storage[this.#context.entryPath] ??= new VirtualStorageAdapter(context, { base: this.#context.entryPath });
1376
- }
1377
- this.#metadata = {};
1378
- if (fs._hasMetadata()) this.#metadata = fs.metadata.values().reduce((ret, metadata) => {
1379
- ret[metadata.id] = {
1380
- id: metadata.id,
1381
- type: metadata.type,
1382
- timestamp: metadata.timestamp ?? Date.now(),
1383
- properties: metadata._hasProperties() ? metadata.properties.values().reduce((ret, item) => {
1384
- ret[item.key] = item.value;
1385
- return ret;
1386
- }, {}) : {}
1387
- };
1388
- return ret;
1389
- }, {});
1390
- this.#ids = {};
1391
- this.#paths = {};
1392
- if (fs._hasIds()) {
1393
- this.#ids = fs.ids.values().reduce((ret, identifier) => {
1394
- ret[identifier.path] ??= identifier.id;
1395
- return ret;
1396
- }, {});
1397
- this.#paths = fs.ids.values().reduce((ret, identifier) => {
1398
- ret[identifier.id] ??= identifier.path;
1399
- return ret;
1400
- }, {});
1401
- }
1402
- this.#log = (0, _powerlines_core_lib_logger.extendLog)(this.#context.log, "file-system");
1403
- }
1404
- /**
1405
- * Asynchronously checks if a file exists in the virtual file system (VFS).
1406
- *
1407
- * @param path - The path to the file.
1408
- * @returns A promise that resolves to `true` if the file exists, otherwise `false`.
1409
- */
1410
- async exists(path) {
1411
- const { relativeKey, adapter } = this.#getStorage(path);
1412
- return adapter.exists(relativeKey);
1413
- }
1414
- /**
1415
- * Synchronously checks if a file exists in the virtual file system (VFS).
1416
- *
1417
- * @param path - The path to the file.
1418
- * @returns `true` if the file exists, otherwise `false`.
1419
- */
1420
- existsSync(path) {
1421
- const { relativeKey, adapter } = this.#getStorage(path);
1422
- return adapter.existsSync(relativeKey);
1423
- }
1424
- /**
1425
- * Checks if a file is virtual in the virtual file system (VFS).
1426
- *
1427
- * @param path - The path to the file.
1428
- * @param importer - An optional path to the importer module.
1429
- * @param options - Additional resolution options.
1430
- * @returns `true` if the file is virtual, otherwise `false`.
1431
- */
1432
- isVirtual(path, importer, options) {
1433
- const resolved = this.resolveSync(path, importer, options);
1434
- if (!(0, _stryke_type_checks_is_set_string.isSetString)(resolved)) {
1435
- if ((0, _stryke_type_checks_is_set.isSet)(resolved)) throw new Error(`Resolved path is not a string: ${String(resolved)}`);
1436
- return false;
1437
- }
1438
- return this.#getStorage(resolved)?.adapter?.preset === "virtual" || resolved.startsWith(`${this.#context.config.framework}:`) || path.startsWith(`${this.#context.config.framework}:`);
1439
- }
1440
- /**
1441
- * Checks if a path is a directory in the virtual file system (VFS).
1442
- *
1443
- * @param path - The path to check.
1444
- * @param importer - An optional path to the importer module.
1445
- * @param options - Additional resolution options.
1446
- * @returns `true` if the path is a directory, otherwise `false`.
1447
- */
1448
- isDirectorySync(path, importer, options) {
1449
- const resolved = this.resolveSync(path, importer, options);
1450
- if (!(0, _stryke_type_checks_is_set_string.isSetString)(resolved)) {
1451
- if ((0, _stryke_type_checks_is_set.isSet)(resolved)) throw new Error(`Resolved path is not a string: ${String(resolved)}`);
1452
- return false;
1453
- }
1454
- return !!(this.existsSync(resolved) && this.#getStorage(resolved)?.adapter?.isDirectorySync(resolved));
1455
- }
1456
- /**
1457
- * Checks if a path is a directory in the virtual file system (VFS).
1458
- *
1459
- * @param path - The path to check.
1460
- * @param importer - An optional path to the importer module.
1461
- * @param options - Additional resolution options.
1462
- * @returns `true` if the path is a directory, otherwise `false`.
1463
- */
1464
- async isDirectory(path, importer, options) {
1465
- const resolved = await this.resolve(path, importer, options);
1466
- if (!(0, _stryke_type_checks_is_set_string.isSetString)(resolved)) {
1467
- if ((0, _stryke_type_checks_is_set.isSet)(resolved)) throw new Error(`Resolved path is not a string: ${String(resolved)}`);
1468
- return false;
1469
- }
1470
- return !!(await this.exists(resolved) && await this.#getStorage(resolved)?.adapter?.isDirectory(resolved));
1471
- }
1472
- /**
1473
- * Checks if a path is a file in the virtual file system (VFS).
1474
- *
1475
- * @param path - The path to check.
1476
- * @param importer - An optional path to the importer module.
1477
- * @param options - Additional resolution options.
1478
- * @returns `true` if the path is a file, otherwise `false`.
1479
- */
1480
- isFileSync(path, importer, options) {
1481
- const resolved = this.resolveSync(path, importer, options);
1482
- if (!(0, _stryke_type_checks_is_set_string.isSetString)(resolved)) {
1483
- if ((0, _stryke_type_checks_is_set.isSet)(resolved)) throw new Error(`Resolved path is not a string: ${String(resolved)}`);
1484
- return false;
1485
- }
1486
- return this.#getStorage(resolved)?.adapter?.isFileSync(resolved) ?? false;
1487
- }
1488
- /**
1489
- * Checks if a path is a file in the virtual file system (VFS).
1490
- *
1491
- * @param path - The path to check.
1492
- * @param importer - An optional path to the importer module.
1493
- * @param options - Additional resolution options.
1494
- * @returns `true` if the path is a file, otherwise `false`.
1495
- */
1496
- async isFile(path, importer, options) {
1497
- const resolved = await this.resolve(path, importer, options);
1498
- if (!(0, _stryke_type_checks_is_set_string.isSetString)(resolved)) {
1499
- if ((0, _stryke_type_checks_is_set.isSet)(resolved)) throw new Error(`Resolved path is not a string: ${String(resolved)}`);
1500
- return false;
1501
- }
1502
- return await this.#getStorage(resolved)?.adapter?.isFile(resolved) ?? false;
1503
- }
1504
- /**
1505
- * Checks if a file Id must be resolved by the virtual file system (VFS).
1506
- *
1507
- * @remarks
1508
- * Examples of file Ids that would be considered virtual include:
1509
- * - Ids that start with the framework specific prefix (e.g. `powerlines:`)
1510
- * - Ids that match a configured alias for virtual modules (returned true from {@link VirtualFileSystemInterface.isVirtual})
1511
- *
1512
- * @param path - The path or id of the file.
1513
- * @param importer - An optional path to the importer module, used for resolving the file path.
1514
- * @param options - Additional options for resolving the file path.
1515
- * @returns `true` if the file is virtual, otherwise `false`.
1516
- */
1517
- isResolvableId(path, importer, options) {
1518
- return path.startsWith(`${this.#context.config.framework}:`) || this.isVirtual(path, importer, options) || this.isAlias(path) || this.isTsconfigPath(path);
1519
- }
1520
- /**
1521
- * Lists files in a given path.
1522
- *
1523
- * @param path - The path to list files from.
1524
- * @returns An array of file names in the specified path.
1525
- */
1526
- listSync(path) {
1527
- let resolvedPath = path;
1528
- if (resolvedPath.includes("*")) {
1529
- this.#log(_storm_software_config_tools_types.LogLevelLabel.WARN, `Invoking "listSync" with a glob pattern is not supported. It is likely you meant to use "globSync". Path: ${path}`);
1530
- resolvedPath = (0, _stryke_path_correct_path.stripStars)(resolvedPath);
1531
- }
1532
- return (0, _stryke_helpers_get_unique.getUnique)(this.#getStorages(resolvedPath, true).map((storage) => storage.adapter.listSync(storage.relativeBase ? storage.base ? (0, _stryke_path_append.appendPath)(storage.relativeBase, storage.base) : storage.relativeBase : storage.base)).flat().filter(Boolean));
1533
- }
1534
- /**
1535
- * Lists files in a given path.
1536
- *
1537
- * @param path - The path to list files from.
1538
- * @returns An array of file names in the specified path.
1539
- */
1540
- async list(path) {
1541
- let resolvedPath = path;
1542
- if (resolvedPath.includes("*")) {
1543
- this.#log(_storm_software_config_tools_types.LogLevelLabel.WARN, `Invoking "list" with a glob pattern is not supported. It is likely you meant to use "glob". Path: ${path}`);
1544
- resolvedPath = (0, _stryke_path_correct_path.stripStars)(resolvedPath);
1545
- }
1546
- return (0, _stryke_helpers_get_unique.getUnique)((await Promise.all(this.#getStorages(resolvedPath, true).map(async (storage) => storage.adapter.list(storage.relativeBase ? storage.base ? (0, _stryke_path_append.appendPath)(storage.relativeBase, storage.base) : storage.relativeBase : storage.base)))).flat().filter(Boolean));
1547
- }
1548
- /**
1549
- * Removes a file in the virtual file system (VFS).
1550
- *
1551
- * @param path - The path to create the directory at.
1552
- */
1553
- async remove(path) {
1554
- const normalizedPath = this.#normalizePath(path);
1555
- this.#log(_storm_software_config_tools_types.LogLevelLabel.TRACE, `Removing file: ${normalizedPath}`);
1556
- const { relativeKey, adapter } = this.#getStorage(normalizedPath);
1557
- if ((0, _stryke_path_file_path_fns.hasFileExtension)(normalizedPath)) await adapter.remove(relativeKey);
1558
- else await adapter.clear(relativeKey);
1559
- const id = this.#ids[normalizedPath];
1560
- if (id && this.#metadata[id]) {
1561
- delete this.#metadata[id];
1562
- delete this.#ids[normalizedPath];
1563
- delete this.#paths[id];
1564
- }
1565
- }
1566
- /**
1567
- * Removes a file in the virtual file system (VFS).
1568
- *
1569
- * @param path - The path to create the directory at.
1570
- */
1571
- removeSync(path) {
1572
- const normalizedPath = this.#normalizePath(path);
1573
- this.#log(_storm_software_config_tools_types.LogLevelLabel.TRACE, `Removing file: ${normalizedPath}`);
1574
- const { relativeKey, adapter } = this.#getStorage(normalizedPath);
1575
- if ((0, _stryke_path_file_path_fns.hasFileExtension)(normalizedPath)) adapter.removeSync(relativeKey);
1576
- else adapter.clearSync(relativeKey);
1577
- const id = this.#ids[normalizedPath];
1578
- if (id && this.#metadata[id]) {
1579
- delete this.#metadata[id];
1580
- delete this.#ids[normalizedPath];
1581
- delete this.#paths[id];
1582
- }
1583
- }
1584
- /**
1585
- * Glob files in the virtual file system (VFS) based on the provided pattern(s).
1586
- *
1587
- * @param patterns - A pattern (or multiple patterns) to use to determine the file paths to return
1588
- * @returns An array of file paths matching the provided pattern(s)
1589
- */
1590
- async glob(patterns) {
1591
- const results = [];
1592
- for (const pattern of normalizeGlobPatterns(this.#context.config.cwd, patterns)) {
1593
- const normalized = this.#normalizePath(pattern);
1594
- if (!/[*?[\]{}]/.test(normalized) && !normalized.includes("*")) if (this.isDirectorySync(normalized)) results.push(...await this.list(normalized));
1595
- else {
1596
- const resolved = await this.resolve(normalized);
1597
- if (resolved && !results.includes(resolved)) results.push(resolved);
1598
- }
1599
- else {
1600
- const absPattern = (0, _stryke_path_is_type.isAbsolutePath)(normalized) ? normalized : this.#normalizePath((0, _stryke_path_append.appendPath)(normalized, this.#context.config.cwd));
1601
- await Promise.all((await this.list((0, _stryke_path_correct_path.stripStars)(absPattern))).map(async (file) => {
1602
- if ((0, _stryke_path_glob_to_regex.globToRegex)(absPattern).test(file)) {
1603
- const resolved = await this.resolve(file);
1604
- if (resolved && !results.includes(resolved)) results.push(resolved);
1605
- }
1606
- }));
1607
- }
1608
- }
1609
- return results;
1610
- }
1611
- /**
1612
- * Synchronously glob files in the virtual file system (VFS) based on the provided pattern(s).
1613
- *
1614
- * @param patterns - A pattern (or multiple patterns) to use to determine the file paths to return
1615
- * @returns An array of file paths matching the provided pattern(s)
1616
- */
1617
- globSync(patterns) {
1618
- const results = [];
1619
- for (const pattern of normalizeGlobPatterns(this.#context.config.cwd, patterns)) {
1620
- const normalized = this.#normalizePath(pattern);
1621
- if (!/[*?[\]{}]/.test(normalized) && !normalized.includes("*")) if (this.isDirectorySync(normalized)) results.push(...this.listSync(normalized));
1622
- else {
1623
- const resolved = this.resolveSync(normalized);
1624
- if (resolved && !results.includes(resolved)) results.push(resolved);
1625
- }
1626
- else {
1627
- const absPattern = (0, _stryke_path_is_type.isAbsolutePath)(normalized) ? normalized : this.#normalizePath((0, _stryke_path_append.appendPath)(normalized, this.#context.config.cwd));
1628
- const files = this.listSync((0, _stryke_path_correct_path.stripStars)(absPattern));
1629
- for (const file of files) if ((0, _stryke_path_glob_to_regex.globToRegex)(absPattern).test(file)) {
1630
- const resolved = this.resolveSync(file);
1631
- if (resolved && !results.includes(resolved)) results.push(resolved);
1632
- }
1633
- }
1634
- }
1635
- return results;
1636
- }
1637
- /**
1638
- * Copies a file from one path to another in the virtual file system (VFS).
1639
- *
1640
- * @param srcPath - The source path to copy
1641
- * @param destPath - The destination path to copy to
1642
- */
1643
- async copy(srcPath, destPath) {
1644
- const src = srcPath instanceof URL ? (0, node_url.fileURLToPath)(srcPath) : srcPath;
1645
- const dest = destPath instanceof URL ? (0, node_url.fileURLToPath)(destPath) : destPath;
1646
- if (!(0, _stryke_type_checks_is_set_string.isSetString)(src) && (!(0, _stryke_type_checks_is_set_object.isSetObject)(src) || !(0, _stryke_type_checks_is_set_string.isSetString)(src.input)) || !(0, _stryke_type_checks_is_set_string.isSetString)(dest)) return;
1647
- const sourceStr = (0, _stryke_type_checks_is_string.isString)(src) ? src : src.input ? src.input : this.#context.config.cwd;
1648
- const source = await this.resolve(sourceStr);
1649
- if (!source) return;
1650
- if (this.isDirectorySync(source) || (0, _stryke_type_checks_is_set_string.isSetString)(src) && src.includes("*") || (0, _stryke_type_checks_is_set_object.isSetObject)(src) && (0, _stryke_type_checks_is_set_string.isSetString)(src.glob)) await Promise.all((await this.glob(src)).map(async (file) => {
1651
- return this.copy(file, (0, _stryke_path_append.appendPath)((0, _stryke_path_replace.replacePath)(file, sourceStr), dest));
1652
- }));
1653
- else {
1654
- const content = await this.read(source);
1655
- if (content !== void 0) await this.write(this.#normalizePath(dest), content, { skipFormat: true });
1656
- }
1657
- }
1658
- /**
1659
- * Synchronously copies a file from one path to another in the virtual file system (VFS).
1660
- *
1661
- * @param srcPath - The source path to copy
1662
- * @param destPath - The destination path to copy to
1663
- */
1664
- copySync(srcPath, destPath) {
1665
- const src = srcPath instanceof URL ? (0, node_url.fileURLToPath)(srcPath) : srcPath;
1666
- const dest = destPath instanceof URL ? (0, node_url.fileURLToPath)(destPath) : destPath;
1667
- if (!(0, _stryke_type_checks_is_set_string.isSetString)(src) && (!(0, _stryke_type_checks_is_set_object.isSetObject)(src) || !(0, _stryke_type_checks_is_set_string.isSetString)(src.input)) || !(0, _stryke_type_checks_is_set_string.isSetString)(dest)) return;
1668
- const sourceStr = (0, _stryke_type_checks_is_string.isString)(src) ? src : src.input ? src.input : this.#context.config.cwd;
1669
- const source = this.resolveSync(sourceStr);
1670
- if (!source) return;
1671
- if (this.isDirectorySync(source) || (0, _stryke_type_checks_is_set_string.isSetString)(src) && src.includes("*") || (0, _stryke_type_checks_is_set_object.isSetObject)(src) && (0, _stryke_type_checks_is_set_string.isSetString)(src.glob)) this.globSync(src).map((file) => {
1672
- return this.copySync(file, (0, _stryke_path_append.appendPath)((0, _stryke_path_file_path_fns.findFilePath)((0, _stryke_path_replace.replacePath)(file, sourceStr)), dest));
1673
- });
1674
- else {
1675
- const content = this.readSync(source);
1676
- if (content !== void 0) this.writeSync(this.#normalizePath((0, _stryke_path_file_path_fns.hasFileExtension)(dest) ? dest : (0, _stryke_path_append.appendPath)((0, _stryke_path_file_path_fns.findFileName)(source), dest)), content, { skipFormat: true });
1677
- }
1678
- }
1679
- /**
1680
- * Moves a file (or files) from one path to another in the virtual file system (VFS).
1681
- *
1682
- * @param srcPath - The source path to move
1683
- * @param destPath - The destination path to move to
1684
- */
1685
- async move(srcPath, destPath) {
1686
- if ((0, _stryke_path_file_path_fns.hasFileExtension)(srcPath)) {
1687
- await this.copy(srcPath, destPath);
1688
- await this.remove(srcPath);
1689
- } else await Promise.all((await this.list(srcPath)).map(async (file) => {
1690
- await this.copy(file, destPath);
1691
- await this.remove(file);
1692
- }));
1693
- }
1694
- /**
1695
- * Synchronously moves a file (or files) from one path to another in the virtual file system (VFS).
1696
- *
1697
- * @param srcPath - The source path to move
1698
- * @param destPath - The destination path to move to
1699
- */
1700
- moveSync(srcPath, destPath) {
1701
- if ((0, _stryke_path_file_path_fns.hasFileExtension)(srcPath)) {
1702
- this.copySync(srcPath, destPath);
1703
- this.removeSync(srcPath);
1704
- } else this.listSync(srcPath).forEach((file) => {
1705
- this.copySync(file, destPath);
1706
- this.removeSync(file);
1707
- });
1708
- }
1709
- /**
1710
- * Asynchronously reads a file from the virtual file system (VFS).
1711
- *
1712
- * @param path - The path or ID of the file to read.
1713
- * @returns A promise that resolves to the contents of the file as a string, or undefined if the file does not exist.
1714
- */
1715
- async read(path) {
1716
- const filePath = await this.resolve(path, void 0, { isFile: true });
1717
- if (!filePath || !this.existsSync(filePath)) return;
1718
- const { adapter } = this.#getStorage(filePath);
1719
- this.#log(_storm_software_config_tools_types.LogLevelLabel.TRACE, `Reading ${adapter.name} file: ${filePath}`);
1720
- return await adapter.get(filePath) ?? void 0;
1721
- }
1722
- /**
1723
- * Synchronously reads a file from the virtual file system (VFS).
1724
- *
1725
- * @param path - The path or ID of the file to read.
1726
- * @returns The contents of the file as a string, or undefined if the file does not exist.
1727
- */
1728
- readSync(path) {
1729
- const filePath = this.resolveSync(path, void 0, { isFile: true });
1730
- if (!filePath || !this.existsSync(filePath)) return;
1731
- const { adapter } = this.#getStorage(filePath);
1732
- this.#log(_storm_software_config_tools_types.LogLevelLabel.TRACE, `Reading ${adapter.name} file: ${filePath}`);
1733
- return adapter.getSync(filePath) ?? void 0;
1734
- }
1735
- /**
1736
- * Writes a file to the virtual file system (VFS).
1737
- *
1738
- * @param path - The path to the file.
1739
- * @param data - The contents of the file.
1740
- * @param options - Optional parameters for writing the file.
1741
- * @returns A promise that resolves when the file is written.
1742
- */
1743
- async write(path, data = "", options = {}) {
1744
- const meta = options.meta ?? {};
1745
- const resolvedPath = await this.resolve(this.#normalizePath(path)) || path;
1746
- const { relativeKey, adapter } = this.#getStorage(resolvedPath, options.storage);
1747
- this.#log(_storm_software_config_tools_types.LogLevelLabel.TRACE, `Writing ${resolvedPath} to ${adapter.name === "virtual" ? "the virtual file system" : adapter.name === "file-system" ? "the local file system" : adapter.name} (size: ${(0, _stryke_string_format_pretty_bytes.prettyBytes)(new node_buffer.Blob((0, _stryke_convert_to_array.toArray)(data)).size)})`);
1748
- let code = data;
1749
- try {
1750
- if (!options.skipFormat) code = await (0, _powerlines_core_lib_utilities_format.format)(this.#context, resolvedPath, data);
1751
- } catch (err) {
1752
- if (DEFAULT_EXTENSIONS.includes((0, _stryke_path_file_path_fns.findFileExtensionSafe)(resolvedPath, { fullExtension: true }))) this.#log(_storm_software_config_tools_types.LogLevelLabel.WARN, `Failed to format file ${resolvedPath} before writing: ${err.message}`);
1753
- code = data;
1754
- }
1755
- this.#log(_storm_software_config_tools_types.LogLevelLabel.TRACE, `Writing ${resolvedPath} to ${adapter.name === "virtual" ? "the virtual file system" : adapter.name === "file-system" ? "the local file system" : adapter.name} (size: ${(0, _stryke_string_format_pretty_bytes.prettyBytes)(new node_buffer.Blob((0, _stryke_convert_to_array.toArray)(code)).size)})`);
1756
- const id = this.#normalizeId(meta.id || resolvedPath);
1757
- this.metadata[id] = {
1758
- type: "normal",
1759
- timestamp: Date.now(),
1760
- ...this.metadata[id] ?? {},
1761
- ...meta
1762
- };
1763
- this.paths[id] = resolvedPath;
1764
- this.ids[resolvedPath] = id;
1765
- return adapter.set(relativeKey, code);
1766
- }
1767
- /**
1768
- * Synchronously writes a file to the virtual file system (VFS).
1769
- *
1770
- * @param path - The file to write.
1771
- * @param data - The contents of the file.
1772
- * @param options - Optional parameters for writing the file.
1773
- */
1774
- writeSync(path, data = "", options = {}) {
1775
- const meta = options.meta ?? {};
1776
- const resolvedPath = this.resolveSync(this.#normalizePath(path)) || path;
1777
- const { relativeKey, adapter } = this.#getStorage(resolvedPath, options.storage);
1778
- this.#log(_storm_software_config_tools_types.LogLevelLabel.TRACE, `Writing ${resolvedPath} file to ${adapter.name === "virtual" ? "the virtual file system" : adapter.name === "file-system" ? "the local file system" : adapter.name} (size: ${(0, _stryke_string_format_pretty_bytes.prettyBytes)(new node_buffer.Blob((0, _stryke_convert_to_array.toArray)(data)).size)})`);
1779
- const id = this.#normalizeId(meta.id || resolvedPath);
1780
- this.metadata[id] = {
1781
- type: "normal",
1782
- timestamp: Date.now(),
1783
- ...this.metadata[id] ?? {},
1784
- ...meta
1785
- };
1786
- this.paths[id] = resolvedPath;
1787
- this.ids[resolvedPath] = id;
1788
- return adapter.setSync(relativeKey, data);
1789
- }
1790
- /**
1791
- * Synchronously creates a directory at the specified path.
1792
- *
1793
- * @param dirPath - The path of the directory to create.
1794
- */
1795
- mkdirSync(dirPath) {
1796
- return this.#getStorage(dirPath)?.adapter?.mkdirSync(dirPath);
1797
- }
1798
- /**
1799
- * Creates a directory at the specified path.
1800
- *
1801
- * @param path - The path of the directory to create.
1802
- */
1803
- async mkdir(path) {
1804
- return this.#getStorage(path)?.adapter?.mkdir(path);
1805
- }
1806
- /**
1807
- * Retrieves the metadata of a file in the virtual file system (VFS).
1808
- *
1809
- * @param pathOrId - The path or ID of the file to retrieve metadata for.
1810
- * @returns The metadata of the file, or undefined if the file does not exist.
1811
- */
1812
- getMetadata(pathOrId) {
1813
- const resolved = this.resolveSync(pathOrId);
1814
- if (resolved && this.metadata[resolved]) return this.metadata[resolved];
1815
- }
1816
- /**
1817
- * Resolves a given module ID using the configured aliases.
1818
- *
1819
- * @remarks
1820
- * This function can be used to map module IDs to different paths based on the alias configuration.
1821
- *
1822
- * @param id - The module ID to resolve.
1823
- * @returns The resolved module ID - after applying any configured aliases (this will be the same as the input ID if no aliases match).
1824
- */
1825
- resolveAlias(id) {
1826
- let path = id;
1827
- if (this.#context.config.resolve.alias) {
1828
- if (Array.isArray(this.#context.config.resolve.alias) && this.#context.config.resolve.alias.length > 0) {
1829
- const found = this.#context.config.resolve.alias.filter((alias) => (0, _stryke_type_checks_is_set_string.isSetString)(alias.find) && (alias.find === path || path.startsWith(`${alias.find}/`)) || (0, _stryke_type_checks_is_regexp.isRegExp)(alias.find) && alias.find.test(path));
1830
- if (found.length > 0) {
1831
- const alias = found.reduce((ret, current) => {
1832
- return ((0, _stryke_type_checks_is_set_string.isSetString)(ret.find) ? ret.find.length : (0, _stryke_type_checks_is_regexp.isRegExp)(ret.find) ? ret.find.source.length : 0) > ((0, _stryke_type_checks_is_set_string.isSetString)(current.find) ? current.find.length : (0, _stryke_type_checks_is_regexp.isRegExp)(current.find) ? current.find.source.length : 0) ? ret : current;
1833
- });
1834
- if ((0, _stryke_type_checks_is_set_string.isSetString)(alias.find)) path = path.replace(new RegExp(`^${alias.find}`), alias.replacement);
1835
- else if ((0, _stryke_type_checks_is_regexp.isRegExp)(alias.find)) path = path.replace(alias.find, alias.replacement);
1836
- }
1837
- } else if ((0, _stryke_type_checks_is_set_object.isSetObject)(this.#context.config.resolve.alias)) {
1838
- const found = Object.keys(this.#context.config.resolve.alias).filter((key) => key === path || path.startsWith(`${key}/`));
1839
- if (found.length > 0) {
1840
- const alias = found.reduce((ret, current) => {
1841
- return ret.length > current.length ? ret : current;
1842
- });
1843
- path = path.replace(new RegExp(`^${alias}`), this.#context.config.resolve.alias[alias]);
1844
- }
1845
- }
1846
- }
1847
- return path;
1848
- }
1849
- /**
1850
- * Checks if a given module ID is an alias.
1851
- *
1852
- * @remarks
1853
- * This function can be used to determine if a module ID matches any configured aliases.
1854
- *
1855
- * @param id - The module ID to check.
1856
- * @returns A boolean indicating whether the module ID is an alias.
1857
- */
1858
- isAlias(id) {
1859
- const path = id;
1860
- if (this.#context.config.resolve.alias) {
1861
- if (Array.isArray(this.#context.config.resolve.alias) && this.#context.config.resolve.alias.length > 0) return this.#context.config.resolve.alias.filter((alias) => (0, _stryke_type_checks_is_set_string.isSetString)(alias.find) && (alias.find === path || path.startsWith(`${alias.find}/`)) || (0, _stryke_type_checks_is_regexp.isRegExp)(alias.find) && alias.find.test(path)).length > 0;
1862
- else if ((0, _stryke_type_checks_is_set_object.isSetObject)(this.#context.config.resolve.alias)) return Object.keys(this.#context.config.resolve.alias).filter((key) => key === path || path.startsWith(`${key}/`)).length > 0;
1863
- }
1864
- return false;
1865
- }
1866
- /**
1867
- * Checks if a given module ID is a tsconfig path.
1868
- *
1869
- * @remarks
1870
- * This function can be used to determine if a module ID matches any configured tsconfig paths.
1871
- *
1872
- * @param id - The module ID to check.
1873
- * @returns A boolean indicating whether the module ID is a tsconfig path.
1874
- */
1875
- isTsconfigPath(id) {
1876
- return !!(this.#context.tsconfig.options.paths && Object.keys(this.#context.tsconfig.options.paths).length > 0 && (0, bundle_require.match)(id, this.#context.resolvePatterns));
1877
- }
1878
- /**
1879
- * A helper function to resolve modules in the virtual file system (VFS).
1880
- *
1881
- * @remarks
1882
- * This function can be used to resolve modules relative to the project root directory.
1883
- *
1884
- * @example
1885
- * ```ts
1886
- * const resolved = await context.resolvePath("some-module", "/path/to/importer");
1887
- * ```
1888
- *
1889
- * @param id - The module to resolve.
1890
- * @param importer - An optional path to the importer module.
1891
- * @param options - Additional resolution options.
1892
- * @returns A promise that resolves to the resolved module path.
1893
- */
1894
- async resolve(id, importer, options = {}) {
1895
- const origResult = await this.#innerResolve(id, importer, options);
1896
- if (origResult && options.isFile && await this.isDirectory(origResult)) {
1897
- const indexResult = await this.resolve((0, _stryke_path_join.joinPaths)(origResult, "index"), importer, options);
1898
- if (indexResult) return indexResult;
1899
- if (!(0, _stryke_path_file_path_fns.hasFileExtension)(origResult)) for (const ext of DEFAULT_EXTENSIONS) {
1900
- const extResult = await this.resolve(`${origResult}.${ext}`, importer, options);
1901
- if (extResult) return extResult;
1902
- }
1903
- return;
1904
- }
1905
- return origResult;
1906
- }
1907
- /**
1908
- * A synchronous helper function to resolve modules using the Jiti resolver
1909
- *
1910
- * @remarks
1911
- * This function can be used to resolve modules relative to the project root directory.
1912
- *
1913
- * @example
1914
- * ```ts
1915
- * const resolvedPath = context.resolveSync("some-module", "/path/to/importer");
1916
- * ```
1917
- *
1918
- * @param id - The module to resolve.
1919
- * @param importer - An optional path to the importer module.
1920
- * @param options - Additional resolution options.
1921
- * @returns The resolved module path.
1922
- */
1923
- resolveSync(id, importer, options = {}) {
1924
- const origResult = this.#innerResolveSync(id, importer, options);
1925
- if (origResult && options.isFile && this.isDirectorySync(origResult)) {
1926
- const indexResult = this.resolveSync((0, _stryke_path_join.joinPaths)(origResult, "index"), importer, options);
1927
- if (indexResult) return indexResult;
1928
- if (!(0, _stryke_path_file_path_fns.hasFileExtension)(origResult)) for (const ext of DEFAULT_EXTENSIONS) {
1929
- const extResult = this.resolveSync(`${origResult}.${ext}`, importer, options);
1930
- if (extResult) return extResult;
1931
- }
1932
- return;
1933
- }
1934
- return origResult;
1935
- }
1936
- /**
1937
- * Disposes of the virtual file system (VFS) by saving its state to disk.
1938
- */
1939
- async dispose() {
1940
- if (!this.#isDisposed) {
1941
- this.#isDisposed = true;
1942
- this.#log(_storm_software_config_tools_types.LogLevelLabel.DEBUG, "Disposing virtual file system...");
1943
- await this.remove((0, _stryke_path_join.joinPaths)(this.#context.dataPath, "fs.bin"));
1944
- const message = new _stryke_capnp.Message();
1945
- const fs = message.initRoot(FileSystem);
1946
- const storage = fs._initStorage(Object.keys(this.#paths).length);
1947
- await Promise.all(Object.values(this.#paths).map(async (path, index) => {
1948
- const code = await this.read(path);
1949
- const fd = storage.get(index);
1950
- fd.path = path;
1951
- fd.code = code || "";
1952
- }));
1953
- const ids = fs._initIds(Object.keys(this.#ids).length);
1954
- Object.entries(this.#ids).filter(([, id]) => id).forEach(([path, id], index) => {
1955
- const fileId = ids.get(index);
1956
- fileId.id = id;
1957
- fileId.path = path;
1958
- });
1959
- const metadata = fs._initMetadata(Object.keys(this.#metadata).length);
1960
- Object.entries(this.#metadata).filter(([, value]) => value).forEach(([id, value], index) => {
1961
- const fileMetadata = metadata.get(index);
1962
- fileMetadata.id = id;
1963
- fileMetadata.type = value.type;
1964
- fileMetadata.timestamp = value.timestamp ?? Date.now();
1965
- if (value.properties) {
1966
- const props = fileMetadata._initProperties(Object.keys(value.properties).length);
1967
- Object.entries(value.properties).filter(([, val]) => (0, _stryke_type_checks_is_set_string.isSetString)(val)).forEach(([key, val], index) => {
1968
- const prop = props.get(index);
1969
- prop.key = key;
1970
- prop.value = val;
1971
- });
1972
- }
1973
- });
1974
- await (0, _stryke_fs_buffer.writeFileBuffer)((0, _stryke_path_join.joinPaths)(this.#context.dataPath, "fs.bin"), message.toArrayBuffer());
1975
- if (!this.#context.config.skipCache) this.resolverCache.save(true);
1976
- await Promise.all(this.#getStorages().map(async (storage) => storage.adapter.dispose()));
1977
- this.#log(_storm_software_config_tools_types.LogLevelLabel.TRACE, "Virtual file system has been disposed.");
1978
- }
1979
- }
1980
- async [Symbol.asyncDispose]() {
1981
- return this.dispose();
1982
- }
1983
- };
1984
-
1985
- //#endregion
1986
- //#region src/typescript/tsconfig.ts
1987
- /**
1988
- * Get the path to the tsconfig.json file.
1989
- *
1990
- * @param workspaceRoot - The root directory of the workspace.
1991
- * @param projectRoot - The root directory of the project.
1992
- * @param tsconfig - The path to the tsconfig.json file.
1993
- * @returns The absolute path to the tsconfig.json file.
1994
- * @throws If the tsconfig.json file does not exist.
1995
- */
1996
- function getTsconfigFilePath(workspaceRoot, projectRoot, tsconfig) {
1997
- let tsconfigFilePath;
1998
- if (tsconfig) tsconfigFilePath = tryTsconfigFilePath(workspaceRoot, projectRoot, tsconfig);
1999
- else {
2000
- tsconfigFilePath = tryTsconfigFilePath(workspaceRoot, projectRoot, "tsconfig.app.json");
2001
- if (!tsconfigFilePath) {
2002
- tsconfigFilePath = tryTsconfigFilePath(workspaceRoot, projectRoot, "tsconfig.lib.json");
2003
- if (!tsconfigFilePath) tsconfigFilePath = tryTsconfigFilePath(workspaceRoot, projectRoot, "tsconfig.json");
2004
- }
2005
- }
2006
- if (!tsconfigFilePath) throw new Error(`Cannot find the \`tsconfig.json\` configuration file for the project at ${projectRoot}.`);
2007
- return tsconfigFilePath;
2008
- }
2009
- /**
2010
- * Get the path to the tsconfig.json file.
2011
- *
2012
- * @param workspaceRoot - The root directory of the workspace.
2013
- * @param projectRoot - The root directory of the project.
2014
- * @param tsconfig - The path to the tsconfig.json file.
2015
- * @returns The absolute path to the tsconfig.json file.
2016
- * @throws If the tsconfig.json file does not exist.
2017
- */
2018
- function tryTsconfigFilePath(workspaceRoot, projectRoot, tsconfig) {
2019
- let tsconfigFilePath = tsconfig;
2020
- if (!(0, _stryke_fs_exists.existsSync)(tsconfigFilePath)) {
2021
- tsconfigFilePath = (0, _stryke_path_append.appendPath)(tsconfig, projectRoot);
2022
- if (!(0, _stryke_fs_exists.existsSync)(tsconfigFilePath)) {
2023
- tsconfigFilePath = (0, _stryke_path_append.appendPath)(tsconfig, (0, _stryke_path_append.appendPath)(projectRoot, workspaceRoot));
2024
- if (!(0, _stryke_fs_exists.existsSync)(tsconfigFilePath)) return;
2025
- }
2026
- }
2027
- return tsconfigFilePath;
2028
- }
2029
- /**
2030
- * Check if the TypeScript configuration type matches any of the provided types.
2031
- *
2032
- * @param tsconfigType - The type from the TypeScript configuration.
2033
- * @param types - An array of type names to check against.
2034
- * @returns True if the TypeScript configuration type matches any of the provided types, false otherwise.
2035
- */
2036
- function findMatch(tsconfigType, types, extensions = [
2037
- ".ts",
2038
- ".tsx",
2039
- ".d.ts"
2040
- ]) {
2041
- 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}`));
2042
- }
2043
- /**
2044
- * Check if the TypeScript configuration type matches any of the provided types.
2045
- *
2046
- * @param tsconfigType - The type from the TypeScript configuration.
2047
- * @param types - An array of type names to check against.
2048
- * @returns True if the TypeScript configuration type matches any of the provided types, false otherwise.
2049
- */
2050
- function findIncludeMatch(tsconfigType, types) {
2051
- return findMatch(tsconfigType, types, [
2052
- ".ts",
2053
- ".tsx",
2054
- ".d.ts",
2055
- ".js",
2056
- ".jsx",
2057
- ".mjs",
2058
- ".cjs",
2059
- ".mts",
2060
- ".cts",
2061
- "/*.ts",
2062
- "/*.tsx",
2063
- "/*.d.ts",
2064
- "/*.js",
2065
- "/*.jsx",
2066
- "/*.mjs",
2067
- "/*.cjs",
2068
- "/*.mts",
2069
- "/*.cts",
2070
- "/**/*.ts",
2071
- "/**/*.tsx",
2072
- "/**/*.d.ts",
2073
- "/**/*.js",
2074
- "/**/*.jsx",
2075
- "/**/*.mjs",
2076
- "/**/*.cjs",
2077
- "/**/*.mts",
2078
- "/**/*.cts"
2079
- ]);
2080
- }
2081
- /**
2082
- * Check if the TypeScript configuration type matches any of the provided types.
2083
- *
2084
- * @param tsconfigType - The type from the TypeScript configuration.
2085
- * @param types - An array of type names to check against.
2086
- * @returns True if the TypeScript configuration type matches any of the provided types, false otherwise.
2087
- */
2088
- function isIncludeMatchFound(tsconfigType, types) {
2089
- return findIncludeMatch(tsconfigType, types) !== void 0;
2090
- }
2091
- /**
2092
- * Get the parsed TypeScript configuration.
2093
- *
2094
- * @param workspaceRoot - The root directory of the workspace.
2095
- * @param projectRoot - The root directory of the project.
2096
- * @param tsconfig - The path to the tsconfig.json file.
2097
- * @param tsconfigRaw - The raw tsconfig.json content.
2098
- * @param originalTsconfigJson - The original tsconfig.json content.
2099
- * @param host - The TypeScript parse config host.
2100
- * @returns The resolved TypeScript configuration.
2101
- */
2102
- function getParsedTypeScriptConfig(workspaceRoot, projectRoot, tsconfig, tsconfigRaw = {}, originalTsconfigJson, host = typescript.default.sys) {
2103
- const tsconfigFilePath = getTsconfigFilePath(workspaceRoot, projectRoot, tsconfig);
2104
- const tsconfigJson = (0, _stryke_fs_json.readJsonFileSync)(tsconfigFilePath);
2105
- if (!tsconfigJson) throw new Error(`Cannot find the \`tsconfig.json\` configuration file at ${(0, _stryke_path_join_paths.joinPaths)(projectRoot, tsconfig ?? "tsconfig.json")}`);
2106
- const parsedCommandLine = typescript.default.parseJsonConfigFileContent((0, defu.default)(tsconfigRaw ?? {}, tsconfigJson), host, (0, _stryke_path_append.appendPath)(projectRoot, workspaceRoot));
2107
- if (parsedCommandLine.errors.length > 0) {
2108
- const errorMessage = `Cannot parse the TypeScript compiler options. Please investigate the following issues:
2109
- ${parsedCommandLine.errors.map((error) => `- ${(error.category !== void 0 && error.code ? `[${error.category}-${error.code}]: ` : "") + error.messageText.toString()}`).join("\n")}
2110
- `;
2111
- throw new Error(errorMessage);
2112
- }
2113
- return {
2114
- ...parsedCommandLine,
2115
- originalTsconfigJson: originalTsconfigJson ?? tsconfigJson,
2116
- tsconfigJson,
2117
- tsconfigFilePath
2118
- };
2119
- }
2120
-
2121
- //#endregion
2122
- //#region src/_internal/helpers/resolver.ts
2123
- /**
2124
- * Create a Jiti resolver for the given workspace and project root.
2125
- *
2126
- * @param options - The options for creating the resolver.
2127
- * @returns A Jiti instance configured for the specified workspace and project root.
2128
- */
2129
- function resolveOptions(options) {
2130
- return (0, defu.default)(options, {
2131
- interopDefault: true,
2132
- fsCache: options.mode !== "development" ? (0, _stryke_path_join_paths.joinPaths)(options.cacheDir, "jiti") : false,
2133
- moduleCache: options.mode !== "development"
2134
- });
2135
- }
2136
- /**
2137
- * Create a Jiti resolver for the given workspace and project root.
2138
- *
2139
- * @param options - The options for creating the resolver.
2140
- * @returns A Jiti instance configured for the specified workspace and project root.
2141
- */
2142
- function createResolver(options) {
2143
- const baseResolver = (0, jiti.createJiti)((0, _stryke_path_join_paths.joinPaths)(options.workspaceRoot, options.root), resolveOptions(options));
2144
- baseResolver.plugin = (0, jiti.createJiti)((0, _stryke_path_join_paths.joinPaths)(options.workspaceRoot, options.root), resolveOptions(options));
2145
- return baseResolver;
2146
- }
2147
-
2148
- //#endregion
2149
- //#region src/context/base-context.ts
2150
- var PowerlinesBaseContext = class PowerlinesBaseContext {
2151
- #timestamp = Date.now();
2152
- #logLevel = "info";
2153
- /**
2154
- * The path to the Powerlines package
2155
- */
2156
- powerlinesPath;
2157
- /**
2158
- * The module resolver for the project
2159
- */
2160
- resolver;
2161
- /**
2162
- * The options provided to the Powerlines process
2163
- */
2164
- options;
2165
- /**
2166
- * The parsed configuration file for the project
2167
- */
2168
- configFile;
2169
- /**
2170
- * A timestamp representing when the context was initialized
2171
- */
2172
- get timestamp() {
2173
- return new Date(this.#timestamp);
2174
- }
2175
- get logLevel() {
2176
- return this.#logLevel || "info";
2177
- }
2178
- set logLevel(level) {
2179
- this.#logLevel = level;
2180
- }
2181
- /**
2182
- * The logger function
2183
- */
2184
- get log() {
2185
- const level = this.logLevel || "info";
2186
- if (!this.logger || this.logger.level !== level) this.logger = {
2187
- log: this.createLog(),
2188
- level
2189
- };
2190
- return this.logger.log;
2191
- }
2192
- /**
2193
- * The environment paths for the project
2194
- */
2195
- get envPaths() {
2196
- return (0, _stryke_env_get_env_paths.getEnvPaths)({
2197
- orgId: this.options.organization,
2198
- appId: this.options.framework || "powerlines",
2199
- workspaceRoot: this.options.cwd || process.cwd() || "."
2200
- });
2201
- }
2202
- /**
2203
- * Creates a clone of the current context with the same configuration and workspace settings. This can be useful for running multiple builds in parallel or for creating isolated contexts for different parts of the build process.
2204
- *
2205
- * @remarks
2206
- * The cloned context will have the same configuration and workspace settings as the original context, but will have a different build ID, release ID, and timestamp. The virtual file system and caches will also be separate between the original and cloned contexts.
2207
- *
2208
- * @returns A promise that resolves to the cloned context.
2209
- */
2210
- async clone() {
2211
- const clone = new PowerlinesBaseContext();
2212
- await clone.init(this.options);
2213
- return clone;
2214
- }
2215
- /**
2216
- * A logging function for fatal messages
2217
- *
2218
- * @param message - The message to log.
2219
- */
2220
- fatal(message) {
2221
- this.log(_storm_software_config_tools_types.LogLevelLabel.FATAL, (0, _stryke_type_checks_is_string.isString)(message) ? message : _stryke_json_storm_json.StormJSON.stringify(message));
2222
- }
2223
- /**
2224
- * A logging function for error messages
2225
- *
2226
- * @param message - The message to log.
2227
- */
2228
- error(message) {
2229
- this.log(_storm_software_config_tools_types.LogLevelLabel.ERROR, (0, _stryke_type_checks_is_string.isString)(message) ? message : _stryke_json_storm_json.StormJSON.stringify(message));
2230
- }
2231
- /**
2232
- * A logging function for warning messages
2233
- *
2234
- * @param message - The message to log.
2235
- */
2236
- warn(message) {
2237
- this.log(_storm_software_config_tools_types.LogLevelLabel.WARN, (0, _stryke_type_checks_is_string.isString)(message) ? message : _stryke_json_storm_json.StormJSON.stringify(message));
2238
- }
2239
- /**
2240
- * A logging function for informational messages
2241
- *
2242
- * @param message - The message to log.
2243
- */
2244
- info(message) {
2245
- this.log(_storm_software_config_tools_types.LogLevelLabel.INFO, (0, _stryke_type_checks_is_string.isString)(message) ? message : _stryke_json_storm_json.StormJSON.stringify(message));
2246
- }
2247
- /**
2248
- * A logging function for debug messages
2249
- *
2250
- * @param message - The message to log.
2251
- */
2252
- debug(message) {
2253
- this.log(_storm_software_config_tools_types.LogLevelLabel.DEBUG, (0, _stryke_type_checks_is_string.isString)(message) ? message : _stryke_json_storm_json.StormJSON.stringify(message));
2254
- }
2255
- /**
2256
- * A logging function for trace messages
2257
- *
2258
- * @param message - The message to log.
2259
- */
2260
- trace(message) {
2261
- this.log(_storm_software_config_tools_types.LogLevelLabel.TRACE, (0, _stryke_type_checks_is_string.isString)(message) ? message : _stryke_json_storm_json.StormJSON.stringify(message));
2262
- }
2263
- /**
2264
- * A function to create a timer for measuring the duration of asynchronous operations
2265
- *
2266
- * @example
2267
- * ```ts
2268
- * const stopTimer = context.timer("Your Async Operation");
2269
- * await performAsyncOperation();
2270
- * stopTimer(); // "Your Async Operation completed in 123.45 milliseconds"
2271
- * ```
2272
- *
2273
- * @param name - The name of the timer.
2274
- * @returns A function that, when called, stops the timer and logs the duration.
2275
- */
2276
- timer(name) {
2277
- const startDate = Date.now();
2278
- const startDuration = performance.now();
2279
- return () => {
2280
- const duration = performance.now() - startDuration;
2281
- this.log(_storm_software_config_tools_types.LogLevelLabel.PERFORMANCE, `${chalk.default.bold.cyanBright(name)} completed in ${chalk.default.bold.cyanBright(duration < 1e3 ? `${duration.toFixed(2)} milliseconds` : (0, date_fns_formatDistanceToNowStrict.formatDistanceToNowStrict)(startDate))}`);
2282
- };
2283
- }
2284
- /**
2285
- * Create a new logger instance
2286
- *
2287
- * @param name - The name to use for the logger instance
2288
- * @returns A logger function
2289
- */
2290
- createLog(name = null) {
2291
- return (0, _powerlines_core_lib_logger.createLog)(name, {
2292
- ...this.options,
2293
- logLevel: (0, _stryke_type_checks_is_null.isNull)(this.logLevel) ? "silent" : this.logLevel
2294
- });
2295
- }
2296
- /**
2297
- * Extend the current logger instance with a new name
2298
- *
2299
- * @param name - The name to use for the extended logger instance
2300
- * @returns A logger function
2301
- */
2302
- extendLog(name) {
2303
- return (0, _powerlines_core_lib_logger.extendLog)(this.log, name);
2304
- }
2305
- /**
2306
- * A logger function specific to this context
2307
- */
2308
- logger;
2309
- /**
2310
- * Initialize the context with the provided configuration options
2311
- *
2312
- * @remarks
2313
- * This method will set up the resolver and load the user configuration file based on the provided options. It is called during the construction of the context and can also be called when cloning the context to ensure that the new context has the same configuration and resolver setup.
2314
- *
2315
- * @param options - The configuration options to initialize the context with
2316
- */
2317
- async init(options = {}) {
2318
- if (!this.powerlinesPath) {
2319
- const powerlinesPath = await (0, _stryke_fs_resolve.resolvePackage)("powerlines");
2320
- if (!powerlinesPath) throw new Error("Could not resolve `powerlines` package location.");
2321
- this.powerlinesPath = powerlinesPath;
2322
- }
2323
- this.options = (0, defu.default)({
2324
- root: options.root,
2325
- cwd: options.cwd,
2326
- mode: options.mode,
2327
- framework: options.framework,
2328
- organization: options.organization,
2329
- configFile: options.configFile
2330
- }, this.options ?? {}, {
2331
- cwd: process.cwd() || ".",
2332
- mode: "production",
2333
- framework: "powerlines"
2334
- });
2335
- this.resolver = createResolver({
2336
- workspaceRoot: this.options.cwd,
2337
- root: this.options.root,
2338
- cacheDir: this.envPaths.cache,
2339
- mode: this.options.mode,
2340
- logLevel: this.logLevel
2341
- });
2342
- this.configFile = await (0, _powerlines_core_lib_config.loadUserConfigFile)(this.options, this.resolver);
2343
- }
2344
- };
2345
-
2346
- //#endregion
2347
- //#region src/context/context.ts
2348
- (0, undici.setGlobalDispatcher)(new undici.Agent({ keepAliveTimeout: 1e4 }).compose(undici.interceptors.retry({
2349
- maxRetries: 3,
2350
- minTimeout: 1e3,
2351
- maxTimeout: 1e4,
2352
- timeoutFactor: 2,
2353
- retryAfter: true
2354
- })));
2355
- const SKIP_CLONING_PROPS = [
2356
- "dependencies",
2357
- "devDependencies",
2358
- "persistedMeta",
2359
- "packageJson",
2360
- "projectJson",
2361
- "tsconfig",
2362
- "resolver",
2363
- "fs",
2364
- "$$internal"
2365
- ];
2366
- var PowerlinesContext = class PowerlinesContext extends PowerlinesBaseContext {
2367
- /**
2368
- * Internal references storage
2369
- *
2370
- * @danger
2371
- * This field is for internal use only and should not be accessed or modified directly. It is unstable and can be changed at anytime.
2372
- *
2373
- * @internal
2374
- */
2375
- #internal = {};
2376
- #checksum = null;
2377
- #buildId = (0, _stryke_unique_id_uuid.uuid)();
2378
- #releaseId = (0, _stryke_unique_id_uuid.uuid)();
2379
- #fs;
2380
- #tsconfig;
2381
- #parserCache;
2382
- #requestCache;
2383
- /**
2384
- * Create a new Storm context from the workspace root and user config.
2385
- *
2386
- * @param options - The options for resolving the context.
2387
- * @returns A promise that resolves to the new context.
2388
- */
2389
- static async fromOptions(options) {
2390
- const context = new PowerlinesContext(options);
2391
- await context.init(options);
2392
- const powerlinesPath = await (0, _stryke_fs_resolve.resolvePackage)("powerlines");
2393
- if (!powerlinesPath) throw new Error("Could not resolve `powerlines` package location.");
2394
- context.powerlinesPath = powerlinesPath;
2395
- return context;
2396
- }
2397
- /**
2398
- * The options provided to the Powerlines process
2399
- */
2400
- options;
2401
- /**
2402
- * An object containing the dependencies that should be installed for the project
2403
- */
2404
- dependencies = {};
2405
- /**
2406
- * An object containing the development dependencies that should be installed for the project
2407
- */
2408
- devDependencies = {};
2409
- /**
2410
- * The persisted meta information about the current build
2411
- */
2412
- persistedMeta = void 0;
2413
- /**
2414
- * The parsed `package.json` file for the project
2415
- */
2416
- packageJson;
2417
- /**
2418
- * The parsed `project.json` file for the project
2419
- */
2420
- projectJson = void 0;
2421
- /**
2422
- * The resolved tsconfig file paths for the project
2423
- */
2424
- resolvePatterns = [];
2425
- /**
2426
- * Internal context fields and methods
2427
- *
2428
- * @danger
2429
- * This field is for internal use only and should not be accessed or modified directly. It is unstable and can be changed at anytime.
2430
- *
2431
- * @internal
2432
- */
2433
- get $$internal() {
2434
- return this.#internal;
2435
- }
2436
- /**
2437
- * Internal context fields and methods
2438
- *
2439
- * @danger
2440
- * This field is for internal use only and should not be accessed or modified directly. It is unstable and can be changed at anytime.
2441
- *
2442
- * @internal
2443
- */
2444
- set $$internal(value) {
2445
- this.#internal = value;
2446
- }
2447
- /**
2448
- * The resolved entry type definitions for the project
2449
- */
2450
- get entry() {
2451
- const entry = this.resolvedEntry;
2452
- return (0, _powerlines_core_lib_entry.resolveInputsSync)(this, entry && entry.length > 0 ? entry : Array.isArray(this.config.input) || (0, _stryke_type_checks_is_set_object.isSetObject)(this.config.input) && !(0, _powerlines_core_lib_entry.isTypeDefinition)(this.config.input) ? this.config.input : (0, _stryke_convert_to_array.toArray)(this.config.input).flat());
2453
- }
2454
- /**
2455
- * The TypeScript configuration parsed from the tsconfig file
2456
- */
2457
- get tsconfig() {
2458
- if (!this.#tsconfig) this.tsconfig = { tsconfigFilePath: this.config.tsconfig };
2459
- return this.#tsconfig;
2460
- }
2461
- /**
2462
- * Sets the TypeScript configuration parsed from the tsconfig file
2463
- */
2464
- set tsconfig(value) {
2465
- this.#tsconfig = value;
2466
- this.resolvePatterns = (0, bundle_require.tsconfigPathsToRegExp)(value?.options?.paths ?? {});
2467
- }
2468
- /**
2469
- * The virtual file system interface for the project
2470
- */
2471
- get fs() {
2472
- if (!this.#fs) this.#fs = VirtualFileSystem.createSync(this);
2473
- return this.#fs;
2474
- }
2475
- /**
2476
- * Get the checksum of the project's current state
2477
- */
2478
- get checksum() {
2479
- return this.#checksum;
2480
- }
2481
- /**
2482
- * The meta information about the current build
2483
- */
2484
- get meta() {
2485
- return {
2486
- buildId: this.#buildId,
2487
- releaseId: this.#releaseId,
2488
- checksum: this.#checksum,
2489
- timestamp: this.timestamp.getTime(),
2490
- rootHash: (0, _stryke_hash.murmurhash)({
2491
- workspaceRoot: this.options?.cwd,
2492
- root: this.config?.root
2493
- }, { maxLength: _powerlines_core_constants.ROOT_HASH_LENGTH }),
2494
- configHash: (0, _stryke_hash.murmurhash)(this.config, { maxLength: _powerlines_core_constants.CACHE_HASH_LENGTH })
2495
- };
2496
- }
2497
- /**
2498
- * The resolved configuration options
2499
- */
2500
- get config() {
2501
- return this.resolvedConfig;
2502
- }
2503
- /**
2504
- * Get the path to the artifacts directory for the project
2505
- */
2506
- get artifactsPath() {
2507
- return (0, _stryke_path_join.joinPaths)(this.config.cwd, this.config.root, this.config.output.artifactsPath);
2508
- }
2509
- /**
2510
- * Get the path to the builtin modules used by the project
2511
- */
2512
- get builtinsPath() {
2513
- return (0, _stryke_path_join.joinPaths)(this.artifactsPath, "builtins");
2514
- }
2515
- /**
2516
- * Get the path to the entry directory for the project
2517
- */
2518
- get entryPath() {
2519
- return (0, _stryke_path_join.joinPaths)(this.artifactsPath, "entry");
2520
- }
2521
- /**
2522
- * Get the path to the infrastructure modules used by the project
2523
- */
2524
- get infrastructurePath() {
2525
- return (0, _stryke_path_join.joinPaths)(this.artifactsPath, "infrastructure");
2526
- }
2527
- /**
2528
- * Get the path to the data directory for the project
2529
- */
2530
- get dataPath() {
2531
- return (0, _stryke_path_join.joinPaths)(this.envPaths.data, "projects", getPrefixedRootHash(this.config.name, this.meta.rootHash));
2532
- }
2533
- /**
2534
- * Get the path to the cache directory for the project
2535
- */
2536
- get cachePath() {
2537
- return (0, _stryke_path_join.joinPaths)(this.envPaths.cache, "projects", (0, _stryke_hash.murmurhash)({
2538
- checksum: this.#checksum,
2539
- config: this.meta.configHash
2540
- }, { maxLength: _powerlines_core_constants.CACHE_HASH_LENGTH }));
2541
- }
2542
- /**
2543
- * Get the path to the generated declaration file for the project
2544
- */
2545
- get typesPath() {
2546
- return this.config.output.types ? (0, _stryke_path_append.appendPath)(this.config.output.types, this.config.cwd) : (0, _stryke_path_join.joinPaths)(this.config.cwd, this.config.root, "powerlines.d.ts");
2547
- }
2548
- /**
2549
- * Get the project root relative to the workspace root
2550
- */
2551
- get relativeToWorkspaceRoot() {
2552
- return (0, _stryke_fs_get_workspace_root.relativeToWorkspaceRoot)(this.config.root);
2553
- }
2554
- /**
2555
- * The builtin module id that exist in the Powerlines virtual file system
2556
- */
2557
- get builtins() {
2558
- return Object.values(this.fs.metadata).filter((meta) => meta && meta.type === "builtin").map((meta) => meta?.id).filter(Boolean);
2559
- }
2560
- /**
2561
- * Additional arguments provided during execution of the command, such as CLI flags or other parameters that may be relevant to the command being executed.
2562
- */
2563
- get additionalArgs() {
2564
- return Object.entries(this.config.inlineConfig?.additionalArgs ?? {}).reduce((ret, [key, value]) => {
2565
- const formattedKey = key.replace(/^--?/, "");
2566
- if (ret[formattedKey]) if (Array.isArray(ret[formattedKey])) if (Array.isArray(value)) ret[formattedKey] = [...(0, _stryke_convert_to_array.toArray)(ret[formattedKey]), ...value];
2567
- else ret[formattedKey] = [...(0, _stryke_convert_to_array.toArray)(ret[formattedKey]), value];
2568
- else ret[formattedKey] = [ret[formattedKey], ...Array.isArray(value) ? value : [value]];
2569
- else ret[formattedKey] = value;
2570
- return ret;
2571
- }, {});
2572
- }
2573
- /**
2574
- * The alias mappings for the project used during module resolution
2575
- *
2576
- * @remarks
2577
- * This includes both the built-in module aliases as well as any custom aliases defined in the build configuration.
2578
- */
2579
- get alias() {
2580
- return this.builtins.reduce((ret, id) => {
2581
- const moduleId = `${this.config?.framework || "powerlines"}:${id.replace(/^.*:/, "")}`;
2582
- if (!ret[moduleId]) {
2583
- const path = this.fs.paths[id];
2584
- if (path) ret[moduleId] = path;
2585
- }
2586
- return ret;
2587
- }, this.config.resolve.alias ? Array.isArray(this.config.resolve.alias) ? this.config.resolve.alias.reduce((ret, alias) => {
2588
- if (!ret[alias.find.toString()]) ret[alias.find.toString()] = alias.replacement;
2589
- return ret;
2590
- }, {}) : this.config.resolve.alias : {});
2591
- }
2592
- /**
2593
- * Gets the parser cache.
2594
- */
2595
- get parserCache() {
2596
- if (!this.#parserCache) this.#parserCache = (0, flat_cache.create)({
2597
- cacheId: "parser",
2598
- cacheDir: this.cachePath,
2599
- ttl: 7200 * 1e3,
2600
- lruSize: 5e3,
2601
- persistInterval: 250
2602
- });
2603
- return this.#parserCache;
2604
- }
2605
- /**
2606
- * Gets the request cache.
2607
- */
2608
- get requestCache() {
2609
- if (!this.#requestCache) this.#requestCache = (0, flat_cache.create)({
2610
- cacheId: "http",
2611
- cacheDir: this.cachePath,
2612
- ttl: 360 * 60 * 1e3,
2613
- lruSize: 5e3,
2614
- persistInterval: 250
2615
- });
2616
- return this.#requestCache;
2617
- }
2618
- /**
2619
- * The entry points that exist in the Powerlines virtual file system
2620
- */
2621
- get resolvedEntry() {
2622
- return Object.entries(this.fs.metadata).filter(([, meta]) => meta && meta.type === "entry").map(([path, meta]) => {
2623
- const typeDefinition = { file: path };
2624
- if (meta.properties) {
2625
- if ((0, _stryke_type_checks_is_set_string.isSetString)(meta.properties.file)) typeDefinition.file = meta.properties.file;
2626
- if ((0, _stryke_type_checks_is_set_string.isSetString)(meta.properties.name)) typeDefinition.name = meta.properties.name;
2627
- if ((0, _stryke_type_checks_is_set_string.isSetString)(meta.properties["input.file"]) || (0, _stryke_type_checks_is_set_string.isSetString)(meta.properties["input.name"])) {
2628
- typeDefinition.input ??= {};
2629
- if ((0, _stryke_type_checks_is_set_string.isSetString)(meta.properties["input.file"])) typeDefinition.input.file = meta.properties["input.file"];
2630
- if ((0, _stryke_type_checks_is_set_string.isSetString)(meta.properties["input.name"])) typeDefinition.input.name = meta.properties["input.name"];
2631
- }
2632
- if ((0, _stryke_type_checks_is_set_string.isSetString)(meta.properties.output)) typeDefinition.output = meta.properties.output;
2633
- }
2634
- return typeDefinition;
2635
- }).filter(Boolean);
2636
- }
2637
- /**
2638
- * Creates a new StormContext instance.
2639
- *
2640
- * @param options - The options to use for creating the context, including the resolved configuration and workspace settings.
2641
- */
2642
- constructor(options) {
2643
- super();
2644
- this.options = options;
2645
- }
2646
- /**
2647
- * Creates a clone of the current context with the same configuration and workspace settings. This can be useful for running multiple builds in parallel or for creating isolated contexts for different parts of the build process.
2648
- *
2649
- * @remarks
2650
- * The cloned context will have the same configuration and workspace settings as the original context, but will have a different build ID, release ID, and timestamp. The virtual file system and caches will also be separate between the original and cloned contexts.
2651
- *
2652
- * @returns A promise that resolves to the cloned context.
2653
- */
2654
- async clone() {
2655
- const clone = await PowerlinesContext.fromOptions(this.config);
2656
- return this.copyTo(clone);
2657
- }
2658
- /**
2659
- * Create a new logger instance
2660
- *
2661
- * @param name - The name to use for the logger instance
2662
- * @returns A logger function
2663
- */
2664
- createLog(name = null) {
2665
- return (0, _powerlines_core_lib_logger.createLog)(name, {
2666
- ...this.config,
2667
- logLevel: (0, _stryke_type_checks_is_null.isNull)(this.logLevel) ? "silent" : this.logLevel
2668
- });
2669
- }
2670
- /**
2671
- * A function to perform HTTP fetch requests
2672
- *
2673
- * @remarks
2674
- * This function uses a caching layer to avoid duplicate requests during the Powerlines process.
2675
- *
2676
- * @example
2677
- * ```ts
2678
- * const response = await context.fetch("https://api.example.com/data");
2679
- * const data = await response.json();
2680
- * ```
2681
- *
2682
- * @see https://github.com/nodejs/undici
2683
- *
2684
- * @param input - The URL to fetch.
2685
- * @param options - The fetch request options.
2686
- * @returns A promise that resolves to a response returned by the fetch.
2687
- */
2688
- async fetch(input, options = {}) {
2689
- const cacheKey = (0, _stryke_hash.murmurhash)({
2690
- input: input.toString(),
2691
- options: JSON.stringify(options)
2692
- });
2693
- if (!this.config.skipCache && !options.skipCache) {
2694
- const cached = this.requestCache.get(cacheKey);
2695
- if (cached) return new undici.Response(cached.body, {
2696
- status: cached.status,
2697
- statusText: cached.statusText,
2698
- headers: cached.headers
2699
- });
2700
- }
2701
- const response = await (0, _stryke_http_fetch.fetchRequest)(input, {
2702
- timeout: 12e3,
2703
- ...options
2704
- });
2705
- const result = {
2706
- body: await response.text(),
2707
- status: response.status,
2708
- statusText: response.statusText,
2709
- headers: Object.fromEntries(response.headers.entries())
2710
- };
2711
- if (!this.config.skipCache && !options.skipCache) try {
2712
- this.requestCache.set(cacheKey, result);
2713
- } catch {}
2714
- return new undici.Response(result.body, {
2715
- status: result.status,
2716
- statusText: result.statusText,
2717
- headers: result.headers
2718
- });
2719
- }
2720
- /**
2721
- * Parse code using [Oxc-Parser](https://github.com/oxc/oxc) into an (ESTree-compatible)[https://github.com/estree/estree] AST object.
2722
- *
2723
- * @remarks
2724
- * This function can be used to parse TypeScript code into an AST for further analysis or transformation.
2725
- *
2726
- * @example
2727
- * ```ts
2728
- * const ast = context.parse("const x: number = 42;");
2729
- * ```
2730
- *
2731
- * @see https://rollupjs.org/plugin-development/#this-parse
2732
- * @see https://github.com/oxc/oxc
2733
- *
2734
- * @param code - The source code to parse.
2735
- * @param options - The options to pass to the parser.
2736
- * @returns An (ESTree-compatible)[https://github.com/estree/estree] AST object.
2737
- */
2738
- async parse(code, options = {}) {
2739
- const cacheKey = (0, _stryke_hash.murmurhash)({
2740
- code,
2741
- options
2742
- });
2743
- let result;
2744
- if (!this.config.skipCache) {
2745
- result = this.parserCache.get(cacheKey);
2746
- if (result) return result;
2747
- }
2748
- result = await (0, oxc_parser.parse)(`source.${options.lang || "ts"}`, code, {
2749
- ...options,
2750
- sourceType: "module",
2751
- showSemanticErrors: this.config.mode === "development"
2752
- });
2753
- if (!this.config.skipCache) this.parserCache.set(cacheKey, result);
2754
- return result;
2755
- }
2756
- /**
2757
- * A helper function to resolve modules in the Virtual File System
2758
- *
2759
- * @remarks
2760
- * This function can be used to resolve modules relative to the project root directory.
2761
- *
2762
- * @example
2763
- * ```ts
2764
- * const resolved = await context.resolve("some-module", "/path/to/importer");
2765
- * ```
2766
- *
2767
- * @param id - The module to resolve.
2768
- * @param importer - An optional path to the importer module.
2769
- * @param options - Additional resolution options.
2770
- * @returns A promise that resolves to the resolved module path.
2771
- */
2772
- async resolve(id, importer, options = {}) {
2773
- let moduleId = id;
2774
- if (this.config.resolve.alias) {
2775
- if (Array.isArray(this.config.resolve.alias)) {
2776
- const alias = this.config.resolve.alias.find((a) => (0, bundle_require.match)(moduleId, [a.find]));
2777
- if (alias) moduleId = alias.replacement;
2778
- } else if ((0, _stryke_type_checks_is_set_object.isSetObject)(this.config.resolve.alias) && this.config.resolve.alias[id]) moduleId = this.config.resolve.alias[id];
2779
- }
2780
- if (this.fs.isResolvableId(moduleId) || importer && this.fs.isResolvableId(importer)) {
2781
- let resolvedImporter = importer;
2782
- if (importer && this.fs.isResolvableId(importer)) resolvedImporter = await this.fs.resolve(importer, void 0, (0, defu.default)({
2783
- conditions: this.config.resolve.conditions,
2784
- extensions: this.config.resolve.extensions
2785
- }, options));
2786
- const result = await this.fs.resolve(moduleId, resolvedImporter, (0, defu.default)({
2787
- conditions: this.config.resolve.conditions,
2788
- extensions: this.config.resolve.extensions
2789
- }, options));
2790
- if (!result) return;
2791
- const external = Boolean(!(0, bundle_require.match)(moduleId, this.config.resolve.noExternal) && ((0, bundle_require.match)(moduleId, this.config.resolve.external) || moduleId.startsWith("node:") || (!this.fs.isVirtual(moduleId, importer, options) || this.fs.isVirtual(moduleId, importer, options) && this.config.projectType !== "application") && this.config.resolve.skipNodeModulesBundle && !/^[A-Z]:[/\\]|^\.{0,2}\/|^\.{1,2}$/.test(moduleId)));
2792
- return {
2793
- id: result,
2794
- external,
2795
- virtual: !external
2796
- };
2797
- }
2798
- if (this.config.resolve.skipNodeModulesBundle) {
2799
- if ((0, bundle_require.match)(moduleId, this.resolvePatterns) || (0, bundle_require.match)(moduleId, this.config.resolve.noExternal)) return;
2800
- if ((0, bundle_require.match)(moduleId, this.config.resolve.external) || moduleId.startsWith("node:")) return {
2801
- id: moduleId,
2802
- external: true,
2803
- virtual: false
2804
- };
2805
- if (!/^[A-Z]:[/\\]|^\.{0,2}\/|^\.{1,2}$/.test(moduleId)) return {
2806
- id: moduleId,
2807
- external: true,
2808
- virtual: false
2809
- };
2810
- } else {
2811
- if ((0, bundle_require.match)(moduleId, this.config.resolve.noExternal)) return;
2812
- if ((0, bundle_require.match)(moduleId, this.config.resolve.external) || moduleId.startsWith("node:")) return {
2813
- id: moduleId,
2814
- external: true,
2815
- virtual: false
2816
- };
2817
- }
2818
- }
2819
- /**
2820
- * A helper function to load modules from the Virtual File System
2821
- *
2822
- * @remarks
2823
- * This function can be used to load modules relative to the project root directory.
2824
- *
2825
- * @example
2826
- * ```ts
2827
- * const module = await context.load("some-module", "/path/to/importer");
2828
- * ```
2829
- *
2830
- * @param id - The module to load.
2831
- * @returns A promise that resolves to the loaded module.
2832
- */
2833
- async load(id) {
2834
- const resolvedId = await this.fs.resolve(id);
2835
- if (!resolvedId) return;
2836
- const code = await this.fs.read(resolvedId);
2837
- if (!code) return;
2838
- return {
2839
- code,
2840
- map: null
2841
- };
2842
- }
2843
- /**
2844
- * Get the builtin virtual files that exist in the Powerlines virtual file system
2845
- */
2846
- async getBuiltins() {
2847
- return Promise.all(Object.entries(this.fs.metadata).filter(([, meta]) => meta && meta.type === "builtin").map(async ([id, meta]) => {
2848
- const code = await this.fs.read(id);
2849
- const path = this.fs.paths[id];
2850
- return {
2851
- ...meta,
2852
- path,
2853
- code
2854
- };
2855
- }));
2856
- }
2857
- /**
2858
- * Resolves a file and writes it to the VFS if it does not already exist
2859
- *
2860
- * @param code - The source code of the file
2861
- * @param path - The path to write the file to
2862
- * @param options - Additional options for writing the file
2863
- */
2864
- async emit(code, path, options = {}) {
2865
- const filePath = options.extension ? (0, _stryke_path_file_path_fns.findFileExtensionSafe)(path) ? options.extension.startsWith(".") ? path.replace((0, _stryke_path_file_path_fns.findFileDotExtensionSafe)(path), options.extension) : path.replace((0, _stryke_path_file_path_fns.findFileExtensionSafe)(path), options.extension) : options.extension.startsWith(".") ? `${path}${options.extension}` : `${path}.${options.extension}` : (0, _stryke_path_file_path_fns.findFileExtensionSafe)(path) ? path : `${path}.ts`;
2866
- if ((0, _stryke_type_checks_is_function.isFunction)(this.emitFile) && options.emitWithBundler) return this.emitFile({
2867
- needsCodeReference: options.needsCodeReference,
2868
- originalFileName: options.originalFileName,
2869
- fileName: filePath,
2870
- source: code,
2871
- type: "asset"
2872
- });
2873
- return this.fs.write(filePath, code, options);
2874
- }
2875
- /**
2876
- * Synchronously resolves a file and writes it to the VFS if it does not already exist
2877
- *
2878
- * @param code - The source code of the file
2879
- * @param path - The path to write the file to
2880
- * @param options - Additional options for writing the file
2881
- */
2882
- emitSync(code, path, options = {}) {
2883
- const filePath = options.extension ? (0, _stryke_path_file_path_fns.findFileExtensionSafe)(path) ? options.extension.startsWith(".") ? path.replace((0, _stryke_path_file_path_fns.findFileDotExtensionSafe)(path), options.extension) : path.replace((0, _stryke_path_file_path_fns.findFileExtensionSafe)(path), options.extension) : options.extension.startsWith(".") ? `${path}${options.extension}` : `${path}.${options.extension}` : (0, _stryke_path_file_path_fns.findFileExtensionSafe)(path) ? path : `${path}.ts`;
2884
- if ((0, _stryke_type_checks_is_function.isFunction)(this.emitFile) && options.emitWithBundler) return this.emitFile({
2885
- needsCodeReference: options.needsCodeReference,
2886
- originalFileName: options.originalFileName,
2887
- fileName: filePath,
2888
- source: code,
2889
- type: "asset"
2890
- });
2891
- return this.fs.writeSync(filePath, code, options);
2892
- }
2893
- /**
2894
- * Resolves a entry virtual file and writes it to the VFS if it does not already exist
2895
- *
2896
- * @param code - The source code of the entry file
2897
- * @param path - A path to write the entry file to
2898
- * @param options - Optional write file options
2899
- */
2900
- async emitEntry(code, path, options = {}) {
2901
- return this.emit(code, (0, _stryke_path_append.appendPath)(path, this.entryPath), (0, defu.default)({ meta: {
2902
- type: "entry",
2903
- properties: {
2904
- file: (0, _stryke_path_append.appendPath)(path, this.entryPath),
2905
- name: options?.name,
2906
- output: options?.output,
2907
- "input.file": options?.input?.file,
2908
- "input.name": options?.input?.name
2909
- }
2910
- } }, (0, _stryke_helpers_omit.omit)(options, ["name"])));
2911
- }
2912
- /**
2913
- * Synchronously resolves a entry virtual file and writes it to the VFS if it does not already exist
2914
- *
2915
- * @param code - The source code of the entry file
2916
- * @param path - A path to write the entry file to
2917
- * @param options - Optional write file options
2918
- */
2919
- emitEntrySync(code, path, options = {}) {
2920
- return this.emitSync(code, (0, _stryke_path_append.appendPath)(path, this.entryPath), (0, defu.default)({ meta: {
2921
- type: "entry",
2922
- properties: {
2923
- file: (0, _stryke_path_append.appendPath)(path, this.entryPath),
2924
- name: options?.name,
2925
- output: options?.output,
2926
- "input.file": options?.input?.file,
2927
- "input.name": options?.input?.name
2928
- }
2929
- } }, (0, _stryke_helpers_omit.omit)(options, ["name"])));
2930
- }
2931
- /**
2932
- * Resolves a builtin virtual file and writes it to the VFS if it does not already exist
2933
- *
2934
- * @param code - The source code of the builtin file
2935
- * @param id - The unique identifier of the builtin file
2936
- * @param options - Optional write file options
2937
- */
2938
- async emitBuiltin(code, id, options = {}) {
2939
- if (!this.builtinsPath) throw new Error(`The builtins path is not set. Cannot emit builtin file with id "${id}".`);
2940
- if (!(0, _stryke_type_checks_is_set_string.isSetString)(id)) throw new Error(`The builtin id must be a non-empty string. Received: ${String(id)}`);
2941
- return this.emit(code, (0, _stryke_path_append.appendPath)(id, this.builtinsPath), (0, defu.default)(options, { meta: {
2942
- type: "builtin",
2943
- id
2944
- } }));
2945
- }
2946
- /**
2947
- * Synchronously resolves a builtin virtual file and writes it to the VFS if it does not already exist
2948
- *
2949
- * @param code - The source code of the builtin file
2950
- * @param id - The unique identifier of the builtin file
2951
- * @param options - Optional write file options
2952
- */
2953
- emitBuiltinSync(code, id, options = {}) {
2954
- if (!this.builtinsPath) throw new Error(`The builtins path is not set. Cannot emit builtin file with id "${id}".`);
2955
- if (!(0, _stryke_type_checks_is_set_string.isSetString)(id)) throw new Error(`The builtin id must be a non-empty string. Received: ${String(id)}`);
2956
- return this.emitSync(code, (0, _stryke_path_append.appendPath)(id, this.builtinsPath), (0, defu.default)(options, { meta: {
2957
- type: "builtin",
2958
- id
2959
- } }));
2960
- }
2961
- /**
2962
- * Resolves a builtin virtual file and writes it to the VFS if it does not already exist
2963
- *
2964
- * @param code - The source code of the builtin file
2965
- * @param id - The unique identifier of the builtin file
2966
- * @param options - Optional write file options
2967
- */
2968
- async emitInfrastructure(code, id, options = {}) {
2969
- if (!this.infrastructurePath) throw new Error(`The infrastructure path is not set. Cannot emit infrastructure file with id "${id}".`);
2970
- if (!(0, _stryke_type_checks_is_set_string.isSetString)(id)) throw new Error(`The infrastructure id must be a non-empty string. Received: ${String(id)}`);
2971
- return this.emit(code, (0, _stryke_path_append.appendPath)(id, this.infrastructurePath), (0, defu.default)(options, { meta: {
2972
- type: "infrastructure",
2973
- id
2974
- } }));
2975
- }
2976
- /**
2977
- * Synchronously resolves an infrastructure virtual file and writes it to the VFS if it does not already exist
2978
- *
2979
- * @param code - The source code of the infrastructure file
2980
- * @param id - The unique identifier of the infrastructure file
2981
- * @param options - Optional write file options
2982
- */
2983
- emitInfrastructureSync(code, id, options = {}) {
2984
- if (!this.infrastructurePath) throw new Error(`The infrastructure path is not set. Cannot emit infrastructure file with id "${id}".`);
2985
- if (!(0, _stryke_type_checks_is_set_string.isSetString)(id)) throw new Error(`The infrastructure id must be a non-empty string. Received: ${String(id)}`);
2986
- return this.emitSync(code, (0, _stryke_path_append.appendPath)(id, this.infrastructurePath), (0, defu.default)(options, { meta: {
2987
- type: "infrastructure",
2988
- id
2989
- } }));
2990
- }
2991
- /**
2992
- * Generates a checksum representing the current context state
2993
- *
2994
- * @param root - The root directory of the project to generate the checksum for
2995
- * @returns A promise that resolves to a string representing the checksum
2996
- */
2997
- async generateChecksum(root = this.config.root) {
2998
- this.#checksum = await (0, _stryke_hash_node.hashDirectory)(root, { ignore: [
2999
- "node_modules",
3000
- ".git",
3001
- ".nx",
3002
- ".cache",
3003
- "tmp",
3004
- "dist"
3005
- ] });
3006
- return this.#checksum;
3007
- }
3008
- /**
3009
- * Initialize the context with the provided configuration options
3010
- */
3011
- async setup() {
3012
- this.resolvedConfig = (0, _powerlines_core_plugin_utils.mergeConfig)({
3013
- inlineConfig: this.config.inlineConfig ?? {},
3014
- userConfig: this.config.userConfig ?? {},
3015
- pluginConfig: this.config.pluginConfig ?? {}
3016
- }, this.options, this.config.inlineConfig ? getConfigProps(this.config.inlineConfig, this.options.root, this.options.cwd) : {}, this.config.userConfig ? getConfigProps(this.config.userConfig, this.options.root, this.options.cwd) : {}, this.config.pluginConfig ? getConfigProps(this.config.pluginConfig, this.options.root, this.options.cwd) : {}, {
3017
- name: this.projectJson?.name || this.packageJson?.name,
3018
- version: this.packageJson?.version,
3019
- description: this.packageJson?.description
3020
- }, {
3021
- projectType: "application",
3022
- platform: "neutral",
3023
- logLevel: "info",
3024
- preview: false,
3025
- environments: {},
3026
- resolve: {}
3027
- });
3028
- await this.innerSetup();
3029
- }
3030
- /**
3031
- * The resolved configuration for this context
3032
- */
3033
- resolvedConfig = {};
3034
- /**
3035
- * Creates a clone of the current context with the same configuration and workspace settings. This can be useful for running multiple builds in parallel or for creating isolated contexts for different parts of the build process.
3036
- *
3037
- * @remarks
3038
- * The cloned context will have the same configuration and workspace settings as the original context, but will have a different build ID, release ID, and timestamp. The virtual file system and caches will also be separate between the original and cloned contexts.
3039
- *
3040
- * @returns The cloned context.
3041
- */
3042
- copyTo(context) {
3043
- for (const [key, value] of Object.entries(this)) if (!SKIP_CLONING_PROPS.includes(key)) if ((0, _stryke_type_checks_is_object.isObject)(value) || Array.isArray(value)) context[key] = (0, _stryke_helpers_deep_clone.deepClone)(value);
3044
- else context[key] = value;
3045
- context.dependencies = (0, _stryke_helpers_deep_clone.deepClone)(this.dependencies);
3046
- context.devDependencies = (0, _stryke_helpers_deep_clone.deepClone)(this.devDependencies);
3047
- context.persistedMeta = this.persistedMeta ? (0, _stryke_helpers_deep_clone.deepClone)(this.persistedMeta) : void 0;
3048
- context.packageJson = (0, _stryke_helpers_deep_clone.deepClone)(this.packageJson);
3049
- context.projectJson = this.projectJson ? (0, _stryke_helpers_deep_clone.deepClone)(this.projectJson) : void 0;
3050
- context.tsconfig ??= (0, _stryke_helpers_deep_clone.deepClone)(this.tsconfig);
3051
- context.resolver ??= this.resolver;
3052
- context.fs ??= this.#fs;
3053
- context.$$internal = this.$$internal;
3054
- return context;
3055
- }
3056
- /**
3057
- * Initialize the context with the provided configuration options
3058
- *
3059
- * @remarks
3060
- * This method will set up the resolver and load the user configuration file based on the provided options. It is called during the construction of the context and can also be called when cloning the context to ensure that the new context has the same configuration and resolver setup.
3061
- *
3062
- * @param options - The configuration options to initialize the context with
3063
- */
3064
- async init(options = {}) {
3065
- await super.init(options);
3066
- this.options.configIndex = options.configIndex ?? this.options.configIndex ?? 0;
3067
- const projectJsonPath = (0, _stryke_path_join.joinPaths)(this.options.root, "project.json");
3068
- if ((0, _stryke_fs_exists.existsSync)(projectJsonPath)) this.projectJson = await (0, _stryke_fs_json.readJsonFile)(projectJsonPath);
3069
- const packageJsonPath = (0, _stryke_path_join.joinPaths)(this.options.root, "package.json");
3070
- if ((0, _stryke_fs_exists.existsSync)(packageJsonPath)) {
3071
- this.packageJson = await (0, _stryke_fs_json.readJsonFile)(packageJsonPath);
3072
- this.options.organization ??= (0, _stryke_type_checks_is_set_object.isSetObject)(this.packageJson?.author) ? (0, _stryke_string_format_kebab_case.kebabCase)(this.packageJson?.author?.name) : (0, _stryke_string_format_kebab_case.kebabCase)(this.packageJson?.author);
3073
- }
3074
- this.#checksum = await this.generateChecksum(this.options.root);
3075
- const userConfig = this.configFile.config ? Array.isArray(this.configFile.config) && this.configFile.config.length > this.options.configIndex ? this.configFile.config[this.options.configIndex] : this.configFile.config : {};
3076
- this.resolvedConfig = {
3077
- ...this.options,
3078
- ...userConfig,
3079
- userConfig
3080
- };
3081
- }
3082
- /**
3083
- * Initialize the context with the provided configuration options
3084
- */
3085
- async innerSetup() {
3086
- this.resolvedConfig.compatibilityDate = (0, compatx.resolveCompatibilityDates)(this.config.inlineConfig.compatibilityDate ?? this.config.userConfig.compatibilityDate ?? this.config.pluginConfig.compatibilityDate, "latest");
3087
- this.resolvedConfig.output = (0, defu.default)(this.resolvedConfig.output ?? {}, {
3088
- path: (0, _stryke_path_append.appendPath)((0, _stryke_path_join.joinPaths)(this.config.root, "dist"), this.config.cwd),
3089
- copy: { assets: [
3090
- { glob: "LICENSE" },
3091
- {
3092
- input: this.config.root,
3093
- glob: "*.md"
3094
- },
3095
- {
3096
- input: this.config.root,
3097
- glob: "package.json"
3098
- }
3099
- ] },
3100
- artifactsPath: `.${this.config.framework ?? "powerlines"}`,
3101
- dts: true,
3102
- types: (0, _stryke_path_join.joinPaths)(this.config.root, `${this.config.framework ?? "powerlines"}.d.ts`)
3103
- });
3104
- this.logger = {
3105
- log: this.createLog(this.config.name),
3106
- level: (0, _stryke_type_checks_is_null.isNull)(this.logLevel) ? "silent" : this.logLevel
3107
- };
3108
- if (this.config.output?.format) this.config.output.format = (0, _stryke_helpers_get_unique.getUnique)((0, _stryke_convert_to_array.toArray)(this.config.output?.format));
3109
- this.config.plugins = (this.config.plugins ?? []).filter(Boolean).reduce((ret, plugin) => {
3110
- if ((0, _powerlines_core_plugin_utils.isPlugin)(plugin) && (0, _powerlines_core_plugin_utils.isDuplicate)(plugin, ret.filter((p) => (0, _powerlines_core_plugin_utils.isPlugin)(p)))) return ret;
3111
- ret.push(plugin);
3112
- return ret;
3113
- }, []);
3114
- this.config.input = (0, _powerlines_core_lib_entry.getUniqueInputs)(this.config.input);
3115
- if (this.config.name?.startsWith("@") && this.config.name.split("/").filter(Boolean).length > 1) this.config.name = this.config.name.split("/").filter(Boolean)[1];
3116
- this.config.title ??= (0, _stryke_string_format_title_case.titleCase)(this.config.name);
3117
- if (this.config.userConfig.resolve?.external) this.config.userConfig.resolve.external = (0, _stryke_helpers_get_unique.getUnique)(this.config.userConfig.resolve.external);
3118
- if (this.config.userConfig.resolve?.noExternal) this.config.userConfig.resolve.noExternal = (0, _stryke_helpers_get_unique.getUnique)(this.config.userConfig.resolve.noExternal);
3119
- if (this.config.resolve.external) this.config.resolve.external = (0, _stryke_helpers_get_unique.getUnique)(this.config.resolve.external);
3120
- if (this.config.resolve.noExternal) this.config.resolve.noExternal = (0, _stryke_helpers_get_unique.getUnique)(this.config.resolve.noExternal);
3121
- this.config.output.format = (0, _stryke_helpers_get_unique.getUnique)((0, _stryke_convert_to_array.toArray)(this.config.output?.format ?? (this.config.projectType === "library" ? ["cjs", "esm"] : ["esm"])));
3122
- if (this.config.output.dts !== false && !this.config.output.types) this.config.output.types = `${this.config.root ? `${this.config.root}/` : ""}${this.config.framework ?? "powerlines"}.d.ts`;
3123
- if (this.config.root && this.config.output.copy !== false) this.config.output.copy = {
3124
- path: (0, _stryke_path_join.joinPaths)(this.config.cwd, "dist", (0, _stryke_path_replace.replacePath)(this.config.root, this.config.cwd)),
3125
- ...this.config.output.copy || {}
3126
- };
3127
- if (this.config.output.copy && this.config.output.copy.path && this.config.output.copy.assets && Array.isArray(this.config.output.copy.assets)) this.config.output.copy.assets = (0, _stryke_helpers_get_unique.getUniqueBy)(this.config.output.copy.assets.map((asset) => {
3128
- return {
3129
- glob: (0, _stryke_type_checks_is_set_object.isSetObject)(asset) ? asset.glob : asset,
3130
- input: (0, _stryke_type_checks_is_string.isString)(asset) || !asset.input || asset.input === "." || asset.input === "/" || asset.input === "./" ? this.options.cwd : (0, _stryke_path_is_parent_path.isParentPath)(asset.input, this.config.cwd) || asset.input === this.config.cwd ? asset.input : (0, _stryke_path_append.appendPath)(asset.input, this.config.cwd),
3131
- output: (0, _stryke_type_checks_is_set_object.isSetObject)(asset) && asset.output ? (0, _stryke_path_is_parent_path.isParentPath)(asset.output, this.config.cwd) ? asset.output : (0, _stryke_path_append.appendPath)((0, _stryke_path_join.joinPaths)(this.config.output.copy.path, (0, _stryke_path_replace.replacePath)((0, _stryke_path_replace.replacePath)(asset.output, (0, _stryke_path_replace.replacePath)(this.config.output.copy.path, this.config.cwd)), this.config.output.copy.path)), this.config.cwd) : (0, _stryke_path_append.appendPath)(this.config.output.copy.path, this.config.cwd),
3132
- ignore: (0, _stryke_type_checks_is_set_object.isSetObject)(asset) && asset.ignore ? (0, _stryke_convert_to_array.toArray)(asset.ignore) : void 0
3133
- };
3134
- }), (a) => `${a.input}-${a.glob}-${a.output}`);
3135
- this.config.plugins = (this.config.plugins ?? []).filter(Boolean).reduce((ret, plugin) => {
3136
- if ((0, _powerlines_core_plugin_utils.isPlugin)(plugin) && (0, _powerlines_core_plugin_utils.isDuplicate)(plugin, ret.filter((p) => (0, _powerlines_core_plugin_utils.isPlugin)(p)))) return ret;
3137
- ret.push(plugin);
3138
- return ret;
3139
- }, []);
3140
- if (this.config.output.types) if ((0, _stryke_type_checks_is_set_string.isSetString)(this.config.output.types)) this.config.output.types = (0, _powerlines_core_plugin_utils.replacePathTokens)(this, this.config.output.types);
3141
- else this.config.output.types = (0, _stryke_path_join.joinPaths)(this.config.root, `${this.config.framework ?? "powerlines"}.d.ts`);
3142
- if (!this.config.userConfig?.logLevel && !this.config.inlineConfig?.logLevel) if (this.config.mode === "development") this.config.logLevel = "debug";
3143
- else this.config.logLevel = "info";
3144
- if (!this.config.userConfig?.output?.sourceMap && !this.config.inlineConfig?.output?.sourceMap) if (this.config.mode === "development") this.config.output.sourceMap = true;
3145
- else this.config.output.sourceMap = false;
3146
- if (!this.config.userConfig?.output?.minify && !this.config.inlineConfig?.output?.minify) if (this.config.mode === "production") this.config.output.minify = true;
3147
- else this.config.output.minify = false;
3148
- if (!this.config.userConfig?.tsconfig && !this.config.inlineConfig?.tsconfig) this.config.tsconfig = getTsconfigFilePath(this.options.cwd, this.options.root);
3149
- else if (this.config.tsconfig) this.config.tsconfig = (0, _stryke_path_replace.replacePath)((0, _powerlines_core_plugin_utils.replacePathTokens)(this, this.config.tsconfig), this.config.cwd);
3150
- if (this.config.output.copy && this.config.output.copy.assets) this.config.output.copy.assets = this.config.output.copy.assets.map((asset) => ({
3151
- ...asset,
3152
- glob: (0, _powerlines_core_plugin_utils.replacePathTokens)(this, asset.glob),
3153
- ignore: asset.ignore ? asset.ignore.map((ignore) => (0, _powerlines_core_plugin_utils.replacePathTokens)(this, ignore)) : void 0,
3154
- input: (0, _powerlines_core_plugin_utils.replacePathTokens)(this, asset.input),
3155
- output: (0, _powerlines_core_plugin_utils.replacePathTokens)(this, asset.output)
3156
- }));
3157
- if ((0, _stryke_type_checks_is_set_string.isSetString)(this.config.output?.storage) && this.config.output.storage === "virtual" || (0, _stryke_type_checks_is_set_object.isSetObject)(this.config.output?.storage) && Object.values(this.config.output.storage).every((adapter) => adapter.preset === "virtual")) this.config.output.overwrite = true;
3158
- this.#fs ??= await VirtualFileSystem.create(this);
3159
- }
3160
- };
3161
-
3162
- //#endregion
3163
- //#region src/_internal/helpers/hooks.ts
3164
- const mergeResultObjects = (0, defu.createDefu)((obj, key, value) => {
3165
- if ((0, _stryke_type_checks_is_string.isString)(obj[key]) && (0, _stryke_type_checks_is_string.isString)(value)) {
3166
- obj[key] = `${obj[key] || ""}\n${value || ""}`.trim();
3167
- return true;
3168
- }
3169
- return false;
3170
- });
3171
- /**
3172
- * Merges the current hook result with the previous results based on their types.
3173
- *
3174
- * @param currentResult - The current hook result to merge with the previous results.
3175
- * @param previousResults - The previous hook results to merge with the current result.
3176
- * @returns The merged result.
3177
- */
3178
- function mergeResults(currentResult, previousResults) {
3179
- if (!previousResults || previousResults.length === 0) return [currentResult];
3180
- if ((0, _stryke_type_checks_is_set_string.isSetString)(currentResult)) previousResults = [`${(0, _stryke_type_checks_is_set_string.isSetString)(previousResults[0]) ? previousResults[0] || "" : ""}\n${(0, _stryke_type_checks_is_set_string.isSetString)(previousResults[0]) ? currentResult.replace(previousResults[0], "") : currentResult}`.trim()];
3181
- else if ((0, _stryke_type_checks_is_object.isObject)(currentResult)) previousResults = [mergeResultObjects(currentResult, previousResults[0] ?? {})];
3182
- return previousResults;
3183
- }
3184
- /**
3185
- * Merges multiple hook results together, with special handling for string values and object values.
3186
- *
3187
- * @param currentResult - The current hook result to merge with the previous results.
3188
- * @param previousResults - The previous hook results to merge with the current result.
3189
- * @returns The merged result.
3190
- */
3191
- function mergeConfigs(currentResult, previousResults) {
3192
- if ((0, _stryke_type_checks_is_string.isString)(currentResult)) previousResults = `${(0, _stryke_type_checks_is_string.isString)(previousResults) ? previousResults || "" : ""}\n${currentResult || ""}`.trim();
3193
- else if ((0, _stryke_type_checks_is_object.isObject)(currentResult)) previousResults = (0, _powerlines_core_plugin_utils.mergeConfig)(currentResult, previousResults ?? {});
3194
- return previousResults;
3195
- }
3196
- /**
3197
- * Calls a hook with the given context, options, and arguments.
3198
- *
3199
- * @param context - The context to use when calling the hook.
3200
- * @param key - The hook to call.
3201
- * @param options - Options for calling the hook.
3202
- * @param args - Arguments to pass to the hook.
3203
- * @returns The return value of the hook.
3204
- */
3205
- async function callHook(context, key, options, ...args) {
3206
- const hooks = context.selectHooks(key, options);
3207
- if (hooks.length > 0) {
3208
- context.debug(` 🧩 Calling ${hooks.length} ${chalk.default.bold.cyanBright(`${key}${options?.order ? ` (${options.order})` : ""}`)} plugin hook${hooks.length > 1 ? "s" : ""}:\n${hooks.map((hook, index) => ` ${index + 1}. ${(0, _powerlines_core_lib_logger.colorText)(hook.plugin.name)}`).join("\n")}`);
3209
- const invokeHook = async (hook, hookArgs) => {
3210
- return Reflect.apply(hook.handler, hook.context, hookArgs);
3211
- };
3212
- let results = [];
3213
- if (options?.sequential === false) results = await Promise.all(hooks.map(async (hook) => {
3214
- if (!(0, _stryke_type_checks_is_function.isFunction)(hook.handler)) throw new Error(`Plugin hook handler for hook "${key}" is not a function.`);
3215
- return invokeHook(hook, [...args]);
3216
- }));
3217
- else for (const hook of hooks) {
3218
- if (!(0, _stryke_type_checks_is_function.isFunction)(hook.handler)) throw new Error(`Plugin hook handler for hook "${key}" is not a function.`);
3219
- if (options?.result === "first" || options?.asNextParam === false) {
3220
- results.push(await Promise.resolve(invokeHook(hook, [...args])));
3221
- if (options?.result === "first" && (0, _stryke_type_checks_is_set.isSet)(results[results.length - 1])) break;
3222
- } else {
3223
- const sequenceArgs = [...args];
3224
- if (results.length > 0 && sequenceArgs.length > 0) sequenceArgs[0] = (0, _stryke_type_checks_is_function.isFunction)(options.asNextParam) ? await Promise.resolve(options.asNextParam(results[0])) : results[0];
3225
- const result = await Promise.resolve(invokeHook(hook, [...sequenceArgs]));
3226
- if (result) if (options.result === "last") results = [result];
3227
- else if (options.result === "merge" && options.merge) results = [results.length > 0 && results[0] ? await Promise.resolve(options.merge(result, results[0])) : result];
3228
- else results = mergeResults(result, results);
3229
- }
3230
- }
3231
- const definedResults = results.filter((result) => (0, _stryke_type_checks_is_set.isSet)(result));
3232
- if (definedResults.length > 0) {
3233
- let mergedResult = void 0;
3234
- for (const result of definedResults) mergedResult = (0, defu.defu)(result, mergedResult ?? {});
3235
- return mergedResult;
3236
- }
3237
- }
3238
- }
3239
- function extractHooks(context, hooks, plugin, key, parentKey) {
3240
- const combinedKey = parentKey ? `${parentKey}:${key}` : key;
3241
- const pluginField = (0, _stryke_helpers_get_field.getField)(plugin, combinedKey.replace(/:/g, "."));
3242
- if ((0, _powerlines_core_plugin_utils.isPluginHookField)(combinedKey) && (0, _powerlines_core_plugin_utils.isPluginHook)(pluginField)) {
3243
- const pluginHook = pluginField;
3244
- if (!(0, _powerlines_core_plugin_utils.isPluginHook)(pluginHook)) return hooks;
3245
- hooks[combinedKey] ??= {
3246
- preEnforced: [],
3247
- preOrdered: [],
3248
- normal: [],
3249
- postEnforced: [],
3250
- postOrdered: []
3251
- };
3252
- if (plugin.enforce) {
3253
- const hookListOrder = `${plugin.enforce}Enforced`;
3254
- hooks[combinedKey][hookListOrder] ??= [];
3255
- hooks[combinedKey][hookListOrder] = (0, _powerlines_core_plugin_utils.addPluginHook)(context, plugin, pluginHook, hooks[combinedKey][hookListOrder]);
3256
- return hooks;
3257
- }
3258
- if ((0, _stryke_type_checks_is_function.isFunction)(pluginHook) || !pluginHook.order) {
3259
- hooks[combinedKey].normal ??= [];
3260
- hooks[combinedKey].normal = (0, _powerlines_core_plugin_utils.addPluginHook)(context, plugin, pluginHook, hooks[combinedKey].normal);
3261
- return hooks;
3262
- }
3263
- const hookListOrder = `${pluginHook.order}Ordered`;
3264
- hooks[combinedKey][hookListOrder] ??= [];
3265
- hooks[combinedKey][hookListOrder] = (0, _powerlines_core_plugin_utils.addPluginHook)(context, plugin, pluginHook, hooks[combinedKey][hookListOrder]);
3266
- return hooks;
3267
- } else if ((0, _stryke_type_checks_is_set_object.isSetObject)(pluginField)) return Object.keys(pluginField).map((pluginKey) => extractHooks(context, hooks, plugin, pluginKey, combinedKey)).reduce((ret, current) => {
3268
- Object.keys(current).forEach((key) => {
3269
- ret[key] ??= {
3270
- preEnforced: [],
3271
- preOrdered: [],
3272
- normal: [],
3273
- postEnforced: [],
3274
- postOrdered: []
3275
- };
3276
- [
3277
- "preEnforced",
3278
- "preOrdered",
3279
- "normal",
3280
- "postEnforced",
3281
- "postOrdered"
3282
- ].forEach((order) => {
3283
- if (current[key]?.[order]) {
3284
- ret[key][order] ??= [];
3285
- ret[key][order] = ret[key][order].concat(current[key][order]);
3286
- }
3287
- });
3288
- });
3289
- return ret;
3290
- }, hooks);
3291
- return hooks;
3292
- }
3293
-
3294
- //#endregion
3295
- //#region src/context/plugin-context.ts
3296
- /**
3297
- * Create a Proxy-based PluginContext
3298
- *
3299
- * @param plugin - The plugin instance
3300
- * @param environment - The environment context
3301
- * @returns The proxied plugin context
3302
- */
3303
- function createPluginContext(plugin, environment) {
3304
- const normalizeMessage = (message) => {
3305
- return (0, _stryke_type_checks_is_string.isString)(message) ? message : message.message;
3306
- };
3307
- const log = environment.extendLog(plugin.name.replaceAll(":", " - "));
3308
- const callHookFn = async (hook, options, ...args) => {
3309
- return environment.$$internal.api.callHook(hook, {
3310
- sequential: true,
3311
- result: "merge",
3312
- ...options,
3313
- environment
3314
- }, ...args);
3315
- };
3316
- const meta = {};
3317
- return new Proxy({}, {
3318
- get(_, prop) {
3319
- if (prop === "$$internal") return {
3320
- ...environment.$$internal,
3321
- environment,
3322
- callHook: callHookFn,
3323
- meta
3324
- };
3325
- if (prop === "log" || prop === "logger") return log;
3326
- if (prop === "fatal") return (message) => {
3327
- log(_storm_software_config_tools_types.LogLevelLabel.FATAL, normalizeMessage(message));
3328
- };
3329
- if (prop === "error") return (message) => {
3330
- log(_storm_software_config_tools_types.LogLevelLabel.ERROR, normalizeMessage(message));
3331
- };
3332
- if (prop === "warn") return (message) => {
3333
- log(_storm_software_config_tools_types.LogLevelLabel.WARN, normalizeMessage(message));
3334
- };
3335
- if (prop === "info") return (message) => {
3336
- log(_storm_software_config_tools_types.LogLevelLabel.INFO, normalizeMessage(message));
3337
- };
3338
- if (prop === "debug") return (message) => {
3339
- log(_storm_software_config_tools_types.LogLevelLabel.DEBUG, normalizeMessage(message));
3340
- };
3341
- if (prop === "trace") return (message) => {
3342
- log(_storm_software_config_tools_types.LogLevelLabel.TRACE, normalizeMessage(message));
3343
- };
3344
- return environment[prop];
3345
- },
3346
- set(_, prop, value) {
3347
- if ([
3348
- "$$internal",
3349
- "environment",
3350
- "config",
3351
- "log",
3352
- "logger",
3353
- "error",
3354
- "warn",
3355
- "plugins",
3356
- "hooks",
3357
- "addPlugin",
3358
- "selectHooks"
3359
- ].includes(prop)) {
3360
- log(_storm_software_config_tools_types.LogLevelLabel.WARN, `Cannot set read-only property "${String(prop)}"`);
3361
- return false;
3362
- }
3363
- environment[prop] = value;
3364
- return true;
3365
- }
3366
- });
3367
- }
3368
-
3369
- //#endregion
3370
- //#region src/context/environment-context.ts
3371
- var PowerlinesEnvironmentContext = class PowerlinesEnvironmentContext extends PowerlinesContext {
3372
- /**
3373
- * The hooks registered by plugins in this environment
3374
- */
3375
- #hooks = {};
3376
- /**
3377
- * Create a new context from the config.
3378
- *
3379
- * @param options - The resolved execution options.
3380
- * @param config - The user configuration options.
3381
- * @returns A promise that resolves to the new context.
3382
- */
3383
- static async fromConfig(options, config, environment) {
3384
- const context = new PowerlinesEnvironmentContext(options, config, environment);
3385
- await context.setup();
3386
- const powerlinesPath = await (0, _stryke_fs_resolve.resolvePackage)("powerlines");
3387
- if (!powerlinesPath) throw new Error("Could not resolve `powerlines` package location.");
3388
- context.powerlinesPath = powerlinesPath;
3389
- return context;
3390
- }
3391
- /**
3392
- * The resolved environment configuration
3393
- */
3394
- environment;
3395
- /**
3396
- * The list of plugins applied to this environment
3397
- */
3398
- plugins = [];
3399
- /**
3400
- * Create a new logger instance
3401
- *
3402
- * @param name - The name to use for the logger instance
3403
- * @returns A logger function
3404
- */
3405
- createLog(name = null) {
3406
- return (0, _powerlines_core_lib_logger.createLog)(name, {
3407
- ...this.config,
3408
- logLevel: (0, _stryke_type_checks_is_null.isNull)(this.config.logLevel) ? "silent" : this.config.logLevel,
3409
- environment: this.environment?.name
3410
- });
3411
- }
3412
- /**
3413
- * The hooks registered by plugins in this environment
3414
- */
3415
- get hooks() {
3416
- return this.#hooks;
3417
- }
3418
- /**
3419
- * Creates a clone of the current context with the same configuration and workspace settings. This can be useful for running multiple builds in parallel or for creating isolated contexts for different parts of the build process.
3420
- *
3421
- * @remarks
3422
- * The cloned context will have the same configuration and workspace settings as the original context, but will have a different build ID, release ID, and timestamp. The virtual file system and caches will also be separate between the original and cloned contexts.
3423
- *
3424
- * @returns A promise that resolves to the cloned context.
3425
- */
3426
- async clone() {
3427
- const context = await PowerlinesEnvironmentContext.fromConfig((0, _stryke_helpers_deep_clone.deepClone)(this.options), (0, _stryke_helpers_deep_clone.deepClone)(this.config), (0, _stryke_helpers_deep_clone.deepClone)(this.environment));
3428
- return this.copyTo(context);
3429
- }
3430
- /**
3431
- * Initialize the context with the provided configuration options
3432
- */
3433
- async setup() {
3434
- this.resolvedConfig = (0, _powerlines_core_plugin_utils.mergeConfig)({
3435
- name: this.config.name,
3436
- title: this.config.title
3437
- }, getConfigProps(this.environment, this.options.root, this.options.cwd), this.config);
3438
- await this.innerSetup();
3439
- }
3440
- async addPlugin(plugin) {
3441
- let resolvedPlugin = plugin;
3442
- if ((0, _stryke_type_checks_is_function.isFunction)(plugin.applyToEnvironment)) {
3443
- const result = await Promise.resolve(plugin.applyToEnvironment(this.environment));
3444
- if (!result || (0, _stryke_type_checks_is_object.isObject)(result) && Object.keys(result).length === 0) return;
3445
- if ((0, _powerlines_core_plugin_utils.isPluginConfig)(result)) return this.$$internal.addPlugin(result);
3446
- resolvedPlugin = (0, _powerlines_core_plugin_utils.isPlugin)(result) ? result : plugin;
3447
- }
3448
- const context = createPluginContext(resolvedPlugin, this);
3449
- this.plugins.push({
3450
- plugin: resolvedPlugin,
3451
- context
3452
- });
3453
- this.#hooks = Object.entries(Object.keys(resolvedPlugin).filter((key) => !_powerlines_core_constants.PLUGIN_NON_HOOK_FIELDS.includes(key)).reduce((ret, key) => extractHooks(context, ret, resolvedPlugin, key), this.hooks)).reduce((ret, [key, value]) => {
3454
- if ((0, _stryke_type_checks_is_set_object.isSetObject)(value)) Object.entries(value).forEach(([type, list]) => {
3455
- ret[key] ??= {};
3456
- ret[key][type] = (0, _powerlines_core_plugin_utils.dedupeHooklist)(list);
3457
- });
3458
- return ret;
3459
- }, {});
3460
- }
3461
- /**
3462
- * Retrieves the hook handlers for a specific hook name
3463
- */
3464
- selectHooks(key, options) {
3465
- const result = [];
3466
- if ((0, _powerlines_core_plugin_utils.isPluginHookField)(key) && this.hooks[key]) {
3467
- if (this.hooks[key]) if (options?.order) {
3468
- const mapHooksToResult = (hooksList) => hooksList.map((hook) => {
3469
- const plugin = this.plugins.find((p) => p.plugin.name === hook.plugin.name);
3470
- if (!plugin) throw new Error(`Could not find plugin context for plugin "${hook.plugin.name}".`);
3471
- return {
3472
- handler: hook.handler,
3473
- plugin: hook.plugin,
3474
- context: plugin.context
3475
- };
3476
- });
3477
- if (options?.order === "pre") {
3478
- result.push(...mapHooksToResult(this.hooks[key].preOrdered ?? []));
3479
- result.push(...mapHooksToResult(this.hooks[key].preEnforced ?? []));
3480
- } else if (options?.order === "post") {
3481
- result.push(...mapHooksToResult(this.hooks[key].postOrdered ?? []));
3482
- result.push(...mapHooksToResult(this.hooks[key].postEnforced ?? []));
3483
- } else result.push(...mapHooksToResult(this.hooks[key].normal ?? []));
3484
- } else {
3485
- result.push(...this.selectHooks(key, { order: "pre" }));
3486
- result.push(...this.selectHooks(key, { order: "normal" }));
3487
- result.push(...this.selectHooks(key, { order: "post" }));
3488
- }
3489
- }
3490
- return result;
3491
- }
3492
- constructor(options, config, environment) {
3493
- super(options);
3494
- this.resolvedConfig = config;
3495
- this.environment = environment;
3496
- }
3497
- /**
3498
- * Creates a clone of the current context with the same configuration and workspace settings. This can be useful for running multiple builds in parallel or for creating isolated contexts for different parts of the build process.
3499
- *
3500
- * @remarks
3501
- * The cloned context will have the same configuration and workspace settings as the original context, but will have a different build ID, release ID, and timestamp. The virtual file system and caches will also be separate between the original and cloned contexts.
3502
- *
3503
- * @returns The cloned context.
3504
- */
3505
- copyTo(context) {
3506
- context.plugins = this.plugins;
3507
- return super.copyTo(context);
3508
- }
3509
- };
3510
-
3511
- //#endregion
3512
- //#region src/context/execution-context.ts
3513
- var PowerlinesExecutionContext = class PowerlinesExecutionContext extends PowerlinesContext {
3514
- #environments = {};
3515
- #plugins = [];
3516
- /**
3517
- * Create a new Storm context from the workspace root and user config.
3518
- *
3519
- * @param options - The options for resolving the context.
3520
- * @returns A promise that resolves to the new context.
3521
- */
3522
- static async fromOptions(options) {
3523
- const context = new PowerlinesExecutionContext(options);
3524
- await context.init(options);
3525
- const powerlinesPath = await (0, _stryke_fs_resolve.resolvePackage)("powerlines");
3526
- if (!powerlinesPath) throw new Error("Could not resolve `powerlines` package location.");
3527
- context.powerlinesPath = powerlinesPath;
3528
- return context;
3529
- }
3530
- /**
3531
- * Create a new Storm context from the workspace root and user config.
3532
- *
3533
- * @param options - The options for resolving the context.
3534
- * @returns A promise that resolves to the new context.
3535
- */
3536
- static async fromConfig(options, config) {
3537
- const context = new PowerlinesExecutionContext(options);
3538
- await context.init(options);
3539
- context.config.inlineConfig = config;
3540
- if (context.config.inlineConfig.command === "new") {
3541
- const workspacePackageJsonPath = (0, _stryke_path_join.joinPaths)(context.config.cwd, "package.json");
3542
- if (!(0, _stryke_fs_exists.existsSync)(workspacePackageJsonPath)) throw new Error(`The workspace package.json file could not be found at ${workspacePackageJsonPath}`);
3543
- context.packageJson = await (0, _stryke_fs_json.readJsonFile)(workspacePackageJsonPath);
3544
- }
3545
- await context.setup();
3546
- const powerlinesPath = await (0, _stryke_fs_resolve.resolvePackage)("powerlines");
3547
- if (!powerlinesPath) throw new Error("Could not resolve `powerlines` package location.");
3548
- context.powerlinesPath = powerlinesPath;
3549
- return context;
3550
- }
3551
- /**
3552
- * Internal context fields and methods
3553
- *
3554
- * @danger
3555
- * This field is for internal use only and should not be accessed or modified directly. It is unstable and can be changed at anytime.
3556
- *
3557
- * @internal
3558
- */
3559
- get $$internal() {
3560
- return super.$$internal;
3561
- }
3562
- /**
3563
- * Internal context fields and methods
3564
- *
3565
- * @danger
3566
- * This field is for internal use only and should not be accessed or modified directly. It is unstable and can be changed at anytime.
3567
- *
3568
- * @internal
3569
- */
3570
- set $$internal(value) {
3571
- super.$$internal = value;
3572
- for (const environment of Object.values(this.environments)) environment.$$internal = super.$$internal;
3573
- }
3574
- /**
3575
- * A record of all environments by name
3576
- */
3577
- get environments() {
3578
- return this.#environments;
3579
- }
3580
- get plugins() {
3581
- return this.#plugins;
3582
- }
3583
- /**
3584
- * Creates a new instance.
3585
- *
3586
- * @param options - The options to use for creating the context, including the resolved configuration and workspace settings.
3587
- */
3588
- constructor(options) {
3589
- super(options);
3590
- }
3591
- /**
3592
- * Creates a clone of the current context with the same configuration and workspace settings. This can be useful for running multiple builds in parallel or for creating isolated contexts for different parts of the build process.
3593
- *
3594
- * @remarks
3595
- * The cloned context will have the same configuration and workspace settings as the original context, but will have a different build ID, release ID, and timestamp. The virtual file system and caches will also be separate between the original and cloned contexts.
3596
- *
3597
- * @returns A promise that resolves to the cloned context.
3598
- */
3599
- async clone() {
3600
- const clone = await PowerlinesExecutionContext.fromOptions(this.options);
3601
- clone.config.userConfig = (0, _stryke_helpers_deep_clone.deepClone)(this.config.userConfig);
3602
- clone.config.inlineConfig = (0, _stryke_helpers_deep_clone.deepClone)(this.config.inlineConfig);
3603
- clone.config.pluginConfig = (0, _stryke_helpers_deep_clone.deepClone)(this.config.pluginConfig);
3604
- await clone.setup();
3605
- clone.$$internal = this.$$internal;
3606
- return this.copyTo(clone);
3607
- }
3608
- /**
3609
- * A function to copy the context and update the fields for a specific environment
3610
- *
3611
- * @param environment - The environment configuration to use.
3612
- * @returns A new context instance with the updated environment.
3613
- */
3614
- async in(environment) {
3615
- const context = this.copyTo(await PowerlinesEnvironmentContext.fromConfig((0, _stryke_helpers_deep_clone.deepClone)(this.options), (0, _stryke_helpers_deep_clone.deepClone)(this.config), (0, _stryke_helpers_deep_clone.deepClone)(environment)));
3616
- context.plugins = [];
3617
- for (const plugin of this.plugins) await context.addPlugin(plugin);
3618
- return context;
3619
- }
3620
- /**
3621
- * Update the context using a new inline configuration options
3622
- */
3623
- async setup() {
3624
- await super.setup();
3625
- await Promise.all((0, _stryke_convert_to_array.toArray)(this.config.environments && Object.keys(this.config.environments).length > 0 ? Object.keys(this.config.environments).map((name) => createEnvironment(name, this.config)) : createDefaultEnvironment(this.config)).map(async (env) => {
3626
- this.#environments[env.name] = await this.in(env);
3627
- }));
3628
- }
3629
- /**
3630
- * Add a plugin to the API context and all environments
3631
- *
3632
- * @param plugin - The plugin to add.
3633
- */
3634
- async addPlugin(plugin) {
3635
- this.plugins.push(plugin);
3636
- await Promise.all(Object.keys(this.environments).map(async (name) => {
3637
- await this.environments[name].addPlugin(plugin);
3638
- }));
3639
- }
3640
- /**
3641
- * Get an environment by name, or the default environment if no name is provided
3642
- *
3643
- * @param name - The name of the environment to retrieve.
3644
- * @returns The requested environment context.
3645
- */
3646
- async getEnvironment(name) {
3647
- let environment;
3648
- if (name) environment = this.environments[name];
3649
- if (Object.keys(this.environments).length === 1) {
3650
- environment = this.environments[Object.keys(this.environments)[0]];
3651
- this.debug(`Applying the only configured environment: ${chalk.default.bold.cyanBright(environment?.environment.name)}`);
3652
- }
3653
- if (!environment) {
3654
- if (name) throw new Error(`Environment "${name}" not found.`);
3655
- environment = await this.in(createDefaultEnvironment(this.config));
3656
- this.warn(`No environment specified, and no default environment found. Using a temporary default environment: ${chalk.default.bold.cyanBright(environment?.environment.name)}`);
3657
- }
3658
- return environment;
3659
- }
3660
- /**
3661
- * A safe version of `getEnvironment` that returns `undefined` if the environment is not found
3662
- *
3663
- * @param name - The name of the environment to retrieve.
3664
- * @returns The requested environment context or `undefined` if not found.
3665
- */
3666
- async getEnvironmentSafe(name) {
3667
- try {
3668
- return await this.getEnvironment(name);
3669
- } catch {
3670
- return;
3671
- }
3672
- }
3673
- /**
3674
- * A function to merge all configured environments into a single context.
3675
- *
3676
- * @remarks
3677
- * If only one environment is configured, that environment will be returned directly.
3678
- *
3679
- * @returns A promise that resolves to a merged/global environment context.
3680
- */
3681
- async toEnvironment() {
3682
- let environment;
3683
- if (Object.keys(this.environments).length > 1) {
3684
- environment = await this.in(createEnvironment(_powerlines_core_constants.GLOBAL_ENVIRONMENT, this.config));
3685
- this.debug(`Combined all ${Object.keys(this.environments).length} environments into a single global context.`);
3686
- } else environment = await this.getEnvironment();
3687
- return environment;
3688
- }
3689
- };
3690
-
3691
- //#endregion
3692
- //#region src/typescript/ts-morph.ts
3693
- var VirtualFileSystemHost = class extends ts_morph.InMemoryFileSystemHost {
3694
- #context;
3695
- constructor(context) {
3696
- super();
3697
- this.#context = context;
3698
- }
3699
- deleteSync(path) {
3700
- this.#context.fs.removeSync(path);
3701
- }
3702
- readDirSync(dirPath) {
3703
- if (!this.#context.fs.isDirectorySync(dirPath)) return [];
3704
- return this.#context.fs.listSync(dirPath).reduce((ret, entry) => {
3705
- const fullPath = this.#context.fs.resolveSync(entry);
3706
- if (fullPath) ret.push({
3707
- name: entry,
3708
- isDirectory: this.#context.fs.isDirectorySync(fullPath),
3709
- isFile: this.#context.fs.isFileSync(fullPath),
3710
- isSymlink: false
3711
- });
3712
- return ret;
3713
- }, []);
3714
- }
3715
- async readFile(filePath) {
3716
- if (!this.#context.fs.isFileSync(filePath)) return "";
3717
- return await this.#context.fs.read(filePath);
3718
- }
3719
- readFileSync(filePath) {
3720
- if (!this.#context.fs.isFileSync(filePath)) return "";
3721
- return this.#context.fs.readSync(filePath);
3722
- }
3723
- async writeFile(filePath, fileText) {
3724
- return this.#context.fs.write(filePath, fileText);
3725
- }
3726
- writeFileSync(filePath, fileText) {
3727
- this.#context.fs.writeSync(filePath, fileText);
3728
- }
3729
- async mkdir(dirPath) {
3730
- await this.#context.fs.mkdir(dirPath);
3731
- }
3732
- mkdirSync(dirPath) {
3733
- this.#context.fs.mkdirSync(dirPath);
3734
- }
3735
- async move(srcPath, destPath) {
3736
- await this.#context.fs.move(srcPath, destPath);
3737
- }
3738
- moveSync(srcPath, destPath) {
3739
- this.#context.fs.moveSync(srcPath, destPath);
3740
- }
3741
- async copy(srcPath, destPath) {
3742
- await this.#context.fs.copy(srcPath, destPath);
3743
- }
3744
- copySync(srcPath, destPath) {
3745
- this.#context.fs.copySync(srcPath, destPath);
3746
- }
3747
- async fileExists(filePath) {
3748
- return this.#context.fs.isFile(filePath);
3749
- }
3750
- fileExistsSync(filePath) {
3751
- return this.#context.fs.isFileSync(filePath);
3752
- }
3753
- async directoryExists(dirPath) {
3754
- return this.#context.fs.isDirectory(dirPath);
3755
- }
3756
- directoryExistsSync(dirPath) {
3757
- return this.#context.fs.isDirectorySync(dirPath);
3758
- }
3759
- realpathSync(path) {
3760
- return this.#context.fs.resolveSync(path) || path;
3761
- }
3762
- getCurrentDirectory() {
3763
- return this.#context.config.cwd;
3764
- }
3765
- async glob(patterns) {
3766
- return this.#context.fs.glob(patterns);
3767
- }
3768
- globSync(patterns) {
3769
- return this.#context.fs.globSync(patterns);
3770
- }
3771
- };
3772
- /**
3773
- * Create a ts-morph {@link Project} instance used for type reflection and module manipulation during processing
3774
- *
3775
- * @param context - The Powerlines context
3776
- * @returns A ts-morph {@link Project} instance
3777
- */
3778
- function createProgram(context, override) {
3779
- context.debug(`Creating ts-morph Project instance with configuration from: ${context.tsconfig.tsconfigFilePath}.`);
3780
- return new ts_morph.Project((0, defu.default)(override ?? {}, {
3781
- skipAddingFilesFromTsConfig: false,
3782
- tsConfigFilePath: context.tsconfig.tsconfigFilePath,
3783
- fileSystem: new VirtualFileSystemHost(context),
3784
- compilerOptions: (0, defu.default)(context.tsconfig.options ?? {}, { lib: ["lib.esnext.full.d.ts"] })
3785
- }));
3786
- }
3787
-
3788
- //#endregion
3789
- //#region src/_internal/helpers/generate-types.ts
3790
- /**
3791
- * Formats the generated TypeScript types source code.
3792
- *
3793
- * @param code - The generated TypeScript code.
3794
- * @returns The formatted TypeScript code.
3795
- */
3796
- function formatTypes(code = "") {
3797
- return code.replaceAll("#private;", "").replace(/__Ω/g, "");
3798
- }
3799
- async function extractModuleDeclarations(ctx, filePath, name, text) {
3800
- const imports = [];
3801
- const exports = [];
3802
- const comment = text.match(new RegExp(`\\/\\*\\*(?s:.)*?@module\\s+${ctx.context.config.framework}:${name}(?s:.)*?\\*\\/\\s+`))?.find((comment) => (0, _stryke_type_checks_is_set_string.isSetString)(comment?.trim()));
3803
- const sourceFile = new ts_morph.Project({ useInMemoryFileSystem: true }).createSourceFile("module.d.ts", text);
3804
- for (const ref of sourceFile.getTypeReferenceDirectives()) if (ref.getFileName() && !ctx.context.builtins.some((builtin) => ref.getFileName().endsWith(builtin))) imports.push({
3805
- name: ref.getFileName(),
3806
- ambient: true
3807
- });
3808
- for (const statement of sourceFile.getStatements()) if (ts_morph.Node.isImportDeclaration(statement)) {
3809
- const moduleSpec = statement.getModuleSpecifierValue();
3810
- if (statement.getNamespaceImport()) imports.push({
3811
- name: moduleSpec,
3812
- specifiers: [{ name: statement.getNamespaceImport().getText() }],
3813
- all: true
3814
- });
3815
- else if (statement.getNamedImports().length > 0 || statement.getDefaultImport()) {
3816
- const specifiers = [];
3817
- if (statement.getDefaultImport()) specifiers.push({
3818
- name: statement.getDefaultImport().getText(),
3819
- default: true,
3820
- type: statement.isTypeOnly()
3821
- });
3822
- statement.getNamedImports().forEach((named) => {
3823
- specifiers.push({
3824
- name: named.getName(),
3825
- alias: named.getAliasNode()?.getText(),
3826
- type: statement.isTypeOnly()
3827
- });
3828
- });
3829
- imports.push({
3830
- name: moduleSpec,
3831
- specifiers
3832
- });
3833
- }
3834
- } else if (ts_morph.Node.isExportDeclaration(statement)) {
3835
- const moduleSpec = statement.getModuleSpecifierValue();
3836
- if (moduleSpec) {
3837
- let resolvedSpec = moduleSpec;
3838
- if (!ctx.context.builtins.includes(moduleSpec)) if (ctx.emitted.has(moduleSpec)) resolvedSpec = ctx.emitted.get(moduleSpec);
3839
- else {
3840
- const resolvedModule = await ctx.context.resolve(moduleSpec, filePath);
3841
- if ((0, _stryke_type_checks_is_set_string.isSetString)(resolvedModule?.id)) resolvedSpec = resolvedModule.id;
3842
- }
3843
- const namedExports = statement.getNamedExports();
3844
- if (namedExports.length > 0) exports.push({
3845
- name: resolvedSpec,
3846
- text: statement.getText(),
3847
- fullText: statement.getFullText(),
3848
- specifiers: namedExports.map((named) => ({
3849
- name: named.getName(),
3850
- alias: named.getAliasNode()?.getText(),
3851
- type: statement.isTypeOnly()
3852
- })),
3853
- comment: statement.getLeadingCommentRanges().filter((c) => (0, _stryke_type_checks_is_set_string.isSetString)(c.getText().trim()) && !c.getText().includes("@module")).map((c) => c.getText().trim()).join("\n").trim()
3854
- });
3855
- else exports.push({
3856
- name: resolvedSpec,
3857
- text: statement.getText(),
3858
- fullText: statement.getFullText(),
3859
- all: true,
3860
- comment: statement.getLeadingCommentRanges().filter((c) => (0, _stryke_type_checks_is_set_string.isSetString)(c.getText().trim()) && !c.getText().includes("@module")).map((c) => c.getText().trim()).join("\n").trim()
3861
- });
3862
- } else {
3863
- const specifiers = statement.getNamedExports().map((named) => ({
3864
- name: named.getName(),
3865
- alias: named.getAliasNode()?.getText(),
3866
- type: statement.isTypeOnly()
3867
- }));
3868
- if (specifiers.length > 0) exports.push({
3869
- text: statement.getText(),
3870
- fullText: statement.getFullText(),
3871
- specifiers,
3872
- comment: statement.getLeadingCommentRanges().filter((c) => (0, _stryke_type_checks_is_set_string.isSetString)(c.getText().trim()) && !c.getText().includes("@module")).map((c) => c.getText().trim()).join("\n")
3873
- });
3874
- }
3875
- } else if (ts_morph.Node.isExportAssignment(statement)) exports.push({
3876
- text: statement.getText(),
3877
- fullText: statement.getFullText(),
3878
- comment: statement.getLeadingCommentRanges().filter((c) => (0, _stryke_type_checks_is_set_string.isSetString)(c.getText().trim()) && !c.getText().includes("@module")).map((c) => c.getText().trim()).join("\n")
3879
- });
3880
- else if (ts_morph.Node.isFunctionDeclaration(statement) && statement.isExported() && statement.getNameNode()) exports.push({
3881
- text: statement.getText(),
3882
- fullText: statement.getFullText(),
3883
- specifiers: [{ name: statement.getNameNode().getText() }],
3884
- comment: statement.getLeadingCommentRanges().filter((c) => (0, _stryke_type_checks_is_set_string.isSetString)(c.getText().trim()) && !c.getText().includes("@module")).map((c) => c.getText().trim()).join("\n")
3885
- });
3886
- else if (ts_morph.Node.isVariableStatement(statement) && statement.isExported()) exports.push({
3887
- text: statement.getText(),
3888
- fullText: statement.getFullText(),
3889
- specifiers: statement.getDeclarationList().getDeclarations().filter((decl) => decl.getNameNode() && ts_morph.Node.isIdentifier(decl.getNameNode())).map((decl) => ({ name: decl.getNameNode().getText() })),
3890
- comment: statement.getLeadingCommentRanges().filter((c) => (0, _stryke_type_checks_is_set_string.isSetString)(c.getText().trim()) && !c.getText().includes("@module")).map((c) => c.getText().trim()).join("\n")
3891
- });
3892
- else if (ts_morph.Node.isClassDeclaration(statement) && statement.isExported()) {
3893
- const nameNode = statement.getNameNode();
3894
- exports.push({
3895
- text: statement.getText(),
3896
- fullText: statement.getFullText(),
3897
- specifiers: nameNode ? [{ name: nameNode.getText() }] : void 0,
3898
- comment: statement.getLeadingCommentRanges().filter((c) => (0, _stryke_type_checks_is_set_string.isSetString)(c.getText().trim()) && !c.getText().includes("@module")).map((c) => c.getText().trim()).join("\n")
3899
- });
3900
- } else if (ts_morph.Node.isInterfaceDeclaration(statement) && statement.isExported()) {
3901
- const nameNode = statement.getNameNode();
3902
- exports.push({
3903
- text: statement.getText(),
3904
- fullText: statement.getFullText(),
3905
- specifiers: nameNode ? [{ name: nameNode.getText() }] : void 0,
3906
- comment: statement.getLeadingCommentRanges().filter((c) => (0, _stryke_type_checks_is_set_string.isSetString)(c.getText().trim()) && !c.getText().includes("@module")).map((c) => c.getText().trim()).join("\n")
3907
- });
3908
- } else if (ts_morph.Node.isTypeAliasDeclaration(statement) && statement.isExported()) {
3909
- const nameNode = statement.getNameNode();
3910
- exports.push({
3911
- text: statement.getText(),
3912
- fullText: statement.getFullText(),
3913
- specifiers: nameNode ? [{ name: nameNode.getText() }] : void 0,
3914
- comment: statement.getLeadingCommentRanges().filter((c) => (0, _stryke_type_checks_is_set_string.isSetString)(c.getText().trim()) && !c.getText().includes("@module")).map((c) => c.getText().trim()).join("\n")
3915
- });
3916
- } else if (ctx.context.config.output.sourceMap || !statement.getFullText().includes("//# sourceMappingURL=")) exports.push({
3917
- text: statement.getText(),
3918
- fullText: statement.getFullText(),
3919
- comment: statement.getLeadingCommentRanges().filter((c) => (0, _stryke_type_checks_is_set_string.isSetString)(c.getText().trim()) && !c.getText().includes("@module")).map((c) => c.getText().trim()).join("\n")
3920
- });
3921
- return {
3922
- name,
3923
- text,
3924
- sourceFile,
3925
- comment,
3926
- imports,
3927
- exports
3928
- };
3929
- }
3930
- /**
3931
- * Emits TypeScript declaration types for the provided files using the given TypeScript configuration.
3932
- *
3933
- * @param context - The context containing options and environment paths.
3934
- * @param files - The list of files to generate types for.
3935
- * @returns A promise that resolves to the generated TypeScript declaration types.
3936
- */
3937
- async function emitBuiltinTypes(context, files) {
3938
- if (files.length === 0) {
3939
- context.debug("No files provided for TypeScript types generation. Typescript compilation for built-in modules will be skipped.");
3940
- return {
3941
- code: "",
3942
- directives: []
3943
- };
3944
- }
3945
- context.debug(`Running the TypeScript compiler for ${files.length} generated built-in module files.`);
3946
- const program = createProgram(context, {
3947
- skipAddingFilesFromTsConfig: true,
3948
- compilerOptions: {
3949
- declaration: true,
3950
- declarationMap: false,
3951
- emitDeclarationOnly: true,
3952
- sourceMap: false,
3953
- outDir: (0, _stryke_path_replace.replacePath)(context.builtinsPath, context.config.cwd),
3954
- composite: false,
3955
- incremental: false,
3956
- tsBuildInfoFile: void 0
3957
- }
3958
- });
3959
- program.addSourceFilesAtPaths(files);
3960
- const emitResult = program.emitToMemory({ emitOnlyDtsFiles: true });
3961
- const diagnostics = emitResult.getDiagnostics();
3962
- if (diagnostics && diagnostics.length > 0) if (diagnostics.some((d) => d.getCategory() === ts_morph.DiagnosticCategory.Error)) throw new Error(`The Typescript emit process failed while generating built-in types: \n ${diagnostics.filter((d) => d.getCategory() === ts_morph.DiagnosticCategory.Error).map((d) => `-${d.getSourceFile() ? `${d.getSourceFile()?.getFilePath()}:` : ""} ${String(d.getMessageText())} (at ${d.getStart()}:${d.getLength()})`).join("\n")}`);
3963
- else if (diagnostics.some((d) => d.getCategory() === ts_morph.DiagnosticCategory.Warning)) context.warn(`The Typescript emit process completed with warnings while generating built-in types: \n ${diagnostics.filter((d) => d.getCategory() === ts_morph.DiagnosticCategory.Warning).map((d) => `-${d.getSourceFile() ? `${d.getSourceFile()?.getFilePath()}:` : ""} ${String(d.getMessageText())} (at ${d.getStart()}:${d.getLength()})`).join("\n")}`);
3964
- else context.debug(`The Typescript emit process completed with diagnostic messages while generating built-in types: \n ${diagnostics.map((d) => `-${d.getSourceFile() ? `${d.getSourceFile()?.getFilePath()}:` : ""} ${String(d.getMessageText())} (at ${d.getStart()}:${d.getLength()})`).join("\n")}`);
3965
- const emittedFiles = emitResult.getFiles();
3966
- context.debug(`The TypeScript compiler emitted ${emittedFiles.length} files for built-in types.`);
3967
- if (emittedFiles.length === 0) {
3968
- context.warn("The TypeScript compiler did not emit any files for built-in types. This may indicate an issue with the TypeScript configuration or the provided files.");
3969
- return {
3970
- code: "",
3971
- directives: []
3972
- };
3973
- }
3974
- const ctx = {
3975
- context,
3976
- modules: [],
3977
- emitted: /* @__PURE__ */ new Map()
3978
- };
3979
- await Promise.all(emittedFiles.map(async (emittedFile) => {
3980
- const filePath = (0, _stryke_path_append.appendPath)(emittedFile.filePath, context.config.cwd);
3981
- if (!filePath.endsWith(".map") && (0, _stryke_path_file_path_fns.findFileName)(filePath) !== "tsconfig.tsbuildinfo" && (0, _stryke_path_is_parent_path.isParentPath)(filePath, context.builtinsPath)) {
3982
- const moduleName = (0, _stryke_path_replace.replaceExtension)((0, _stryke_path_replace.replacePath)((0, _stryke_path_replace.replacePath)(filePath, context.builtinsPath), (0, _stryke_path_replace.replacePath)(context.builtinsPath, context.config.cwd)), "", { fullExtension: true });
3983
- if (context.builtins.includes(moduleName)) {
3984
- ctx.emitted.set(filePath, moduleName);
3985
- ctx.modules.push(await extractModuleDeclarations(ctx, filePath, moduleName, emittedFile.text));
3986
- }
3987
- }
3988
- }));
3989
- const commonDeclarations = [];
3990
- for (const mod of ctx.modules) for (const importRef of mod.imports.filter((importRef) => context.builtins.some((builtin) => importRef.name.endsWith(`:${builtin}`)))) {
3991
- const moduleRef = ctx.modules.find((moduleRef) => importRef.name.endsWith(`:${moduleRef.name}`));
3992
- if (moduleRef) {
3993
- let declaration;
3994
- for (const decl of moduleRef.exports.filter((decl) => (0, _stryke_type_checks_is_set_object.isSetObject)(decl))) {
3995
- const specifiers = decl.specifiers?.filter((specifier) => importRef.specifiers?.some((s) => (specifier.alias ? specifier.alias : specifier.name) === (s.alias ? s.alias : s.name)));
3996
- if (specifiers && specifiers.length > 0) {
3997
- importRef.specifiers = importRef.specifiers?.filter((s) => !specifiers.some((specifier) => (specifier.alias ? specifier.alias : specifier.name) === (s.alias ? s.alias : s.name)));
3998
- if (importRef.specifiers && importRef.specifiers.length === 0 && !importRef.all && !importRef.ambient) mod.imports = mod.imports.filter((imp) => imp.name !== importRef.name);
3999
- declaration = decl;
4000
- break;
4001
- }
4002
- }
4003
- if (declaration) {
4004
- for (const decl of moduleRef.exports.filter((decl) => (0, _stryke_type_checks_is_set_object.isSetObject)(decl) && !decl.specifiers?.some((s) => declaration?.specifiers?.some((specifier) => (specifier.alias ? specifier.alias : specifier.name) === (s.alias ? s.alias : s.name))))) {
4005
- const exportModuleRef = decl;
4006
- if ((exportModuleRef.specifiers?.some((s) => s.alias || s.name) || exportModuleRef.name) && new RegExp(`(^|\\s|\\n|\\r\\n|\\(|\\)|<|>|{|}|\\[|\\]|\\!|\\?|\\.|,|\\*|&|:)(${exportModuleRef.specifiers?.map((s) => `${s.alias ? `${s.alias}|` : ""}${s.name}`).join("|") || exportModuleRef.name})($|\\s|\\n|\\r\\n|\\(|\\)|<|>|{|}|\\[|\\]|\\!|\\?|\\.|,|\\*|&|:|;)`).test(declaration.text)) commonDeclarations.push(exportModuleRef);
4007
- }
4008
- commonDeclarations.push(declaration);
4009
- }
4010
- }
4011
- }
4012
- let code = "";
4013
- const directives = [];
4014
- for (const commonDeclaration of commonDeclarations) {
4015
- code += formatTypes(`${commonDeclaration.comment?.trim() ? commonDeclaration.comment.trim() : ""}${commonDeclaration.comment?.trim() ? "\n" : ""}${formatTypes(commonDeclaration.text.replace(/\s*export\s*/, "").replace(/\s*declare\s*interface\s*/, "interface ").replace(/\s*declare\s*type\s*/, "type "))}`);
4016
- code += "\n\n";
4017
- }
4018
- for (const mod of ctx.modules) {
4019
- code += mod.comment ? `${mod.comment.trim()}\n` : "";
4020
- code += `declare module "${context.config.framework}:${mod.name}" { \n`;
4021
- for (const importRef of mod.imports) if (importRef.ambient) code += directives.push(importRef.name);
4022
- else if (importRef.all) code += `\timport * as ${(0, _stryke_path_file_path_fns.findFileName)(importRef.name)} from "${importRef.name}";\n`;
4023
- else if (importRef.specifiers) {
4024
- const typeOnly = importRef.specifiers.every((s) => s.type) ? " type" : "";
4025
- const specifiers = importRef.specifiers.map((s) => s.alias ? `${s.name} as ${s.alias}` : s.name).join(", ");
4026
- code += `\timport${typeOnly} { ${specifiers} } from "${importRef.name}";\n`;
4027
- }
4028
- if (mod.imports.length > 0) code += "\n";
4029
- for (const exportRef of mod.exports.filter((e) => (0, _stryke_type_checks_is_string.isString)(e) || !e.specifiers || !commonDeclarations.some((commonDecl) => commonDecl.specifiers && commonDecl.specifiers.some((specifier) => e.specifiers?.some((s) => (s.alias ? s.alias : s.name) === (specifier.alias ? specifier.alias : specifier.name)))))) if ((0, _stryke_type_checks_is_set_string.isSetString)(exportRef)) code += `${exportRef}\n`;
4030
- else if (exportRef.name) {
4031
- if (exportRef.all) code += `${exportRef.comment?.trim() ? exportRef.comment.trim() : ""}${exportRef.comment?.trim() ? "\n" : ""}export * from "${exportRef.name}";\n`;
4032
- else if (exportRef.specifiers) {
4033
- if (exportRef.comment?.trim()) code += `${exportRef.comment.trim()}\n`;
4034
- code += `\texport${exportRef.specifiers.every((s) => s.type) ? " type" : ""} { ${exportRef.specifiers.map((s) => s.alias ? `${s.name} as ${s.alias}` : s.name).join(", ")} } from "${exportRef.name}";\n`;
4035
- }
4036
- } else code += `${exportRef.comment?.trim() ? exportRef.comment.trim() : ""}${exportRef.comment?.trim() ? "\n" : ""}${formatTypes(exportRef.text.replace(/\s*export\s*declare\s*/, "export ").replace(/\s*declare\s*/, " "))}\n`;
4037
- mod.exports.filter((e) => !(0, _stryke_type_checks_is_string.isString)(e) && e.specifiers && commonDeclarations.some((commonDeclaration) => commonDeclaration.specifiers && commonDeclaration.specifiers.some((specifier) => e.specifiers?.some((s) => (s.alias ? s.alias : s.name) === (specifier.alias ? specifier.alias : specifier.name))))).forEach((e, i, arr) => {
4038
- if (i === 0) code += "\nexport { ";
4039
- else code += ", ";
4040
- code += `${e?.specifiers?.filter((s) => commonDeclarations.some((commonDeclaration) => commonDeclaration.specifiers && commonDeclaration.specifiers.some((specifier) => (s.alias ? s.alias : s.name) === (specifier.alias ? specifier.alias : specifier.name)))).map((s) => s.alias ? `${s.name} as ${s.alias}` : s.name).join(", ") || ""}`;
4041
- if (i === arr.length - 1) code += ` };\n`;
4042
- });
4043
- code += "}";
4044
- code += "\n\n";
4045
- }
4046
- code = await (0, _powerlines_core_lib_utilities_format.format)(context, context.typesPath, code);
4047
- context.debug(`A TypeScript declaration file (size: ${(0, _stryke_string_format_pretty_bytes.prettyBytes)(new Blob((0, _stryke_convert_to_array.toArray)(code)).size)}) emitted for the built-in modules types.`);
4048
- return {
4049
- code,
4050
- directives
4051
- };
4052
- }
4053
-
4054
- //#endregion
4055
- //#region src/_internal/helpers/install.ts
4056
- /**
4057
- * Installs a package if it is not already installed.
4058
- *
4059
- * @param context - The resolved options
4060
- * @param packageName - The name of the package to install
4061
- * @param dev - Whether to install the package as a dev dependency
4062
- */
4063
- async function installPackage(context, packageName, dev = false) {
4064
- if (!await (0, _stryke_fs_package_fns.isPackageListed)((0, _stryke_string_format_package.getPackageName)(packageName), { cwd: context.config.root })) if (context.config.autoInstall) {
4065
- context.warn(`The package "${packageName}" is not installed. It will be installed automatically.`);
4066
- const result = await (0, _stryke_fs_install.install)(packageName, {
4067
- cwd: context.config.root,
4068
- dev
4069
- });
4070
- if ((0, _stryke_type_checks_is_number.isNumber)(result.exitCode) && result.exitCode > 0) {
4071
- context.error(result.stderr);
4072
- throw new Error(`An error occurred while installing the package "${packageName}"`);
4073
- }
4074
- } else context.warn(`The package "${packageName}" is not installed. Since the "autoInstall" option is set to false, it will not be installed automatically.`);
4075
- else if ((0, _stryke_string_format_package.hasPackageVersion)(packageName) && !process.env.POWERLINES_SKIP_VERSION_CHECK) {
4076
- if (!await (0, _stryke_fs_package_fns.doesPackageMatch)((0, _stryke_string_format_package.getPackageName)(packageName), (0, _stryke_string_format_package.getPackageVersion)(packageName), context.config.root)) {
4077
- const packageListing = await (0, _stryke_fs_package_fns.getPackageListing)((0, _stryke_string_format_package.getPackageName)(packageName), { cwd: context.config.root });
4078
- if (!packageListing?.version.startsWith("catalog:") && !packageListing?.version.startsWith("workspace:")) context.warn(`The package "${(0, _stryke_string_format_package.getPackageName)(packageName)}" is installed but does not match the expected version ${(0, _stryke_string_format_package.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.`);
4079
- }
4080
- }
4081
- }
4082
-
4083
- //#endregion
4084
- //#region src/_internal/helpers/install-dependencies.ts
4085
- /**
4086
- * Install missing project dependencies.
4087
- *
4088
- * @param context - The build context.
4089
- */
4090
- async function installDependencies(context) {
4091
- context.debug(`Checking and installing missing project dependencies.`);
4092
- context.dependencies ??= {};
4093
- context.devDependencies ??= {};
4094
- if (Object.keys(context.dependencies).length === 0 && Object.keys(context.devDependencies).length === 0) {
4095
- context.debug(`No dependencies or devDependencies to install. Skipping installation step.`);
4096
- return;
4097
- }
4098
- context.debug(`The following packages are required: \nDependencies: \n${Object.entries(context.dependencies).map(([name, version]) => `- ${name}@${String(version)}`).join(" \n")}\n\nDevDependencies: \n${Object.entries(context.devDependencies).map(([name, version]) => `- ${name}@${String(version)}`).join(" \n")}`);
4099
- await Promise.all([Promise.all(Object.entries(context.dependencies).map(async ([name, version]) => installPackage(context, `${(0, _stryke_string_format_package.getPackageName)(name)}@${String(version)}`, false))), Promise.all(Object.entries(context.devDependencies).map(async ([name, version]) => installPackage(context, `${(0, _stryke_string_format_package.getPackageName)(name)}@${String(version)}`, true)))]);
4100
- }
4101
-
4102
- //#endregion
4103
- //#region src/_internal/helpers/resolve-tsconfig.ts
4104
- function getTsconfigDtsPath(context) {
4105
- return (0, _stryke_path_join_paths.joinPaths)((0, _stryke_path_file_path_fns.relativePath)((0, _stryke_path_join_paths.joinPaths)(context.options.cwd, context.options.root), (0, _stryke_path_file_path_fns.findFilePath)(context.typesPath)), (0, _stryke_path_file_path_fns.findFileName)(context.typesPath));
4106
- }
4107
- async function resolveTsconfigChanges(context) {
4108
- const tsconfig = getParsedTypeScriptConfig(context.options.cwd, context.options.root, context.config.tsconfig, context.config.tsconfigRaw);
4109
- const tsconfigJson = await (0, _stryke_fs_json.readJsonFile)(getTsconfigFilePath(context.options.cwd, context.options.root, context.config.tsconfig));
4110
- tsconfigJson.compilerOptions ??= {};
4111
- if (context.config.output.dts !== false) {
4112
- const dtsRelativePath = getTsconfigDtsPath(context);
4113
- if (!tsconfigJson.include?.some((filePattern) => isIncludeMatchFound(filePattern, [context.typesPath, dtsRelativePath]))) {
4114
- tsconfigJson.include ??= [];
4115
- tsconfigJson.include.push(dtsRelativePath.startsWith("./") ? dtsRelativePath.slice(2) : dtsRelativePath);
4116
- }
4117
- }
4118
- if (!tsconfig.options.lib?.some((lib) => [
4119
- "lib.esnext.d.ts",
4120
- "lib.es2021.d.ts",
4121
- "lib.es2022.d.ts",
4122
- "lib.es2023.d.ts"
4123
- ].includes(lib.toLowerCase()))) {
4124
- tsconfigJson.compilerOptions.lib ??= [];
4125
- tsconfigJson.compilerOptions.lib.push("esnext");
4126
- }
4127
- if (tsconfig.options.esModuleInterop !== true) tsconfigJson.compilerOptions.esModuleInterop = true;
4128
- if (tsconfig.options.isolatedModules !== true) tsconfigJson.compilerOptions.isolatedModules = true;
4129
- if (context.config.platform === "node") {
4130
- if (!tsconfig.options.types?.some((type) => type.toLowerCase() === "node" || type.toLowerCase() === "@types/node")) {
4131
- tsconfigJson.compilerOptions.types ??= [];
4132
- tsconfigJson.compilerOptions.types.push("node");
4133
- }
4134
- }
4135
- return tsconfigJson;
4136
- }
4137
- async function initializeTsconfig(context) {
4138
- context.debug("Initializing TypeScript configuration (tsconfig.json) for the Powerlines project.");
4139
- if (!(0, _stryke_fs_package_fns.isPackageExists)("typescript")) throw new Error("The TypeScript package is not installed. Please install the package using the command: \"npm install typescript --save-dev\"");
4140
- const tsconfigFilePath = getTsconfigFilePath(context.config.cwd, context.config.root, context.config.tsconfig);
4141
- context.tsconfig.originalTsconfigJson = await (0, _stryke_fs_json.readJsonFile)(tsconfigFilePath);
4142
- context.tsconfig.tsconfigJson = await resolveTsconfigChanges(context);
4143
- context.debug("Writing updated TypeScript configuration (tsconfig.json) file to disk.");
4144
- await context.fs.write(tsconfigFilePath, _stryke_json_storm_json.StormJSON.stringify(context.tsconfig.tsconfigJson));
4145
- context.tsconfig = getParsedTypeScriptConfig(context.config.cwd, context.config.root, context.config.tsconfig, context.config.tsconfigRaw, context.tsconfig.originalTsconfigJson);
4146
- }
4147
- async function resolveTsconfig(context) {
4148
- const updateTsconfigJson = await (0, _stryke_fs_json.readJsonFile)(context.tsconfig.tsconfigFilePath);
4149
- if (updateTsconfigJson?.compilerOptions?.types && Array.isArray(updateTsconfigJson.compilerOptions.types) && !updateTsconfigJson.compilerOptions.types.length) delete updateTsconfigJson.compilerOptions.types;
4150
- const result = (0, _donedeal0_superdiff.getObjectDiff)(context.tsconfig.originalTsconfigJson, updateTsconfigJson, {
4151
- ignoreArrayOrder: true,
4152
- showOnly: {
4153
- statuses: [
4154
- "added",
4155
- "deleted",
4156
- "updated"
4157
- ],
4158
- granularity: "deep"
4159
- }
4160
- });
4161
- const changes = [];
4162
- const getChanges = (difference, property) => {
4163
- if (difference.status === "added" || difference.status === "deleted" || difference.status === "updated") if (difference.diff) for (const diff of difference.diff) getChanges(diff, property ? `${property}.${difference.property}` : difference.property);
4164
- else changes.push({
4165
- field: property ? `${property}.${difference.property}` : difference.property,
4166
- status: difference.status,
4167
- previous: difference.status === "added" ? "---" : _stryke_json_storm_json.StormJSON.stringify(difference.previousValue),
4168
- current: difference.status === "deleted" ? "---" : _stryke_json_storm_json.StormJSON.stringify(difference.currentValue)
4169
- });
4170
- };
4171
- for (const diff of result.diff) getChanges(diff);
4172
- if (changes.length > 0) context.warn(`Updating the following configuration values in "${context.tsconfig.tsconfigFilePath}" file:
4173
-
4174
- ${changes.map((change, i) => `${chalk.default.bold.whiteBright(`${i + 1}. ${(0, _stryke_string_format_title_case.titleCase)(change.status)} the ${change.field} field: `)}
4175
- ${chalk.default.red(` - Previous: ${change.previous} `)}
4176
- ${chalk.default.green(` - Updated: ${change.current} `)}
4177
- `).join("\n")}
4178
- `);
4179
- await context.fs.write(context.tsconfig.tsconfigFilePath, _stryke_json_storm_json.StormJSON.stringify(updateTsconfigJson));
4180
- context.tsconfig = getParsedTypeScriptConfig(context.config.cwd, context.config.root, context.config.tsconfig);
4181
- if (!context.tsconfig) throw new Error("Failed to parse the TypeScript configuration file.");
4182
- }
4183
-
4184
- //#endregion
4185
- //#region src/_internal/execution.ts
4186
- /**
4187
- * The Powerlines Execution API class
4188
- *
4189
- * @remarks
4190
- * This class is responsible for managing the Powerlines project lifecycle, including initialization, building, and finalization.
4191
- *
4192
- * @internal
4193
- */
4194
- var PowerlinesExecution = class PowerlinesExecution {
4195
- /**
4196
- * The Powerlines context
4197
- */
4198
- #context;
4199
- async #handleBuild(context) {
4200
- await this.callHook("build", {
4201
- environment: context,
4202
- order: "pre"
4203
- });
4204
- context.debug("Formatting the generated entry files before the build process starts.");
4205
- await (0, _powerlines_core_lib_utilities_format.formatFolder)(context, context.entryPath);
4206
- await this.callHook("build", {
4207
- environment: context,
4208
- order: "normal"
4209
- });
4210
- if (context.config.output.copy) {
4211
- context.debug("Copying project's files from build output directory.");
4212
- const destinationPath = (0, _stryke_path_is_parent_path.isParentPath)((0, _stryke_path_append.appendPath)(context.config.output.path, context.config.cwd), (0, _stryke_path_append.appendPath)(context.config.root, context.config.cwd)) ? (0, _stryke_path_join_paths.joinPaths)(context.config.output.copy.path, (0, _stryke_path_file_path_fns.relativePath)((0, _stryke_path_append.appendPath)(context.config.root, context.config.cwd), (0, _stryke_path_append.appendPath)(context.config.output.path, context.config.cwd))) : (0, _stryke_path_join_paths.joinPaths)(context.config.output.copy.path, "dist");
4213
- const sourcePath = context.config.output.path;
4214
- if ((0, _stryke_fs_exists.existsSync)(sourcePath) && sourcePath !== destinationPath) {
4215
- context.debug(`Copying files from project's build output directory (${context.config.output.path}) to the project's copy/publish directory (${destinationPath}).`);
4216
- await (0, _stryke_fs_copy_file.copyFiles)(sourcePath, destinationPath);
4217
- } else context.warn(`The source path for the copy operation ${!(0, _stryke_fs_exists.existsSync)(sourcePath) ? "does not exist" : "is the same as the destination path"}. Source: ${sourcePath}, Destination: ${destinationPath}. Skipping copying of build output files.`);
4218
- if (context.config.output.copy.assets && Array.isArray(context.config.output.copy.assets)) await Promise.all(context.config.output.copy.assets.map(async (asset) => {
4219
- context.trace(`Copying asset(s): ${chalk.default.redBright(context.config.cwd === asset.input ? asset.glob : (0, _stryke_path_append.appendPath)(asset.glob, (0, _stryke_path_replace.replacePath)(asset.input, context.config.cwd)))} -> ${chalk.default.greenBright((0, _stryke_path_append.appendPath)(asset.glob, (0, _stryke_path_replace.replacePath)(asset.output, context.config.cwd)))} ${Array.isArray(asset.ignore) && asset.ignore.length > 0 ? ` (ignoring: ${asset.ignore.map((i) => chalk.default.yellowBright(i)).join(", ")})` : ""}`);
4220
- await context.fs.copy(asset, asset.output);
4221
- }));
4222
- } else context.debug("No copy configuration found for the project output. Skipping the copying of build output files.");
4223
- await this.callHook("build", {
4224
- environment: context,
4225
- order: "post"
4226
- });
4227
- }
4228
- /**
4229
- * Get the configured environments
4230
- *
4231
- * @returns The configured environments
4232
- */
4233
- async #getEnvironments() {
4234
- if (!this.context.config.environments || Object.keys(this.context.config.environments).length <= 1) {
4235
- this.context.debug("No environments are configured for this Powerlines project. Using the default environment.");
4236
- return [await this.context.getEnvironment()];
4237
- }
4238
- this.context.debug(`Found ${Object.keys(this.context.config.environments).length} configured environment(s) for this Powerlines project.`);
4239
- return (await Promise.all(Object.entries(this.context.config.environments).map(async ([name, config]) => {
4240
- if (!await this.context.getEnvironmentSafe(name)) {
4241
- const resolvedEnvironment = await this.callHook("configEnvironment", { environment: name }, name, config);
4242
- if (resolvedEnvironment) this.context.environments[name] = await this.context.in(resolvedEnvironment);
4243
- }
4244
- return this.context.environments[name];
4245
- }))).filter((context) => (0, _stryke_type_checks_is_set.isSet)(context));
4246
- }
4247
- /**
4248
- * Execute a handler function for each environment
4249
- *
4250
- * @param handle - The handler function to execute for each environment
4251
- */
4252
- async #executeEnvironments(handle) {
4253
- await Promise.all((await this.#getEnvironments()).map(async (context) => {
4254
- return Promise.resolve(handle(context));
4255
- }));
4256
- }
4257
- /**
4258
- * Initialize a Powerlines plugin
4259
- *
4260
- * @param config - The configuration for the plugin
4261
- * @returns The initialized plugin instance, or null if the plugin was a duplicate
4262
- * @throws Will throw an error if the plugin cannot be found or is invalid
4263
- */
4264
- async #initPlugin(config) {
4265
- let awaited = config;
4266
- if ((0, _stryke_type_checks_is_promise.isPromiseLike)(config)) awaited = await Promise.resolve(config);
4267
- if (!(0, _powerlines_core_plugin_utils.isPluginConfig)(awaited)) {
4268
- const invalid = (0, _powerlines_core_plugin_utils.findInvalidPluginConfig)(awaited);
4269
- 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`);
4270
- }
4271
- let plugins;
4272
- if ((0, _powerlines_core_plugin_utils.isPlugin)(awaited)) plugins = [awaited];
4273
- else if ((0, _stryke_type_checks_is_function.isFunction)(awaited)) plugins = (0, _stryke_convert_to_array.toArray)(await Promise.resolve(awaited()));
4274
- else if ((0, _stryke_type_checks_is_string.isString)(awaited)) {
4275
- const resolved = await this.#resolvePlugin(awaited);
4276
- if ((0, _stryke_type_checks_is_function.isFunction)(resolved)) plugins = (0, _stryke_convert_to_array.toArray)(await Promise.resolve(resolved()));
4277
- else plugins = (0, _stryke_convert_to_array.toArray)(resolved);
4278
- } else if (Array.isArray(awaited) && awaited.every(_powerlines_core_plugin_utils.isPlugin)) plugins = awaited;
4279
- else if (Array.isArray(awaited) && awaited.every(_powerlines_core_plugin_utils.isPluginConfig)) {
4280
- plugins = [];
4281
- for (const pluginConfig of awaited) {
4282
- const initialized = await this.#initPlugin(pluginConfig);
4283
- if (initialized) plugins.push(...initialized);
4284
- }
4285
- } else if ((0, _powerlines_core_plugin_utils.isPluginConfigTuple)(awaited) || (0, _powerlines_core_plugin_utils.isPluginConfigObject)(awaited)) {
4286
- let pluginConfig;
4287
- let pluginOptions;
4288
- if ((0, _powerlines_core_plugin_utils.isPluginConfigTuple)(awaited)) {
4289
- pluginConfig = awaited[0];
4290
- pluginOptions = awaited?.length === 2 ? awaited[1] : void 0;
4291
- } else {
4292
- pluginConfig = awaited.plugin;
4293
- pluginOptions = awaited.options;
4294
- }
4295
- if ((0, _stryke_type_checks_is_set_string.isSetString)(pluginConfig)) {
4296
- const resolved = await this.#resolvePlugin(pluginConfig);
4297
- if ((0, _stryke_type_checks_is_function.isFunction)(resolved)) plugins = (0, _stryke_convert_to_array.toArray)(await Promise.resolve(pluginOptions ? resolved(pluginOptions) : resolved()));
4298
- else plugins = (0, _stryke_convert_to_array.toArray)(resolved);
4299
- } else if ((0, _stryke_type_checks_is_function.isFunction)(pluginConfig)) plugins = (0, _stryke_convert_to_array.toArray)(await Promise.resolve(pluginConfig(pluginOptions)));
4300
- else if (Array.isArray(pluginConfig) && pluginConfig.every(_powerlines_core_plugin_utils.isPlugin)) plugins = pluginConfig;
4301
- else if ((0, _powerlines_core_plugin_utils.isPlugin)(pluginConfig)) plugins = (0, _stryke_convert_to_array.toArray)(pluginConfig);
4302
- }
4303
- if (!plugins) throw new Error(`The plugin configuration ${JSON.stringify(awaited)} is invalid. This configuration must point to a valid Powerlines plugin module.`);
4304
- if (plugins.length > 0 && !plugins.every(_powerlines_core_plugin_utils.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.`);
4305
- const result = [];
4306
- for (const plugin of plugins) if ((0, _powerlines_core_plugin_utils.isDuplicate)(plugin, this.context.plugins)) this.context.trace(`Duplicate ${chalk.default.bold.cyanBright(plugin.name)} plugin dependency detected - Skipping initialization.`);
4307
- else {
4308
- result.push(plugin);
4309
- this.context.trace(`Initializing the ${chalk.default.bold.cyanBright(plugin.name)} plugin...`);
4310
- }
4311
- return result;
4312
- }
4313
- async #resolvePlugin(pluginPath) {
4314
- if (pluginPath.startsWith("@") && pluginPath.split("/").filter(Boolean).length > 2) {
4315
- const splits = pluginPath.split("/").filter(Boolean);
4316
- pluginPath = `${splits[0]}/${splits[1]}`;
4317
- }
4318
- const isInstalled = (0, _stryke_fs_package_fns.isPackageExists)(pluginPath, { paths: [this.context.config.cwd, this.context.config.root] });
4319
- if (!isInstalled && this.context.config.autoInstall) {
4320
- this.#context.warn(`The plugin package "${pluginPath}" is not installed. It will be installed automatically.`);
4321
- const result = await (0, _stryke_fs_install.install)(pluginPath, { cwd: this.context.config.root });
4322
- if ((0, _stryke_type_checks_is_number.isNumber)(result.exitCode) && result.exitCode > 0) {
4323
- this.#context.error(result.stderr);
4324
- throw new Error(`An error occurred while installing the build plugin package "${pluginPath}" `);
4325
- }
4326
- }
4327
- try {
4328
- const module = await this.context.resolver.plugin.import(this.context.resolver.plugin.esmResolve((0, _stryke_path_join_paths.joinPaths)(pluginPath, "plugin")));
4329
- const result = module.plugin ?? module.default;
4330
- if (!result) throw new Error(`The plugin package "${pluginPath}" does not export a valid module.`);
4331
- return result;
4332
- } catch (error) {
4333
- try {
4334
- const module = await this.context.resolver.plugin.import(this.context.resolver.plugin.esmResolve(pluginPath));
4335
- const result = module.plugin ?? module.default;
4336
- if (!result) throw new Error(`The plugin package "${pluginPath}" does not export a valid module.`);
4337
- return result;
4338
- } catch {
4339
- if (!isInstalled) throw new Error(`The plugin package "${pluginPath}" is not installed. Please install the package using the command: "npm install ${pluginPath} --save-dev"`);
4340
- else throw new Error(`An error occurred while importing the build plugin package "${pluginPath}":
4341
- ${(0, _stryke_type_checks_is_error.isError)(error) ? error.message : String(error)}
4342
-
4343
- 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\`.`);
4344
- }
4345
- }
4346
- }
4347
- /**
4348
- * Generate the Powerlines TypeScript declaration file
4349
- *
4350
- * @remarks
4351
- * This method will generate the TypeScript declaration file for the Powerlines project, including any types provided by plugins.
4352
- *
4353
- * @param context - The environment context to use for generating the TypeScript declaration file
4354
- * @returns A promise that resolves when the TypeScript declaration file has been generated
4355
- */
4356
- async #types(context) {
4357
- context.debug(`Preparing the TypeScript definitions for the Powerlines project.`);
4358
- if (context.fs.existsSync(context.typesPath)) await context.fs.remove(context.typesPath);
4359
- if (!await (0, _stryke_fs_resolve.resolvePackage)("typescript")) throw new Error("Could not resolve TypeScript package location. Please ensure TypeScript is installed.");
4360
- context.debug("Running TypeScript compiler for built-in runtime module files.");
4361
- let { code, directives } = await emitBuiltinTypes(context, (await context.getBuiltins()).reduce((ret, builtin) => {
4362
- const formatted = (0, _stryke_path_replace.replacePath)(builtin.path, context.config.cwd);
4363
- if (!ret.includes(formatted)) ret.push(formatted);
4364
- return ret;
4365
- }, []));
4366
- context.debug(`Generating TypeScript declaration file ${context.typesPath}.`);
4367
- const merge = async (currentResult, previousResult) => {
4368
- if (!(0, _stryke_type_checks_is_set_string.isSetString)(currentResult) && !(0, _stryke_type_checks_is_set_object.isSetObject)(currentResult) && !(0, _stryke_type_checks_is_set_string.isSetString)(previousResult) && !(0, _stryke_type_checks_is_set_object.isSetObject)(previousResult)) return {
4369
- code,
4370
- directives
4371
- };
4372
- const previous = (await (0, _powerlines_core_lib_utilities_format.format)(context, context.typesPath, (0, _stryke_type_checks_is_set_string.isSetString)(previousResult) ? previousResult : (0, _stryke_type_checks_is_set_object.isSetObject)(previousResult) ? previousResult.code : "")).trim().replace(code, "").trim();
4373
- const current = (await (0, _powerlines_core_lib_utilities_format.format)(context, context.typesPath, (0, _stryke_type_checks_is_set_string.isSetString)(currentResult) ? currentResult : (0, _stryke_type_checks_is_set_object.isSetObject)(currentResult) ? currentResult.code : "")).trim().replace(previous, "").trim().replace(code, "").trim();
4374
- return {
4375
- directives: [...(0, _stryke_type_checks_is_set_object.isSetObject)(currentResult) && currentResult.directives ? currentResult.directives : [], ...(0, _stryke_type_checks_is_set_object.isSetObject)(previousResult) && previousResult.directives ? previousResult.directives : []],
4376
- code: await (0, _powerlines_core_lib_utilities_format.format)(context, context.typesPath, `${!previous.includes((0, _powerlines_core_lib_utilities_file_header.getTypescriptFileHeader)(context)) && !current.includes((0, _powerlines_core_lib_utilities_file_header.getTypescriptFileHeader)(context)) ? `${code}\n` : ""}${previous}\n${current}`.trim())
4377
- };
4378
- };
4379
- const asNextParam = (previousResult) => (0, _stryke_type_checks_is_object.isObject)(previousResult) ? previousResult.code : previousResult;
4380
- let result = await this.callHook("types", {
4381
- environment: context,
4382
- sequential: true,
4383
- order: "pre",
4384
- result: "merge",
4385
- merge,
4386
- asNextParam
4387
- }, code);
4388
- if (result) {
4389
- if ((0, _stryke_type_checks_is_set_object.isSetObject)(result)) {
4390
- code = result.code;
4391
- if (Array.isArray(result.directives) && result.directives.length > 0) directives = (0, _stryke_helpers_get_unique.getUnique)([...directives, ...result.directives]).filter(Boolean);
4392
- } else if ((0, _stryke_type_checks_is_set_string.isSetString)(result)) code = result;
4393
- }
4394
- result = await this.callHook("types", {
4395
- environment: context,
4396
- sequential: true,
4397
- order: "normal",
4398
- result: "merge",
4399
- merge,
4400
- asNextParam
4401
- }, code);
4402
- if (result) {
4403
- if ((0, _stryke_type_checks_is_set_object.isSetObject)(result)) {
4404
- code = result.code;
4405
- if (Array.isArray(result.directives) && result.directives.length > 0) directives = (0, _stryke_helpers_get_unique.getUnique)([...directives, ...result.directives]).filter(Boolean);
4406
- } else if ((0, _stryke_type_checks_is_set_string.isSetString)(result)) code = result;
4407
- }
4408
- result = await this.callHook("types", {
4409
- environment: context,
4410
- sequential: true,
4411
- order: "post",
4412
- result: "merge",
4413
- merge,
4414
- asNextParam
4415
- }, code);
4416
- if (result) {
4417
- if ((0, _stryke_type_checks_is_set_object.isSetObject)(result)) {
4418
- code = result.code;
4419
- if (Array.isArray(result.directives) && result.directives.length > 0) directives = (0, _stryke_helpers_get_unique.getUnique)([...directives, ...result.directives]).filter(Boolean);
4420
- } else if ((0, _stryke_type_checks_is_set_string.isSetString)(result)) code = result;
4421
- }
4422
- if ((0, _stryke_type_checks_is_set_string.isSetString)(code?.trim()) || directives.length > 0) await context.fs.write(context.typesPath, `${directives.length > 0 ? `${directives.map((directive) => `/// <reference types="${directive}" />`).join("\n")}
4423
-
4424
- ` : ""}${(0, _powerlines_core_lib_utilities_file_header.getTypescriptFileHeader)(context, {
4425
- directive: null,
4426
- prettierIgnore: false
4427
- })}
4428
-
4429
- ${formatTypes(code)}
4430
- `);
4431
- }
4432
- /**
4433
- * The Powerlines context
4434
- */
4435
- get context() {
4436
- return this.#context;
4437
- }
4438
- /**
4439
- * Initialize a Powerlines API instance
4440
- *
4441
- * @param options - The options to initialize the API with
4442
- * @returns A new instance of the Powerlines API
4443
- */
4444
- static async fromConfig(options, config) {
4445
- const api = new PowerlinesExecution(await PowerlinesExecutionContext.fromOptions(options));
4446
- api.#context.config.inlineConfig = config;
4447
- await api.#context.setup();
4448
- api.#context.$$internal = {
4449
- api,
4450
- addPlugin: api.addPlugin.bind(api)
4451
- };
4452
- const timer = api.context.timer("Initialization");
4453
- for (const plugin of api.context.config.plugins.flat(10) ?? []) await api.addPlugin(plugin);
4454
- 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.");
4455
- else api.context.info(`Loaded ${api.context.plugins.length} ${(0, _stryke_string_format_title_case.titleCase)(api.context.config.framework)} plugin${api.context.plugins.length > 1 ? "s" : ""}: \n${api.context.plugins.map((plugin, index) => ` ${index + 1}. ${(0, _powerlines_core_lib_logger.colorText)(plugin.name)}`).join("\n")}`);
4456
- const pluginConfig = await api.callHook("config", {
4457
- environment: await api.context.getEnvironment(),
4458
- sequential: true,
4459
- result: "merge",
4460
- merge: mergeConfigs
4461
- });
4462
- if (pluginConfig) {
4463
- api.#context.config.pluginConfig = pluginConfig;
4464
- await api.#context.setup();
4465
- }
4466
- timer();
4467
- return api;
4468
- }
4469
- /**
4470
- * Generate the Powerlines typescript declaration file
4471
- *
4472
- * @remarks
4473
- * This method will only generate the typescript declaration file for the Powerlines project. It is generally recommended to run the full `prepare` command, which will run this method as part of its process.
4474
- *
4475
- * @param inlineConfig - The inline configuration for the types command
4476
- */
4477
- async types(inlineConfig = { command: "types" }) {
4478
- this.context.debug(" Aggregating configuration options for the Powerlines project");
4479
- inlineConfig.command ??= "types";
4480
- this.context.config.inlineConfig = inlineConfig;
4481
- await this.context.setup();
4482
- await this.#executeEnvironments(async (context) => {
4483
- context.debug(`Initializing the processing options for the Powerlines project.`);
4484
- await this.callHook("configResolved", {
4485
- environment: context,
4486
- order: "pre"
4487
- });
4488
- await initializeTsconfig(context);
4489
- await this.callHook("configResolved", {
4490
- environment: context,
4491
- order: "normal"
4492
- });
4493
- if (context.entry.length > 0) context.debug(`The configuration provided ${(0, _stryke_type_checks_is_object.isObject)(context.config.input) ? Object.keys(context.config.input).length : (0, _stryke_convert_to_array.toArray)(context.config.input).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")}` : ""}`);
4494
- 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.`);
4495
- await resolveTsconfig(context);
4496
- await installDependencies(context);
4497
- await this.callHook("configResolved", {
4498
- environment: context,
4499
- order: "post"
4500
- });
4501
- context.trace(`Powerlines configuration has been resolved: \n\n${(0, _storm_software_config_tools_logger_console.formatLogMessage)({
4502
- ...context.config,
4503
- userConfig: (0, _stryke_type_checks_is_set_object.isSetObject)(context.config.userConfig) ? (0, _stryke_helpers_omit.omit)(context.config.userConfig, ["plugins"]) : void 0,
4504
- inlineConfig: (0, _stryke_type_checks_is_set_object.isSetObject)(context.config.inlineConfig) ? (0, _stryke_helpers_omit.omit)(context.config.inlineConfig, ["plugins"]) : void 0,
4505
- plugins: context.plugins.map((plugin) => plugin.plugin.name)
4506
- })}`);
4507
- if (!context.fs.existsSync(context.cachePath)) await (0, _stryke_fs_helpers.createDirectory)(context.cachePath);
4508
- if (!context.fs.existsSync(context.dataPath)) await (0, _stryke_fs_helpers.createDirectory)(context.dataPath);
4509
- if (context.config.skipCache === true || context.persistedMeta?.checksum !== context.meta.checksum) context.debug(`Using previously prepared files as the meta checksum has not changed.`);
4510
- else {
4511
- context.info(`Running \`prepare\` command as the meta checksum has changed since the last run.`);
4512
- await this.prepare((0, defu.default)({ output: { types: false } }, inlineConfig));
4513
- }
4514
- await this.#types(context);
4515
- this.context.debug("Formatting files generated during the types step.");
4516
- await (0, _powerlines_core_lib_utilities_format.format)(context, context.typesPath, await context.fs.read(context.typesPath) ?? "");
4517
- await writeMetaFile(context);
4518
- context.persistedMeta = context.meta;
4519
- });
4520
- }
4521
- /**
4522
- * Prepare the Powerlines API
4523
- *
4524
- * @remarks
4525
- * This method will prepare the Powerlines API for use, initializing any necessary resources.
4526
- *
4527
- * @param inlineConfig - The inline configuration for the prepare command
4528
- */
4529
- async prepare(inlineConfig = { command: "prepare" }) {
4530
- inlineConfig.command ??= "prepare";
4531
- this.context.config.inlineConfig = inlineConfig;
4532
- await this.context.setup();
4533
- await this.#executeEnvironments(async (context) => {
4534
- context.debug(`Initializing the processing options for the Powerlines project.`);
4535
- await this.callHook("configResolved", {
4536
- environment: context,
4537
- order: "pre"
4538
- });
4539
- await initializeTsconfig(context);
4540
- await this.callHook("configResolved", {
4541
- environment: context,
4542
- order: "normal"
4543
- });
4544
- if (context.entry.length > 0) context.debug(`The configuration provided ${(0, _stryke_type_checks_is_object.isObject)(context.config.input) ? Object.keys(context.config.input).length : (0, _stryke_convert_to_array.toArray)(context.config.input).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")}` : ""}`);
4545
- 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.`);
4546
- await resolveTsconfig(context);
4547
- await installDependencies(context);
4548
- await this.callHook("configResolved", {
4549
- environment: context,
4550
- order: "post"
4551
- });
4552
- context.trace(`Powerlines configuration has been resolved: \n\n${(0, _storm_software_config_tools_logger_console.formatLogMessage)({
4553
- ...context.config,
4554
- userConfig: (0, _stryke_type_checks_is_set_object.isSetObject)(context.config.userConfig) ? (0, _stryke_helpers_omit.omit)(context.config.userConfig, ["plugins"]) : void 0,
4555
- inlineConfig: (0, _stryke_type_checks_is_set_object.isSetObject)(context.config.inlineConfig) ? (0, _stryke_helpers_omit.omit)(context.config.inlineConfig, ["plugins"]) : void 0,
4556
- plugins: context.plugins.map((plugin) => plugin.plugin.name)
4557
- })}`);
4558
- if (!context.fs.existsSync(context.cachePath)) await (0, _stryke_fs_helpers.createDirectory)(context.cachePath);
4559
- if (!context.fs.existsSync(context.dataPath)) await (0, _stryke_fs_helpers.createDirectory)(context.dataPath);
4560
- await this.callHook("prepare", {
4561
- environment: context,
4562
- order: "pre"
4563
- });
4564
- await this.callHook("prepare", {
4565
- environment: context,
4566
- order: "normal"
4567
- });
4568
- await this.callHook("prepare", {
4569
- environment: context,
4570
- order: "post"
4571
- });
4572
- if (context.config.output.types !== false) await this.#types(context);
4573
- this.context.debug("Formatting files generated during the prepare step.");
4574
- await Promise.all([(0, _powerlines_core_lib_utilities_format.formatFolder)(context, context.builtinsPath), (0, _powerlines_core_lib_utilities_format.formatFolder)(context, context.entryPath)]);
4575
- await writeMetaFile(context);
4576
- context.persistedMeta = context.meta;
4577
- });
4578
- }
4579
- /**
4580
- * Create a new Powerlines project
4581
- *
4582
- * @remarks
4583
- * This method will create a new Powerlines project in the current directory.
4584
- *
4585
- * @param inlineConfig - The inline configuration for the new command
4586
- * @returns A promise that resolves when the project has been created
4587
- */
4588
- async new(inlineConfig) {
4589
- inlineConfig.command ??= "new";
4590
- await this.prepare(inlineConfig);
4591
- await this.#executeEnvironments(async (context) => {
4592
- context.debug("Initializing the processing options for the Powerlines project.");
4593
- await this.callHook("new", {
4594
- environment: context,
4595
- order: "pre"
4596
- });
4597
- const files = await (0, _stryke_fs_list_files.listFiles)((0, _stryke_path_join_paths.joinPaths)(context.powerlinesPath, "files/common/**/*.hbs"));
4598
- for (const file of files) {
4599
- context.trace(`Adding template file to project: ${file}`);
4600
- const template = handlebars.default.compile(file);
4601
- await context.fs.write((0, _stryke_path_join_paths.joinPaths)(context.config.root, file.replace(".hbs", "")), template(context));
4602
- }
4603
- await this.callHook("new", {
4604
- environment: context,
4605
- order: "normal"
4606
- });
4607
- if (context.config.projectType === "application") {
4608
- const files = await (0, _stryke_fs_list_files.listFiles)((0, _stryke_path_join_paths.joinPaths)(context.powerlinesPath, "files/application/**/*.hbs"));
4609
- for (const file of files) {
4610
- context.trace(`Adding application template file: ${file}`);
4611
- const template = handlebars.default.compile(file);
4612
- await context.fs.write((0, _stryke_path_join_paths.joinPaths)(context.config.root, file.replace(".hbs", "")), template(context));
4613
- }
4614
- } else {
4615
- const files = await (0, _stryke_fs_list_files.listFiles)((0, _stryke_path_join_paths.joinPaths)(context.powerlinesPath, "files/library/**/*.hbs"));
4616
- for (const file of files) {
4617
- context.trace(`Adding library template file: ${file}`);
4618
- const template = handlebars.default.compile(file);
4619
- await context.fs.write((0, _stryke_path_join_paths.joinPaths)(context.config.root, file.replace(".hbs", "")), template(context));
4620
- }
4621
- }
4622
- await this.callHook("new", {
4623
- environment: context,
4624
- order: "post"
4625
- });
4626
- });
4627
- }
4628
- /**
4629
- * Clean any previously prepared artifacts
4630
- *
4631
- * @remarks
4632
- * This method will remove the previous Powerlines artifacts from the project.
4633
- *
4634
- * @param inlineConfig - The inline configuration for the clean command
4635
- * @returns A promise that resolves when the clean command has completed
4636
- */
4637
- async clean(inlineConfig = { command: "clean" }) {
4638
- inlineConfig.command ??= "clean";
4639
- await this.prepare(inlineConfig);
4640
- await this.#executeEnvironments(async (context) => {
4641
- context.debug("Cleaning the project's dist and artifacts directories.");
4642
- await context.fs.remove((0, _stryke_path_join_paths.joinPaths)(context.config.cwd, context.config.output.path));
4643
- await context.fs.remove((0, _stryke_path_join_paths.joinPaths)(context.config.cwd, context.config.root, context.config.output.artifactsPath));
4644
- await this.callHook("clean", {
4645
- environment: context,
4646
- sequential: false
4647
- });
4648
- });
4649
- }
4650
- /**
4651
- * Lint the project
4652
- *
4653
- * @param inlineConfig - The inline configuration for the lint command
4654
- * @returns A promise that resolves when the lint command has completed
4655
- */
4656
- async lint(inlineConfig = { command: "lint" }) {
4657
- inlineConfig.command ??= "lint";
4658
- await this.prepare(inlineConfig);
4659
- await this.#executeEnvironments(async (context) => {
4660
- await this.callHook("lint", {
4661
- environment: context,
4662
- sequential: false
4663
- });
4664
- });
4665
- }
4666
- /**
4667
- * Test the project source code
4668
- *
4669
- * @param inlineConfig - The inline configuration for the test command
4670
- * @returns A promise that resolves when the test command has completed
4671
- */
4672
- async test(inlineConfig = { command: "test" }) {
4673
- inlineConfig.command ??= "test";
4674
- await this.prepare(inlineConfig);
4675
- await this.#executeEnvironments(async (context) => {
4676
- await this.callHook("test", {
4677
- environment: context,
4678
- sequential: false
4679
- });
4680
- });
4681
- }
4682
- /**
4683
- * Build the project
4684
- *
4685
- * @remarks
4686
- * This method will build the Powerlines project, generating the necessary artifacts.
4687
- *
4688
- * @param inlineConfig - The inline configuration for the build command
4689
- * @returns A promise that resolves when the build command has completed
4690
- */
4691
- async build(inlineConfig = { command: "build" }) {
4692
- inlineConfig.command ??= "build";
4693
- await this.context.generateChecksum();
4694
- if (this.context.meta.checksum !== this.context.persistedMeta?.checksum || this.context.config.skipCache) {
4695
- this.context.info(!this.context.persistedMeta?.checksum ? "No previous build cache found. Preparing the project for the initial build." : this.context.meta.checksum !== this.context.persistedMeta.checksum ? "The project has been modified since the last time `prepare` was ran. Re-preparing the project." : "The project is configured to skip cache. Re-preparing the project.");
4696
- await this.prepare(inlineConfig);
4697
- } else {
4698
- this.context.config.inlineConfig = inlineConfig;
4699
- await this.context.setup();
4700
- }
4701
- if (this.context.config.singleBuild) await this.#handleBuild(await this.#context.toEnvironment());
4702
- else await this.#executeEnvironments(async (context) => {
4703
- await this.#handleBuild(context);
4704
- });
4705
- }
4706
- /**
4707
- * Prepare the documentation for the project
4708
- *
4709
- * @param inlineConfig - The inline configuration for the docs command
4710
- * @returns A promise that resolves when the documentation generation has completed
4711
- */
4712
- async docs(inlineConfig = { command: "docs" }) {
4713
- inlineConfig.command ??= "docs";
4714
- await this.prepare(inlineConfig);
4715
- await this.#executeEnvironments(async (context) => {
4716
- context.debug("Writing documentation for the Powerlines project artifacts.");
4717
- await this.callHook("docs", { environment: context });
4718
- });
4719
- }
4720
- /**
4721
- * Deploy the project source code
4722
- *
4723
- * @remarks
4724
- * This method will prepare and build the Powerlines project, generating the necessary artifacts for the deployment.
4725
- *
4726
- * @param inlineConfig - The inline configuration for the deploy command
4727
- */
4728
- async deploy(inlineConfig = { command: "deploy" }) {
4729
- inlineConfig.command ??= "deploy";
4730
- await this.prepare(inlineConfig);
4731
- await this.#executeEnvironments(async (context) => {
4732
- await this.callHook("deploy", { environment: context });
4733
- });
4734
- }
4735
- /**
4736
- * Finalization/cleanup processing for the Powerlines API
4737
- *
4738
- * @remarks
4739
- * This step includes any final processes or clean up required by Powerlines. It will be run after each Powerlines command.
4740
- *
4741
- * @returns A promise that resolves when the finalization process has completed
4742
- */
4743
- async finalize() {
4744
- await this.#executeEnvironments(async (context) => {
4745
- await this.callHook("finalize", { environment: context });
4746
- await context.fs.dispose();
4747
- if ((0, _stryke_fs_exists.existsSync)(context.cachePath) && !(await (0, _stryke_fs_list_files.listFiles)((0, _stryke_path_join_paths.joinPaths)(context.cachePath, "**/*")))?.length) await (0, _stryke_fs_helpers.removeDirectory)(context.cachePath);
4748
- });
4749
- }
4750
- /**
4751
- * Invokes the configured plugin hooks
4752
- *
4753
- * @remarks
4754
- * By default, it will call the `"pre"`, `"normal"`, and `"post"` ordered hooks in sequence
4755
- *
4756
- * @param hook - The hook to call
4757
- * @param options - The options to provide to the hook
4758
- * @param args - The arguments to pass to the hook
4759
- * @returns The result of the hook call
4760
- */
4761
- async callHook(hook, options, ...args) {
4762
- return callHook((0, _stryke_type_checks_is_set_object.isSetObject)(options?.environment) ? options.environment : await this.#context.getEnvironment(options?.environment), hook, {
4763
- sequential: true,
4764
- ...options
4765
- }, ...args);
4766
- }
4767
- /**
4768
- * Create a new Powerlines API instance
4769
- *
4770
- * @param context - The Powerlines context
4771
- */
4772
- constructor(context) {
4773
- this.#context = context;
4774
- }
4775
- /**
4776
- * Add a Powerlines plugin used in the build process
4777
- *
4778
- * @param config - The import path of the plugin to add
4779
- */
4780
- async addPlugin(config) {
4781
- if (config) {
4782
- const result = await this.#initPlugin(config);
4783
- if (!result) return;
4784
- for (const plugin of result) {
4785
- this.context.debug(`Successfully initialized the ${chalk.default.bold.cyanBright(plugin.name)} plugin`);
4786
- await this.context.addPlugin(plugin);
4787
- }
4788
- }
4789
- }
4790
- };
4791
-
4792
- //#endregion
4793
- //#region src/_internal/worker.ts
4794
- async function clean({ options, config }) {
4795
- const execution = await PowerlinesExecution.fromConfig(options, config);
4796
- await execution.clean(config);
4797
- await execution.finalize();
4798
- }
4799
- async function prepare({ options, config }) {
4800
- const execution = await PowerlinesExecution.fromConfig(options, config);
4801
- await execution.prepare(config);
4802
- await execution.finalize();
4803
- }
4804
- async function types({ options, config }) {
4805
- const execution = await PowerlinesExecution.fromConfig(options, config);
4806
- await execution.types(config);
4807
- await execution.finalize();
4808
- }
4809
- async function lint({ options, config }) {
4810
- const execution = await PowerlinesExecution.fromConfig(options, config);
4811
- await execution.lint(config);
4812
- await execution.finalize();
4813
- }
4814
- async function test({ options, config }) {
4815
- const execution = await PowerlinesExecution.fromConfig(options, config);
4816
- await execution.test(config);
4817
- await execution.finalize();
4818
- }
4819
- async function build({ options, config }) {
4820
- const execution = await PowerlinesExecution.fromConfig(options, config);
4821
- await execution.build(config);
4822
- await execution.finalize();
4823
- }
4824
- async function docs({ options, config }) {
4825
- const execution = await PowerlinesExecution.fromConfig(options, config);
4826
- await execution.docs(config);
4827
- await execution.finalize();
4828
- }
4829
- async function deploy({ options, config }) {
4830
- const execution = await PowerlinesExecution.fromConfig(options, config);
4831
- await execution.deploy(config);
4832
- await execution.finalize();
4833
- }
4834
-
4835
- //#endregion
4836
- exports.build = build;
4837
- exports.clean = clean;
4838
- exports.deploy = deploy;
4839
- exports.docs = docs;
4840
- exports.lint = lint;
4841
- exports.prepare = prepare;
4842
- exports.test = test;
4843
- exports.types = types;