@keq-request/cli 5.0.0-alpha.29 → 5.0.0-alpha.31

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 (83) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/dist/chunk-7HHTHYRA.cjs +2895 -0
  3. package/dist/chunk-7HHTHYRA.cjs.map +1 -0
  4. package/dist/chunk-DZ2SC5HL.js +2895 -0
  5. package/dist/chunk-DZ2SC5HL.js.map +1 -0
  6. package/dist/chunk-HN7ZZFKC.js +19 -0
  7. package/dist/chunk-HN7ZZFKC.js.map +1 -0
  8. package/dist/chunk-JX74ZR2O.cjs +670 -0
  9. package/dist/chunk-JX74ZR2O.cjs.map +1 -0
  10. package/dist/chunk-OC36G3PF.cjs +19 -0
  11. package/dist/chunk-OC36G3PF.cjs.map +1 -0
  12. package/dist/chunk-XRZLO2PB.js +670 -0
  13. package/dist/chunk-XRZLO2PB.js.map +1 -0
  14. package/dist/cli.cjs +171 -2757
  15. package/dist/cli.cjs.map +1 -1
  16. package/dist/cli.js +161 -2724
  17. package/dist/cli.js.map +1 -1
  18. package/dist/compiler/compiler.d.ts +2 -0
  19. package/dist/compiler/compiler.d.ts.map +1 -1
  20. package/dist/index.cjs +8 -2777
  21. package/dist/index.cjs.map +1 -1
  22. package/dist/index.js +15 -2738
  23. package/dist/index.js.map +1 -1
  24. package/dist/plugins/body-fallback/constants/metadata-storage.d.ts +1 -0
  25. package/dist/plugins/body-fallback/constants/metadata-storage.d.ts.map +1 -1
  26. package/dist/plugins/body-fallback/index.d.ts.map +1 -1
  27. package/dist/plugins/chinese-to-pinyin/chinese-to-pinyin.plugin.d.ts.map +1 -1
  28. package/dist/plugins/chinese-to-pinyin/constants/metadata-storage.d.ts +1 -0
  29. package/dist/plugins/chinese-to-pinyin/constants/metadata-storage.d.ts.map +1 -1
  30. package/dist/plugins/download-http-file/constants/metadata-storage.d.ts +1 -0
  31. package/dist/plugins/download-http-file/constants/metadata-storage.d.ts.map +1 -1
  32. package/dist/plugins/download-http-file/download-http-file.plugin.d.ts.map +1 -1
  33. package/dist/plugins/download-local-file/constants/metadata-storage.d.ts +1 -0
  34. package/dist/plugins/download-local-file/constants/metadata-storage.d.ts.map +1 -1
  35. package/dist/plugins/download-local-file/download-local-file.plugin.d.ts.map +1 -1
  36. package/dist/plugins/eslint/constants/metadata-storage.d.ts +1 -0
  37. package/dist/plugins/eslint/constants/metadata-storage.d.ts.map +1 -1
  38. package/dist/plugins/eslint/eslint.plugin.d.ts.map +1 -1
  39. package/dist/plugins/generate-declaration/constants/metadata-storage.d.ts +2 -1
  40. package/dist/plugins/generate-declaration/constants/metadata-storage.d.ts.map +1 -1
  41. package/dist/plugins/generate-declaration/generate-declaration.plugin.d.ts.map +1 -1
  42. package/dist/plugins/generate-micro-function/constants/metadata-storage.d.ts +1 -0
  43. package/dist/plugins/generate-micro-function/constants/metadata-storage.d.ts.map +1 -1
  44. package/dist/plugins/generate-micro-function/generate-micro-function.plugin.d.ts.map +1 -1
  45. package/dist/plugins/generate-nestjs-module/constants/metadata-storage.d.ts +1 -0
  46. package/dist/plugins/generate-nestjs-module/constants/metadata-storage.d.ts.map +1 -1
  47. package/dist/plugins/generate-nestjs-module/generate-nestjs-module.d.ts.map +1 -1
  48. package/dist/plugins/index.d.ts +1 -0
  49. package/dist/plugins/index.d.ts.map +1 -1
  50. package/dist/plugins/prettier/constants/metadata-storage.d.ts +1 -0
  51. package/dist/plugins/prettier/constants/metadata-storage.d.ts.map +1 -1
  52. package/dist/plugins/prettier/prettier.plugin.d.ts.map +1 -1
  53. package/dist/plugins/shaking/constants/metadata-storage.d.ts +1 -0
  54. package/dist/plugins/shaking/constants/metadata-storage.d.ts.map +1 -1
  55. package/dist/plugins/shaking/shaking.plugin.d.ts.map +1 -1
  56. package/dist/plugins/terminal-select/constants/metadata-storage.d.ts +1 -0
  57. package/dist/plugins/terminal-select/constants/metadata-storage.d.ts.map +1 -1
  58. package/dist/plugins/terminal-select/terminal-select.plugin.d.ts.map +1 -1
  59. package/dist/plugins/use-valibot/constants/index.d.ts +2 -0
  60. package/dist/plugins/use-valibot/constants/index.d.ts.map +1 -0
  61. package/dist/plugins/use-valibot/constants/metadata-storage.d.ts +6 -0
  62. package/dist/plugins/use-valibot/constants/metadata-storage.d.ts.map +1 -0
  63. package/dist/plugins/use-valibot/index.d.ts +3 -0
  64. package/dist/plugins/use-valibot/index.d.ts.map +1 -0
  65. package/dist/plugins/use-valibot/use-valibot.plugin.d.ts +12 -0
  66. package/dist/plugins/use-valibot/use-valibot.plugin.d.ts.map +1 -0
  67. package/dist/plugins.cjs +3 -2532
  68. package/dist/plugins.cjs.map +1 -1
  69. package/dist/plugins.js +31 -2500
  70. package/dist/plugins.js.map +1 -1
  71. package/dist/transformers/json-schema/json-schema.transformer.d.ts +2 -0
  72. package/dist/transformers/json-schema/json-schema.transformer.d.ts.map +1 -1
  73. package/dist/transformers/json-schema/valibot.renderer.d.ts +26 -0
  74. package/dist/transformers/json-schema/valibot.renderer.d.ts.map +1 -0
  75. package/dist/transformers/schema-definition/schema-definition.transformer.d.ts +5 -0
  76. package/dist/transformers/schema-definition/schema-definition.transformer.d.ts.map +1 -1
  77. package/dist/translators.cjs +8 -2047
  78. package/dist/translators.cjs.map +1 -1
  79. package/dist/translators.js +7 -2008
  80. package/dist/translators.js.map +1 -1
  81. package/dist/utils/scan-generated-files.d.ts +15 -0
  82. package/dist/utils/scan-generated-files.d.ts.map +1 -0
  83. package/package.json +3 -3
@@ -0,0 +1,670 @@
1
+ import {
2
+ MicroFunctionTranslator
3
+ } from "./chunk-HN7ZZFKC.js";
4
+ import {
5
+ ApiDocumentV3_1,
6
+ Asset,
7
+ Exception,
8
+ FileNamingStyle,
9
+ InitializePlugin,
10
+ ModuleDefinition,
11
+ OpenapiUtils
12
+ } from "./chunk-DZ2SC5HL.js";
13
+
14
+ // src/compiler/compiler.ts
15
+ import { Listr } from "listr2";
16
+ import { AsyncParallelHook, AsyncSeriesBailHook, AsyncSeriesHook, SyncHook } from "tapable";
17
+
18
+ // src/compiler/tasks/setup/index.ts
19
+ import fs3 from "fs-extra";
20
+ import path2 from "path";
21
+ import { cosmiconfig } from "cosmiconfig";
22
+
23
+ // src/utils/ignore-matcher.ts
24
+ import * as R from "ramda";
25
+ import fs from "fs-extra";
26
+ var IgnoreMatcher = class _IgnoreMatcher {
27
+ rules;
28
+ constructor(rules) {
29
+ this.rules = rules;
30
+ }
31
+ static async read(filepath) {
32
+ let content = await fs.readFile(filepath, "utf-8");
33
+ content = content.replace(/\r\n/g, "\n").replace(/\r/g, "\n").replace(/#.*$/gm, "").replace(/\n+/g, "\n").trim();
34
+ let rules = content.split("\n").map((line) => line.trim()).filter((line) => line.length > 0).map((line) => {
35
+ const matched = line.match(/^(!)?\s*([^\s/]+)\s+([^\s/]+)\s+([^\s]+)$/);
36
+ if (!matched) throw new Error(`Invalid ignore rule: "${line}"`);
37
+ const [, flag, moduleName, operationMethod, operationPathname] = matched;
38
+ return {
39
+ persist: true,
40
+ ignore: !flag,
41
+ moduleName,
42
+ operationMethod: operationMethod.toLowerCase(),
43
+ operationPathname
44
+ };
45
+ });
46
+ rules = R.sortBy(
47
+ R.prop("ignore"),
48
+ rules
49
+ );
50
+ rules = R.uniqWith(
51
+ (a, b) => a.moduleName === b.moduleName && a.operationMethod === b.operationMethod && a.operationPathname === b.operationPathname,
52
+ rules
53
+ );
54
+ return new _IgnoreMatcher(rules);
55
+ }
56
+ async write(filepath) {
57
+ const ruleGroups = R.compose(
58
+ R.groupBy((rule) => rule.moduleName),
59
+ R.reverse,
60
+ R.uniqWith(
61
+ (a, b) => a.moduleName === b.moduleName && a.operationMethod === b.operationMethod && a.operationPathname === b.operationPathname
62
+ ),
63
+ R.filter((rule) => rule.persist)
64
+ )(this.rules);
65
+ const content = Object.entries(ruleGroups).sort((a, b) => {
66
+ const aModuleName = a[0];
67
+ const bModuleName = b[0];
68
+ if (aModuleName === "*") return -1;
69
+ if (bModuleName === "*") return 1;
70
+ return aModuleName.localeCompare(bModuleName);
71
+ }).map(([, rules]) => (rules || []).sort((a, b) => {
72
+ if (a.ignore !== b.ignore) return a.ignore ? -1 : 1;
73
+ if (a.operationMethod === "*") return -1;
74
+ if (b.operationMethod === "*") return 1;
75
+ if (a.operationPathname === "*") return -1;
76
+ if (b.operationPathname === "*") return 1;
77
+ if (a.operationMethod !== b.operationMethod) return a.operationMethod.localeCompare(b.operationMethod);
78
+ if (a.operationPathname !== b.operationPathname) return a.operationPathname.localeCompare(b.operationPathname);
79
+ return 0;
80
+ }).map((rule) => `${rule.ignore ? "" : "! "}${rule.moduleName} ${rule.operationMethod.toUpperCase()} ${rule.operationPathname}`).join("\n")).join("\n\n");
81
+ await fs.writeFile(filepath, content, "utf-8");
82
+ }
83
+ append(rule) {
84
+ this.rules.unshift(rule);
85
+ }
86
+ // if operation is ignored, return true
87
+ isOperationIgnored(operationDefinition) {
88
+ const moduleName = operationDefinition.module.name;
89
+ const operationMethod = operationDefinition.method.toLowerCase();
90
+ const operationPathname = operationDefinition.pathname;
91
+ for (const rule of this.rules) {
92
+ if (rule.moduleName !== "*" && rule.moduleName !== moduleName) continue;
93
+ if (rule.operationMethod !== "*" && rule.operationMethod !== operationMethod) continue;
94
+ if (rule.operationPathname !== "*" && rule.operationPathname !== operationPathname) continue;
95
+ return rule.ignore;
96
+ }
97
+ return false;
98
+ }
99
+ isModuleIgnored(moduleDefinition) {
100
+ const moduleName = moduleDefinition.name;
101
+ for (const rule of this.rules) {
102
+ if (!rule.ignore) {
103
+ if (rule.moduleName === "*" || rule.moduleName === moduleName) return false;
104
+ continue;
105
+ }
106
+ if (rule.operationMethod !== "*") continue;
107
+ if (rule.operationPathname !== "*") continue;
108
+ if (rule.moduleName === "*" || rule.moduleName === moduleName) return true;
109
+ }
110
+ return false;
111
+ }
112
+ };
113
+
114
+ // src/compiler/tasks/setup/utils/find-nearest-package-json.ts
115
+ import fs2 from "fs";
116
+ import path from "path";
117
+ function findNearestPackageJson(startDir = process.cwd()) {
118
+ let dir = startDir;
119
+ while (true) {
120
+ const pkgPath = path.join(dir, "package.json");
121
+ if (fs2.existsSync(pkgPath)) {
122
+ const json = JSON.parse(fs2.readFileSync(pkgPath, "utf8"));
123
+ return { json, path: pkgPath };
124
+ }
125
+ const parent = path.dirname(dir);
126
+ if (parent === dir) break;
127
+ dir = parent;
128
+ }
129
+ return null;
130
+ }
131
+
132
+ // src/compiler/tasks/setup/utils/get-project-module-system.ts
133
+ function getProjectModuleSystem(pkgInfo) {
134
+ if (!pkgInfo?.json) return "cjs";
135
+ const { json } = pkgInfo;
136
+ if (json.type === "module") return "esm";
137
+ return "cjs";
138
+ }
139
+
140
+ // src/compiler/tasks/setup/utils/parse-runtime-config.ts
141
+ import { Value } from "@sinclair/typebox/value";
142
+
143
+ // src/types/address.ts
144
+ import { Type } from "@sinclair/typebox";
145
+ var Address = Type.Object({
146
+ url: Type.String(),
147
+ headers: Type.Optional(Type.Record(Type.String(), Type.String(), { default: {} })),
148
+ encoding: Type.Optional(
149
+ Type.Union([
150
+ Type.Literal("utf8"),
151
+ Type.Literal("ascii")
152
+ ], { default: "utf8" })
153
+ )
154
+ });
155
+
156
+ // src/types/runtime-config.ts
157
+ import * as R2 from "ramda";
158
+ import { Type as Type2 } from "@sinclair/typebox";
159
+
160
+ // src/utils/is-valid-url.ts
161
+ var URL_REGEX = /^[a-zA-Z][a-zA-Z0-9+.-]*:\/\/(?:[^\s@]+@)?[^\s/:?#]*(?::\d+)?(?:\/[^\s?#]*)?(?:\?[^\s#]*)?(?:#[^\s]*)?$/;
162
+ function isValidURL(url) {
163
+ return URL_REGEX.test(url);
164
+ }
165
+
166
+ // src/types/runtime-config.ts
167
+ var Modules = Type2.Transform(
168
+ Type2.Record(
169
+ Type2.String(),
170
+ Type2.Union([Type2.String(), Address])
171
+ )
172
+ ).Decode((value) => {
173
+ const keys = Object.keys(value);
174
+ for (const key of keys) {
175
+ if (!/^[A-Za-z_][A-Za-z0-9_$]*$/.test(key)) {
176
+ throw new Error(`Module name "${key}" is not valid. It must start with a letter or underscore, and can only contain letters, numbers, and underscores.`);
177
+ }
178
+ }
179
+ const keysGroupByLowerCase = R2.groupBy(R2.toLower, keys);
180
+ for (const groupKey in keysGroupByLowerCase) {
181
+ const keys2 = keysGroupByLowerCase[groupKey] || [];
182
+ if (keys2.length > 1) {
183
+ throw new Error(`Module names ${keys2.map((name) => `"${name}"`).join(", ")} are case-insensitively duplicated.`);
184
+ }
185
+ }
186
+ for (const key in value) {
187
+ const url = typeof value[key] === "string" ? value[key] : value[key].url;
188
+ if (isValidURL(url)) continue;
189
+ throw new Error(`The ${JSON.stringify(url)} of module "${key}" is not a valid URL.`);
190
+ }
191
+ return R2.map(
192
+ (item) => typeof item !== "string" ? item : { url: item, headers: {}, encoding: "utf8" },
193
+ value
194
+ );
195
+ }).Encode((value) => value);
196
+ var RawConfig = Type2.Object({
197
+ /**
198
+ * Whether to generate ES Module code
199
+ *
200
+ * If not specified, the module system will be inferred from the nearest package.json "type" field
201
+ * or defaults to "cjs" if no package.json is found.
202
+ */
203
+ esm: Type2.Optional(Type2.Boolean({ default: false })),
204
+ /**
205
+ * Output directory for generated files
206
+ */
207
+ outdir: Type2.String({ default: `${process.cwd()}/api` }),
208
+ /**
209
+ * File naming style for generated files
210
+ */
211
+ fileNamingStyle: Type2.Enum(FileNamingStyle, { default: "snakeCase" /* snakeCase */ }),
212
+ /**
213
+ * OpenAPI/Swagger document sources
214
+ *
215
+ * A map of module names to their document URLs or Address objects.
216
+ * The module name must be a valid JavaScript identifier.
217
+ */
218
+ modules: Modules,
219
+ /**
220
+ * Enable debug mode to output detailed logs during compilation
221
+ */
222
+ debug: Type2.Optional(Type2.Boolean({ default: false })),
223
+ /**
224
+ * Whether to tolerate wrong openapi/swagger structure
225
+ */
226
+ tolerant: Type2.Optional(Type2.Boolean({ default: false })),
227
+ /**
228
+ * Translators to transform generated code
229
+ *
230
+ * Used to customize the code generation process for different frameworks or patterns.
231
+ * Can be a single translator or an array of translators.
232
+ */
233
+ translators: Type2.Optional(
234
+ Type2.Transform(
235
+ Type2.Union([
236
+ Type2.Unsafe(Type2.Any()),
237
+ Type2.Array(Type2.Unsafe(Type2.Any()))
238
+ ], { default: [] })
239
+ ).Decode((value) => {
240
+ if (value === void 0) return [];
241
+ if (Array.isArray(value)) return value;
242
+ return [value];
243
+ }).Encode((value) => value)
244
+ ),
245
+ /**
246
+ * Plugins to extend code generation functionality
247
+ *
248
+ * An array of plugin instances that can hook into various stages of the compilation process,
249
+ * such as code transformation, formatting, linting, or custom file generation.
250
+ */
251
+ plugins: Type2.Optional(Type2.Array(Type2.Unsafe(Type2.Any()), { default: [] }))
252
+ });
253
+
254
+ // src/compiler/tasks/setup/utils/parse-runtime-config.ts
255
+ function parseRuntimeConfig(data) {
256
+ try {
257
+ const originalPlugins = typeof data === "object" && data !== null && "plugins" in data ? data.plugins : void 0;
258
+ const originTranslators = typeof data === "object" && data !== null && "translators" in data ? data.translators : void 0;
259
+ const parsed = Value.Parse(RawConfig, data);
260
+ if (originalPlugins !== void 0) {
261
+ parsed.plugins = originalPlugins;
262
+ }
263
+ if (originTranslators !== void 0) {
264
+ parsed.translators = originTranslators;
265
+ }
266
+ return parsed;
267
+ } catch (error) {
268
+ if (error instanceof Error) {
269
+ error.message = `Invalid Config: ${error.message}`;
270
+ }
271
+ throw error;
272
+ }
273
+ }
274
+
275
+ // src/compiler/tasks/setup/index.ts
276
+ var explore = cosmiconfig("keq");
277
+ function main(compiler, options) {
278
+ return {
279
+ task: async (context, task) => {
280
+ const result = options?.config ? await explore.load(options.config) : await explore.search();
281
+ if (!result || "isEmpty" in result && result.isEmpty) {
282
+ throw new Error("Cannot find config file.");
283
+ }
284
+ const rc = parseRuntimeConfig(result.config);
285
+ if (options?.debug) {
286
+ await fs3.ensureDir(".keq");
287
+ rc.debug = true;
288
+ }
289
+ rc.tolerant = Boolean(rc.tolerant);
290
+ if (!rc.translators || !rc.translators.length) {
291
+ rc.translators = [new MicroFunctionTranslator()];
292
+ }
293
+ const packageJsonInfo = findNearestPackageJson();
294
+ if (packageJsonInfo) {
295
+ const moduleSystem = getProjectModuleSystem(packageJsonInfo);
296
+ rc.esm = moduleSystem === "esm";
297
+ }
298
+ context.rc = rc;
299
+ let matcher = new IgnoreMatcher([]);
300
+ if (result.filepath) {
301
+ const ignoreFilepath = path2.resolve(path2.dirname(result.filepath), ".keqignore");
302
+ if (await fs3.exists(ignoreFilepath)) {
303
+ matcher = await IgnoreMatcher.read(ignoreFilepath);
304
+ }
305
+ }
306
+ const ignoreRules = options.ignore === false ? [] : options.ignore?.rules || [];
307
+ for (const rule of ignoreRules) {
308
+ matcher.append({
309
+ persist: !!rule.persist,
310
+ ignore: rule.ignore,
311
+ moduleName: rule.moduleName,
312
+ operationMethod: rule.operationMethod,
313
+ operationPathname: rule.operationPathname
314
+ });
315
+ }
316
+ context.matcher = matcher;
317
+ await compiler.hooks.setup.promise(task);
318
+ }
319
+ };
320
+ }
321
+ function createSetupTask(compiler, options) {
322
+ return {
323
+ title: "Setup",
324
+ enabled: options?.enabled,
325
+ skip: options?.skip,
326
+ task: (context, task) => task.newListr(
327
+ [
328
+ main(compiler, options),
329
+ {
330
+ task: (context2, task2) => compiler.hooks.afterSetup.promise(task2)
331
+ }
332
+ ],
333
+ {
334
+ concurrent: false
335
+ }
336
+ )
337
+ };
338
+ }
339
+
340
+ // src/compiler/tasks/download/index.ts
341
+ import { PRESET_TIMER } from "listr2";
342
+ import { validate } from "@scalar/openapi-parser";
343
+ function main2(compiler, options) {
344
+ return {
345
+ task: (context, task) => {
346
+ if (!context.rc || !context.matcher) {
347
+ throw new Error("Please run setup task first.");
348
+ }
349
+ const rc = context.rc;
350
+ const matcher = context.matcher;
351
+ context.documents = [];
352
+ return task.newListr(
353
+ Object.entries(rc.modules).map(([moduleName, address]) => new ModuleDefinition(moduleName, address)).map((moduleDefinition) => ({
354
+ title: moduleDefinition.name,
355
+ task: async (ctx, task2) => {
356
+ if (options?.skipIgnoredModules && matcher.isModuleIgnored(moduleDefinition)) {
357
+ task2.skip(`(${moduleDefinition.name}) is ignored`);
358
+ return;
359
+ }
360
+ task2.output = `Downloaded from ${moduleDefinition.address.url}`;
361
+ const content = await compiler.hooks.download.promise(moduleDefinition.address, moduleDefinition, task2);
362
+ if (!content) {
363
+ throw new Exception(moduleDefinition, `Cannot download document from ${moduleDefinition.address.url}`);
364
+ }
365
+ const spec = JSON.parse(content);
366
+ const { valid, errors } = await validate(spec);
367
+ if (!valid) {
368
+ const message = `${moduleDefinition.name} module openapi/swagger file does not conform to the openapi specifications or have grammatical errors, which may cause unexpected errors:
369
+ ${errors?.map((e) => ` - ${e.message}`).join("\n")}`;
370
+ task2.output = message;
371
+ }
372
+ OpenapiUtils.dereferenceOperation(spec);
373
+ const document = new ApiDocumentV3_1(
374
+ spec,
375
+ moduleDefinition
376
+ );
377
+ ctx.documents?.push(document);
378
+ }
379
+ })),
380
+ {
381
+ concurrent: true,
382
+ exitOnError: false,
383
+ collectErrors: "minimal",
384
+ rendererOptions: {
385
+ collapseSubtasks: false,
386
+ // collapseSkips: false,
387
+ suffixSkips: true,
388
+ timer: PRESET_TIMER
389
+ }
390
+ }
391
+ );
392
+ }
393
+ };
394
+ }
395
+ function createDownloadTask(compiler, options) {
396
+ return {
397
+ title: "Download",
398
+ enabled: options?.enabled,
399
+ skip: options?.skip,
400
+ task: (_, task) => task.newListr(
401
+ [
402
+ main2(compiler, options),
403
+ {
404
+ task: (context, task2) => compiler.hooks.afterDownload.promise(task2)
405
+ }
406
+ ],
407
+ {
408
+ concurrent: false
409
+ }
410
+ )
411
+ };
412
+ }
413
+
414
+ // src/compiler/tasks/persist/index.ts
415
+ import * as path3 from "path";
416
+ import fs4 from "fs-extra";
417
+ function createPersistArtifactTask() {
418
+ return {
419
+ title: "Write files",
420
+ task: async (context, task) => {
421
+ if (!context.rc) throw new Error("Please run setup task first.");
422
+ if (!context.artifacts || context.artifacts.length === 0) {
423
+ task.skip("No compiled artifacts to persist.");
424
+ return;
425
+ }
426
+ const rc = context.rc;
427
+ const artifacts = context.artifacts;
428
+ const total = artifacts.length;
429
+ let completed = 0;
430
+ const files = await Promise.all(artifacts.map(async (artifact) => {
431
+ const realpath = `./${path3.join(rc.outdir, artifact.filepath)}`;
432
+ await fs4.ensureFile(realpath);
433
+ await fs4.writeFile(realpath, artifact.renderer());
434
+ completed += 1;
435
+ task.output = `Persisted ${completed}/${total} files`;
436
+ return new Asset(path3.resolve(realpath));
437
+ }));
438
+ context.assets = files;
439
+ }
440
+ };
441
+ }
442
+ function createPersistIgnoreTask() {
443
+ return {
444
+ title: "Update .keqignore",
445
+ task: async (context, task) => {
446
+ if (!context.matcher) throw new Error("Please run setup task first.");
447
+ const matcher = context.matcher;
448
+ await matcher.write(".keqignore");
449
+ }
450
+ };
451
+ }
452
+ function main3() {
453
+ return {
454
+ task: (context, task) => task.newListr(
455
+ [
456
+ createPersistArtifactTask(),
457
+ createPersistIgnoreTask()
458
+ ],
459
+ {
460
+ concurrent: true,
461
+ rendererOptions: {
462
+ collapseSubtasks: true
463
+ }
464
+ }
465
+ )
466
+ };
467
+ }
468
+ function createPersistTask(compiler, options) {
469
+ return {
470
+ title: "Persist",
471
+ enabled: options?.enabled,
472
+ skip: options?.skip,
473
+ task: (context, task) => task.newListr(
474
+ [
475
+ main3(),
476
+ {
477
+ task: (context2, task2) => compiler.hooks.afterPersist.promise(task2)
478
+ }
479
+ ],
480
+ {
481
+ concurrent: false
482
+ }
483
+ )
484
+ };
485
+ }
486
+
487
+ // src/compiler/tasks/compile/index.ts
488
+ function main4(compiler) {
489
+ return {
490
+ task: async (context, task) => {
491
+ if (!context.rc) throw new Error("Please run setup task first.");
492
+ if (!context.documents) throw new Error("Please run shaking task first.");
493
+ context.artifacts = [];
494
+ await compiler.hooks.compile.promise(task);
495
+ }
496
+ };
497
+ }
498
+ function createCompileTask(compiler, options) {
499
+ return {
500
+ title: "Compile",
501
+ enabled: options?.enabled,
502
+ skip: options?.skip,
503
+ task: (context, task) => task.newListr(
504
+ [
505
+ {
506
+ task: (context2, task2) => compiler.hooks.beforeCompile.promise(task2)
507
+ },
508
+ main4(compiler),
509
+ {
510
+ task: (context2, task2) => compiler.hooks.afterCompile.promise(task2)
511
+ }
512
+ ],
513
+ {
514
+ concurrent: false
515
+ }
516
+ )
517
+ };
518
+ }
519
+
520
+ // src/compiler/intercepter/print-information.ts
521
+ function proxyTaskWrapper(pluginName, task) {
522
+ return new Proxy(task, {
523
+ set(target, prop2, value) {
524
+ if (prop2 !== "output") {
525
+ return Reflect.set(target, prop2, value);
526
+ }
527
+ target.output = `[Plugin: ${pluginName}] ${value}`;
528
+ return true;
529
+ }
530
+ });
531
+ }
532
+ function printInformation(taskIndex) {
533
+ return {
534
+ register: (tap) => {
535
+ const fn = tap.fn;
536
+ if (tap.type === "promise") {
537
+ tap.fn = (...args) => {
538
+ const task = args[taskIndex];
539
+ const proxyTask = proxyTaskWrapper(tap.name, task);
540
+ args[taskIndex] = proxyTask;
541
+ proxyTask.output = "Processing...";
542
+ return fn(...args);
543
+ };
544
+ }
545
+ if (tap.type === "sync") {
546
+ tap.fn = (...args) => {
547
+ const task = args[taskIndex];
548
+ const proxyTask = proxyTaskWrapper(tap.name, task);
549
+ args[taskIndex] = proxyTask;
550
+ proxyTask.output = "Processing...";
551
+ return fn(...args);
552
+ };
553
+ }
554
+ if (tap.type === "async") {
555
+ tap.fn = (...args) => {
556
+ const task = args[taskIndex];
557
+ const proxyTask = proxyTaskWrapper(tap.name, task);
558
+ args[taskIndex] = proxyTask;
559
+ proxyTask.output = "Processing...";
560
+ return fn(...args);
561
+ };
562
+ }
563
+ return tap;
564
+ }
565
+ };
566
+ }
567
+
568
+ // src/compiler/intercepter/perfect-error-message.ts
569
+ import * as R3 from "ramda";
570
+ function perfectErrorMessage() {
571
+ return {
572
+ register: (tap) => {
573
+ const fn = tap.fn;
574
+ function prefix(err) {
575
+ if (err instanceof Error) {
576
+ err.message = `[Plugin: ${tap.name}] ${err.message}`;
577
+ }
578
+ }
579
+ if (tap.type === "promise") {
580
+ tap.fn = async (...args) => {
581
+ try {
582
+ return await fn(...args);
583
+ } catch (err) {
584
+ prefix(err);
585
+ throw err;
586
+ }
587
+ };
588
+ }
589
+ if (tap.type === "sync") {
590
+ tap.fn = (...args) => {
591
+ try {
592
+ return fn(...args);
593
+ } catch (err) {
594
+ prefix(err);
595
+ throw err;
596
+ }
597
+ };
598
+ }
599
+ if (tap.type === "async") {
600
+ tap.fn = (...args) => {
601
+ const callback = R3.last(args);
602
+ return fn(...R3.init(args), (err, result) => {
603
+ prefix(err);
604
+ return callback(err, result);
605
+ });
606
+ };
607
+ }
608
+ return tap;
609
+ }
610
+ };
611
+ }
612
+
613
+ // src/compiler/compiler.ts
614
+ var Compiler = class {
615
+ constructor(options) {
616
+ this.options = options;
617
+ for (const hook of Object.values(this.hooks)) {
618
+ hook.intercept(perfectErrorMessage());
619
+ }
620
+ this.hooks.afterSetup.intercept(printInformation(0));
621
+ this.hooks.afterPersist.intercept(printInformation(0));
622
+ new InitializePlugin({
623
+ build: options.build,
624
+ interactive: options.interactive,
625
+ includes: options.includes
626
+ }).apply(this);
627
+ }
628
+ context = {};
629
+ hooks = {
630
+ setup: new AsyncParallelHook(["task"]),
631
+ afterSetup: new AsyncSeriesHook(["task"]),
632
+ beforeDownload: new AsyncSeriesHook(["task"]),
633
+ download: new AsyncSeriesBailHook(["address", "moduleDefinition", "task"]),
634
+ afterDownload: new AsyncSeriesHook(["task"]),
635
+ beforeCompile: new AsyncSeriesHook(["task"]),
636
+ compile: new AsyncParallelHook(["task"]),
637
+ afterCompile: new AsyncSeriesHook(["task"]),
638
+ beforePersist: new AsyncSeriesHook(["task"]),
639
+ persist: new AsyncParallelHook(["task"]),
640
+ afterPersist: new AsyncSeriesHook(["task"]),
641
+ done: new SyncHook()
642
+ };
643
+ async run() {
644
+ const options = this.options;
645
+ const tasks = new Listr(
646
+ [
647
+ createSetupTask(this, options),
648
+ createDownloadTask(this, { skipIgnoredModules: !options.interactive }),
649
+ createCompileTask(this, { enabled: !!options.build }),
650
+ createPersistTask(this, { enabled: !!options.persist })
651
+ ],
652
+ {
653
+ concurrent: false,
654
+ renderer: options.silent ? "silent" : "default",
655
+ rendererOptions: {
656
+ suffixSkips: true,
657
+ collapseSubtasks: false,
658
+ collapseErrors: false
659
+ }
660
+ }
661
+ );
662
+ await tasks.run(this.context);
663
+ await this.hooks.done.promise();
664
+ }
665
+ };
666
+
667
+ export {
668
+ Compiler
669
+ };
670
+ //# sourceMappingURL=chunk-XRZLO2PB.js.map