@kubb/cli 4.22.0 → 4.22.2

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 (47) hide show
  1. package/dist/{generate-Bjj-OR20.js → generate-CBsaUUvV.js} +49 -674
  2. package/dist/generate-CBsaUUvV.js.map +1 -0
  3. package/dist/{generate-BJyvkuLI.cjs → generate-CLDVb4hB.cjs} +53 -678
  4. package/dist/generate-CLDVb4hB.cjs.map +1 -0
  5. package/dist/getCosmiConfig-Co29x0Wv.cjs +458 -0
  6. package/dist/getCosmiConfig-Co29x0Wv.cjs.map +1 -0
  7. package/dist/getCosmiConfig-y2n_oW_y.js +438 -0
  8. package/dist/getCosmiConfig-y2n_oW_y.js.map +1 -0
  9. package/dist/index.cjs +9 -7
  10. package/dist/index.cjs.map +1 -1
  11. package/dist/index.js +9 -7
  12. package/dist/index.js.map +1 -1
  13. package/dist/{init-CxLDdq2k.cjs → init-Bs1GxKWV.cjs} +3 -3
  14. package/dist/{init-CxLDdq2k.cjs.map → init-Bs1GxKWV.cjs.map} +1 -1
  15. package/dist/{init-D37jPiGV.js → init-D3zCeqU-.js} +3 -3
  16. package/dist/{init-D37jPiGV.js.map → init-D3zCeqU-.js.map} +1 -1
  17. package/dist/{mcp-By2JUPa8.js → mcp-BdwwUv36.js} +1 -1
  18. package/dist/{mcp-By2JUPa8.js.map → mcp-BdwwUv36.js.map} +1 -1
  19. package/dist/{mcp-l-_Z-WU6.cjs → mcp-Hy_PYnFp.cjs} +1 -1
  20. package/dist/{mcp-l-_Z-WU6.cjs.map → mcp-Hy_PYnFp.cjs.map} +1 -1
  21. package/dist/package-BFidNEkS.js +6 -0
  22. package/dist/package-BFidNEkS.js.map +1 -0
  23. package/dist/{package-Cnl_fuIC.cjs → package-CiB5tqr0.cjs} +2 -2
  24. package/dist/package-CiB5tqr0.cjs.map +1 -0
  25. package/dist/start-BeoZd1fL.cjs +128 -0
  26. package/dist/start-BeoZd1fL.cjs.map +1 -0
  27. package/dist/start-Ohz4V8k2.js +125 -0
  28. package/dist/start-Ohz4V8k2.js.map +1 -0
  29. package/dist/{validate-DkHCd-Tl.cjs → validate-Cvb5aOEb.cjs} +1 -1
  30. package/dist/{validate-DkHCd-Tl.cjs.map → validate-Cvb5aOEb.cjs.map} +1 -1
  31. package/dist/{validate-0Qu8SxVA.js → validate-YI4YkVTl.js} +1 -1
  32. package/dist/{validate-0Qu8SxVA.js.map → validate-YI4YkVTl.js.map} +1 -1
  33. package/package.json +4 -4
  34. package/src/commands/generate.ts +0 -38
  35. package/src/commands/init.ts +3 -1
  36. package/src/commands/start.ts +149 -0
  37. package/src/index.ts +2 -1
  38. package/src/loggers/clackLogger.ts +32 -12
  39. package/src/loggers/githubActionsLogger.ts +21 -8
  40. package/src/loggers/plainLogger.ts +20 -8
  41. package/dist/generate-BJyvkuLI.cjs.map +0 -1
  42. package/dist/generate-Bjj-OR20.js.map +0 -1
  43. package/dist/package-Cnl_fuIC.cjs.map +0 -1
  44. package/dist/package-Qh9BQXK6.js +0 -6
  45. package/dist/package-Qh9BQXK6.js.map +0 -1
  46. package/src/loggers/streamLogger.ts +0 -93
  47. package/src/utils/streamServer.ts +0 -163
@@ -1,26 +1,21 @@
1
1
  import { t as __name } from "./chunk-jHaXqnEa.js";
2
- import { t as version } from "./package-Qh9BQXK6.js";
2
+ import { t as version } from "./package-BFidNEkS.js";
3
+ import { n as getConfigs, r as generate, t as getCosmiConfig } from "./getCosmiConfig-y2n_oW_y.js";
3
4
  import { defineCommand, showUsage } from "citty";
4
5
  import path, { relative, resolve } from "node:path";
5
6
  import * as process$2 from "node:process";
6
7
  import process$1 from "node:process";
7
8
  import * as clack from "@clack/prompts";
8
- import { LogLevel, PromiseManager, defineLogger, isInputPath, safeBuild, setup } from "@kubb/core";
9
- import { AsyncEventEmitter, executeIfOnline, formatHrtime, formatMs, isPromise, serializePluginOptions } from "@kubb/core/utils";
9
+ import { LogLevel, PromiseManager, defineLogger, isInputPath } from "@kubb/core";
10
+ import { AsyncEventEmitter, executeIfOnline, formatHrtime, formatMs } from "@kubb/core/utils";
10
11
  import getLatestVersion from "latest-version";
11
12
  import pc from "picocolors";
12
13
  import { lt } from "semver";
13
- import { execa, execaCommand } from "execa";
14
+ import { execa } from "execa";
14
15
  import gradientString from "gradient-string";
15
16
  import seedrandom from "seedrandom";
16
17
  import { Writable } from "node:stream";
17
18
  import { write } from "@kubb/core/fs";
18
- import { createHash } from "node:crypto";
19
- import { parseArgsStringToArgv } from "string-argv";
20
- import { cosmiconfig } from "cosmiconfig";
21
- import { createJiti } from "jiti";
22
- import { readFileSync } from "node:fs";
23
- import { createServer } from "node:http";
24
19
 
25
20
  //#region src/utils/formatMsWithColor.ts
26
21
  /**
@@ -436,25 +431,30 @@ Run \`npm install -g @kubb/cli\` to update`, "Update available for `Kubb`", {
436
431
  error: null
437
432
  });
438
433
  } catch (err) {
439
- const error = /* @__PURE__ */ new Error("Hook execute failed");
440
- error.cause = err;
434
+ const error = err;
435
+ const stderr = typeof error.stderr === "string" ? error.stderr : String(error.stderr);
436
+ const stdout = typeof error.stdout === "string" ? error.stdout : String(error.stdout);
441
437
  await context.emit("debug", {
442
438
  date: /* @__PURE__ */ new Date(),
443
- logs: [err.stdout]
439
+ logs: [stdout, stderr].filter(Boolean)
444
440
  });
441
+ if (stderr) console.error(stderr);
442
+ if (stdout) console.log(stdout);
443
+ const errorMessage = /* @__PURE__ */ new Error(`Hook execute failed: ${commandWithArgs}`);
445
444
  await context.emit("hook:end", {
446
445
  command: command$1,
447
446
  args,
448
447
  id,
449
448
  success: false,
450
- error
449
+ error: errorMessage
451
450
  });
452
- await context.emit("error", error);
451
+ await context.emit("error", errorMessage);
453
452
  }
454
453
  return;
455
454
  }
456
455
  clack.intro(text);
457
- const writable = new ClackWritable(clack.taskLog({ title: getMessage(["Executing hook", logLevel >= LogLevel.info ? pc.dim(commandWithArgs) : void 0].filter(Boolean).join(" ")) }));
456
+ const logger = clack.taskLog({ title: getMessage(["Executing hook", logLevel >= LogLevel.info ? pc.dim(commandWithArgs) : void 0].filter(Boolean).join(" ")) });
457
+ const writable = new ClackWritable(logger);
458
458
  try {
459
459
  const result = await execa(command$1, args, {
460
460
  detached: true,
@@ -473,20 +473,24 @@ Run \`npm install -g @kubb/cli\` to update`, "Update available for `Kubb`", {
473
473
  error: null
474
474
  });
475
475
  } catch (err) {
476
- const error = /* @__PURE__ */ new Error("Hook execute failed");
477
- error.cause = err;
476
+ const error = err;
477
+ const stderr = typeof error.stderr === "string" ? error.stderr : String(error.stderr);
478
+ const stdout = typeof error.stdout === "string" ? error.stdout : String(error.stdout);
478
479
  await context.emit("debug", {
479
480
  date: /* @__PURE__ */ new Date(),
480
- logs: [err.stdout]
481
+ logs: [stdout, stderr].filter(Boolean)
481
482
  });
483
+ if (stderr) logger.error(stderr);
484
+ if (stdout) logger.message(stdout);
485
+ const errorMessage = /* @__PURE__ */ new Error(`Hook execute failed: ${commandWithArgs}`);
482
486
  await context.emit("hook:end", {
483
487
  command: command$1,
484
488
  args,
485
489
  id,
486
- success: true,
487
- error
490
+ success: false,
491
+ error: errorMessage
488
492
  });
489
- await context.emit("error", error);
493
+ await context.emit("error", errorMessage);
490
494
  }
491
495
  });
492
496
  context.on("hook:end", ({ command: command$1, args }) => {
@@ -879,7 +883,7 @@ const githubActionsLogger = defineLogger({
879
883
  date: /* @__PURE__ */ new Date(),
880
884
  logs: [result.stdout]
881
885
  });
882
- console.log(result.stdout);
886
+ if (logLevel > LogLevel.silent) console.log(result.stdout);
883
887
  await context.emit("hook:end", {
884
888
  command: command$1,
885
889
  args,
@@ -888,20 +892,24 @@ const githubActionsLogger = defineLogger({
888
892
  error: null
889
893
  });
890
894
  } catch (err) {
891
- const error = /* @__PURE__ */ new Error("Hook execute failed");
892
- error.cause = err;
895
+ const error = err;
896
+ const stderr = typeof error.stderr === "string" ? error.stderr : String(error.stderr);
897
+ const stdout = typeof error.stdout === "string" ? error.stdout : String(error.stdout);
893
898
  await context.emit("debug", {
894
899
  date: /* @__PURE__ */ new Date(),
895
- logs: [err.stdout]
900
+ logs: [stdout, stderr].filter(Boolean)
896
901
  });
902
+ if (stderr) console.error(`::error::${stderr}`);
903
+ if (stdout) console.log(stdout);
904
+ const errorMessage = /* @__PURE__ */ new Error(`Hook execute failed: ${commandWithArgs}`);
897
905
  await context.emit("hook:end", {
898
906
  command: command$1,
899
907
  args,
900
908
  id,
901
909
  success: false,
902
- error
910
+ error: errorMessage
903
911
  });
904
- await context.emit("error", error);
912
+ await context.emit("error", errorMessage);
905
913
  }
906
914
  });
907
915
  context.on("hook:end", ({ command: command$1, args }) => {
@@ -1050,7 +1058,8 @@ const plainLogger = defineLogger({
1050
1058
  console.log(text);
1051
1059
  });
1052
1060
  context.on("hook:start", async ({ id, command: command$1, args }) => {
1053
- const text = getMessage(`Hook ${args?.length ? `${command$1} ${args.join(" ")}` : command$1} started`);
1061
+ const commandWithArgs = args?.length ? `${command$1} ${args.join(" ")}` : command$1;
1062
+ const text = getMessage(`Hook ${commandWithArgs} started`);
1054
1063
  if (logLevel > LogLevel.silent) console.log(text);
1055
1064
  if (!id) return;
1056
1065
  try {
@@ -1062,7 +1071,7 @@ const plainLogger = defineLogger({
1062
1071
  date: /* @__PURE__ */ new Date(),
1063
1072
  logs: [result.stdout]
1064
1073
  });
1065
- console.log(result.stdout);
1074
+ if (logLevel > LogLevel.silent) console.log(result.stdout);
1066
1075
  await context.emit("hook:end", {
1067
1076
  command: command$1,
1068
1077
  args,
@@ -1071,20 +1080,24 @@ const plainLogger = defineLogger({
1071
1080
  error: null
1072
1081
  });
1073
1082
  } catch (err) {
1074
- const error = /* @__PURE__ */ new Error("Hook execute failed");
1075
- error.cause = err;
1083
+ const error = err;
1084
+ const stderr = typeof error.stderr === "string" ? error.stderr : String(error.stderr);
1085
+ const stdout = typeof error.stdout === "string" ? error.stdout : String(error.stdout);
1076
1086
  await context.emit("debug", {
1077
1087
  date: /* @__PURE__ */ new Date(),
1078
- logs: [err.stdout]
1088
+ logs: [stdout, stderr].filter(Boolean)
1079
1089
  });
1090
+ if (stderr) console.error(stderr);
1091
+ if (stdout) console.log(stdout);
1092
+ const errorMessage = /* @__PURE__ */ new Error(`Hook execute failed: ${commandWithArgs}`);
1080
1093
  await context.emit("hook:end", {
1081
1094
  command: command$1,
1082
1095
  args,
1083
1096
  id,
1084
1097
  success: false,
1085
- error
1098
+ error: errorMessage
1086
1099
  });
1087
- await context.emit("error", error);
1100
+ await context.emit("error", errorMessage);
1088
1101
  }
1089
1102
  });
1090
1103
  context.on("hook:end", ({ command: command$1, args }) => {
@@ -1129,615 +1142,6 @@ async function setupLogger(context, { logLevel }) {
1129
1142
  return cleanup;
1130
1143
  }
1131
1144
 
1132
- //#endregion
1133
- //#region src/utils/detectFormatter.ts
1134
- /**
1135
- * Check if a formatter command is available in the system.
1136
- *
1137
- * @param formatter - The formatter to check ('biome', 'prettier', or 'oxfmt')
1138
- * @returns Promise that resolves to true if the formatter is available, false otherwise
1139
- *
1140
- * @remarks
1141
- * This function checks availability by running `<formatter> --version` command.
1142
- * All supported formatters (biome, prettier, oxfmt) implement the --version flag.
1143
- */
1144
- async function isFormatterAvailable(formatter) {
1145
- try {
1146
- await execaCommand(`${formatter} --version`, { stdio: "ignore" });
1147
- return true;
1148
- } catch {
1149
- return false;
1150
- }
1151
- }
1152
- /**
1153
- * Detect which formatter is available in the system.
1154
- *
1155
- * @returns Promise that resolves to the first available formatter or undefined if none are found
1156
- *
1157
- * @remarks
1158
- * Checks in order of preference: biome, oxfmt, prettier.
1159
- * Uses the `--version` flag to detect if each formatter command is available.
1160
- * This is a reliable method as all supported formatters implement this flag.
1161
- *
1162
- * @example
1163
- * ```typescript
1164
- * const formatter = await detectFormatter()
1165
- * if (formatter) {
1166
- * console.log(`Using ${formatter} for formatting`)
1167
- * } else {
1168
- * console.log('No formatter found')
1169
- * }
1170
- * ```
1171
- */
1172
- async function detectFormatter() {
1173
- for (const formatter of [
1174
- "biome",
1175
- "oxfmt",
1176
- "prettier"
1177
- ]) if (await isFormatterAvailable(formatter)) return formatter;
1178
- }
1179
-
1180
- //#endregion
1181
- //#region src/utils/detectLinter.ts
1182
- /**
1183
- * Check if a linter command is available in the system.
1184
- *
1185
- * @param linter - The linter to check ('biome', 'oxlint', or 'eslint')
1186
- * @returns Promise that resolves to true if the linter is available, false otherwise
1187
- *
1188
- * @remarks
1189
- * This function checks availability by running `<linter> --version` command.
1190
- * All supported linters (biome, oxlint, eslint) implement the --version flag.
1191
- */
1192
- async function isLinterAvailable(linter) {
1193
- try {
1194
- await execaCommand(`${linter} --version`, { stdio: "ignore" });
1195
- return true;
1196
- } catch {
1197
- return false;
1198
- }
1199
- }
1200
- /**
1201
- * Detect which linter is available in the system.
1202
- *
1203
- * @returns Promise that resolves to the first available linter or undefined if none are found
1204
- *
1205
- * @remarks
1206
- * Checks in order of preference: biome, oxlint, eslint.
1207
- * Uses the `--version` flag to detect if each linter command is available.
1208
- * This is a reliable method as all supported linters implement this flag.
1209
- *
1210
- * @example
1211
- * ```typescript
1212
- * const linter = await detectLinter()
1213
- * if (linter) {
1214
- * console.log(`Using ${linter} for linting`)
1215
- * } else {
1216
- * console.log('No linter found')
1217
- * }
1218
- * ```
1219
- */
1220
- async function detectLinter() {
1221
- for (const linter of [
1222
- "biome",
1223
- "oxlint",
1224
- "eslint"
1225
- ]) if (await isLinterAvailable(linter)) return linter;
1226
- }
1227
-
1228
- //#endregion
1229
- //#region src/utils/executeHooks.ts
1230
- async function executeHooks({ hooks, events }) {
1231
- const commands = Array.isArray(hooks.done) ? hooks.done : [hooks.done].filter(Boolean);
1232
- for (const command$1 of commands) {
1233
- const [cmd, ...args] = [...parseArgsStringToArgv(command$1)];
1234
- if (!cmd) continue;
1235
- const hookId = createHash("sha256").update(command$1).digest("hex");
1236
- await events.emit("hook:start", {
1237
- id: hookId,
1238
- command: cmd,
1239
- args
1240
- });
1241
- await events.onOnce("hook:end", async ({ success, error }) => {
1242
- if (!success) throw error;
1243
- await events.emit("success", `${pc.dim(command$1)} successfully executed`);
1244
- });
1245
- }
1246
- }
1247
-
1248
- //#endregion
1249
- //#region src/utils/formatters.ts
1250
- const formatters = {
1251
- prettier: {
1252
- command: "prettier",
1253
- args: (outputPath) => [
1254
- "--ignore-unknown",
1255
- "--write",
1256
- outputPath
1257
- ],
1258
- errorMessage: "Prettier not found"
1259
- },
1260
- biome: {
1261
- command: "biome",
1262
- args: (outputPath) => [
1263
- "format",
1264
- "--write",
1265
- outputPath
1266
- ],
1267
- errorMessage: "Biome not found"
1268
- },
1269
- oxfmt: {
1270
- command: "oxfmt",
1271
- args: (outputPath) => [outputPath],
1272
- errorMessage: "Oxfmt not found"
1273
- }
1274
- };
1275
-
1276
- //#endregion
1277
- //#region src/runners/generate.ts
1278
- async function generate({ input, config: userConfig, events, logLevel }) {
1279
- const inputPath = input ?? ("path" in userConfig.input ? userConfig.input.path : void 0);
1280
- const hrStart = process$1.hrtime();
1281
- const config = {
1282
- ...userConfig,
1283
- root: userConfig.root || process$1.cwd(),
1284
- input: inputPath ? {
1285
- ...userConfig.input,
1286
- path: inputPath
1287
- } : userConfig.input,
1288
- output: {
1289
- write: true,
1290
- barrelType: "named",
1291
- extension: { ".ts": ".ts" },
1292
- format: "prettier",
1293
- ...userConfig.output
1294
- }
1295
- };
1296
- await events.emit("generation:start", config);
1297
- await events.emit("info", config.name ? `Setup generation ${pc.bold(config.name)}` : "Setup generation", inputPath);
1298
- const { sources, fabric, pluginManager } = await setup({
1299
- config,
1300
- events
1301
- });
1302
- await events.emit("info", config.name ? `Build generation ${pc.bold(config.name)}` : "Build generation", inputPath);
1303
- const { files, failedPlugins, pluginTimings, error } = await safeBuild({
1304
- config,
1305
- events
1306
- }, {
1307
- pluginManager,
1308
- fabric,
1309
- events,
1310
- sources
1311
- });
1312
- await events.emit("info", "Load summary");
1313
- if (failedPlugins.size > 0 || error) {
1314
- [error, ...Array.from(failedPlugins).filter((it) => it.error).map((it) => it.error)].filter(Boolean).forEach((err) => {
1315
- events.emit("error", err);
1316
- });
1317
- await events.emit("generation:end", config, files, sources);
1318
- await events.emit("generation:summary", config, {
1319
- failedPlugins,
1320
- filesCreated: files.length,
1321
- status: failedPlugins.size > 0 || error ? "failed" : "success",
1322
- hrStart,
1323
- pluginTimings: logLevel >= LogLevel.verbose ? pluginTimings : void 0
1324
- });
1325
- process$1.exit(1);
1326
- }
1327
- await events.emit("success", "Generation successfully", inputPath);
1328
- await events.emit("generation:end", config, files, sources);
1329
- if (config.output.format) {
1330
- await events.emit("format:start");
1331
- let formatter = config.output.format;
1332
- if (formatter === "auto") {
1333
- const detectedFormatter = await detectFormatter();
1334
- if (!detectedFormatter) await events.emit("warn", "No formatter found (biome, prettier, or oxfmt). Skipping formatting.");
1335
- else {
1336
- formatter = detectedFormatter;
1337
- await events.emit("info", `Auto-detected formatter: ${pc.dim(formatter)}`);
1338
- }
1339
- }
1340
- if (formatter && formatter !== "auto" && formatter in formatters) {
1341
- const formatterConfig = formatters[formatter];
1342
- const outputPath = path.resolve(config.root, config.output.path);
1343
- try {
1344
- const hookId = createHash("sha256").update([config.name, formatter].filter(Boolean).join("-")).digest("hex");
1345
- await events.emit("hook:start", {
1346
- id: hookId,
1347
- command: formatterConfig.command,
1348
- args: formatterConfig.args(outputPath)
1349
- });
1350
- await events.onOnce("hook:end", async ({ success, error: error$1 }) => {
1351
- if (!success) throw error$1;
1352
- await events.emit("success", [
1353
- `Formatting with ${pc.dim(formatter)}`,
1354
- logLevel >= LogLevel.info ? `on ${pc.dim(outputPath)}` : void 0,
1355
- "successfully"
1356
- ].filter(Boolean).join(" "));
1357
- });
1358
- } catch (caughtError) {
1359
- const error$1 = new Error(formatterConfig.errorMessage);
1360
- error$1.cause = caughtError;
1361
- await events.emit("error", error$1);
1362
- }
1363
- }
1364
- await events.emit("format:end");
1365
- }
1366
- if (config.output.lint) {
1367
- await events.emit("lint:start");
1368
- let linter = config.output.lint;
1369
- if (linter === "auto") {
1370
- const detectedLinter = await detectLinter();
1371
- if (!detectedLinter) await events.emit("warn", "No linter found (biome, oxlint, or eslint). Skipping linting.");
1372
- else {
1373
- linter = detectedLinter;
1374
- await events.emit("info", `Auto-detected linter: ${pc.dim(linter)}`);
1375
- }
1376
- }
1377
- if (linter && linter !== "auto") {
1378
- await events.emit("info", [`Linting with ${pc.dim(linter)}`, logLevel >= LogLevel.info ? `on ${pc.dim(path.resolve(config.root, config.output.path))}` : void 0].filter(Boolean).join(" "));
1379
- if (linter === "eslint") try {
1380
- const hookId = createHash("sha256").update([config.name, linter].filter(Boolean).join("-")).digest("hex");
1381
- await events.emit("hook:start", {
1382
- id: hookId,
1383
- command: "eslint",
1384
- args: [path.resolve(config.root, config.output.path), "--fix"]
1385
- });
1386
- await events.onOnce("hook:end", async ({ success, error: error$1 }) => {
1387
- if (!success) throw error$1;
1388
- await events.emit("success", [
1389
- `Linted with ${pc.dim(linter)}`,
1390
- logLevel >= LogLevel.info ? `on ${pc.dim(path.resolve(config.root, config.output.path))}` : void 0,
1391
- "successfully"
1392
- ].filter(Boolean).join(" "));
1393
- });
1394
- } catch (caughtError) {
1395
- const error$1 = /* @__PURE__ */ new Error("Eslint not found");
1396
- error$1.cause = caughtError;
1397
- await events.emit("error", error$1);
1398
- }
1399
- if (linter === "biome") try {
1400
- const hookId = createHash("sha256").update([config.name, linter].filter(Boolean).join("-")).digest("hex");
1401
- await events.emit("hook:start", {
1402
- id: hookId,
1403
- command: "biome",
1404
- args: [
1405
- "lint",
1406
- "--fix",
1407
- path.resolve(config.root, config.output.path)
1408
- ]
1409
- });
1410
- await events.onOnce("hook:end", async ({ success, error: error$1 }) => {
1411
- if (!success) throw error$1;
1412
- await events.emit("success", [
1413
- `Linted with ${pc.dim(linter)}`,
1414
- logLevel >= LogLevel.info ? `on ${pc.dim(path.resolve(config.root, config.output.path))}` : void 0,
1415
- "successfully"
1416
- ].filter(Boolean).join(" "));
1417
- });
1418
- } catch (caughtError) {
1419
- const error$1 = /* @__PURE__ */ new Error("Biome not found");
1420
- error$1.cause = caughtError;
1421
- await events.emit("error", error$1);
1422
- }
1423
- if (linter === "oxlint") try {
1424
- const hookId = createHash("sha256").update([config.name, linter].filter(Boolean).join("-")).digest("hex");
1425
- await events.emit("hook:start", {
1426
- id: hookId,
1427
- command: "oxlint",
1428
- args: ["--fix", path.resolve(config.root, config.output.path)]
1429
- });
1430
- await events.onOnce("hook:end", async ({ success, error: error$1 }) => {
1431
- if (!success) throw error$1;
1432
- await events.emit("success", [
1433
- `Linted with ${pc.dim(linter)}`,
1434
- logLevel >= LogLevel.info ? `on ${pc.dim(path.resolve(config.root, config.output.path))}` : void 0,
1435
- "successfully"
1436
- ].filter(Boolean).join(" "));
1437
- });
1438
- } catch (caughtError) {
1439
- const error$1 = /* @__PURE__ */ new Error("Oxlint not found");
1440
- error$1.cause = caughtError;
1441
- await events.emit("error", error$1);
1442
- }
1443
- }
1444
- await events.emit("lint:end");
1445
- }
1446
- if (config.hooks) {
1447
- await events.emit("hooks:start");
1448
- await executeHooks({
1449
- hooks: config.hooks,
1450
- events
1451
- });
1452
- await events.emit("hooks:end");
1453
- }
1454
- await events.emit("generation:summary", config, {
1455
- failedPlugins,
1456
- filesCreated: files.length,
1457
- status: failedPlugins.size > 0 || error ? "failed" : "success",
1458
- hrStart,
1459
- pluginTimings
1460
- });
1461
- }
1462
-
1463
- //#endregion
1464
- //#region src/utils/getPlugins.ts
1465
- function isJSONPlugins(plugins) {
1466
- return !!plugins?.some((plugin) => {
1467
- return Array.isArray(plugin) && typeof plugin?.at(0) === "string";
1468
- });
1469
- }
1470
- function isObjectPlugins(plugins) {
1471
- return plugins instanceof Object && !Array.isArray(plugins);
1472
- }
1473
- function getPlugins(plugins) {
1474
- if (isObjectPlugins(plugins)) throw new Error("Object plugins are not supported anymore, best to use http://kubb.dev/getting-started/configure#json");
1475
- if (isJSONPlugins(plugins)) throw new Error("JSON plugins are not supported anymore, best to use http://kubb.dev/getting-started/configure#json");
1476
- return Promise.resolve(plugins);
1477
- }
1478
-
1479
- //#endregion
1480
- //#region src/utils/getConfigs.ts
1481
- /**
1482
- * Converting UserConfig to Config Array without a change in the object beside the JSON convert.
1483
- */
1484
- async function getConfigs(result, args) {
1485
- const config = result?.config;
1486
- let kubbUserConfig = Promise.resolve(config);
1487
- if (typeof config === "function") {
1488
- const possiblePromise = config(args);
1489
- if (isPromise(possiblePromise)) kubbUserConfig = possiblePromise;
1490
- kubbUserConfig = Promise.resolve(possiblePromise);
1491
- }
1492
- let JSONConfig = await kubbUserConfig;
1493
- if (!Array.isArray(JSONConfig)) JSONConfig = [JSONConfig];
1494
- const results = [];
1495
- for (const item of JSONConfig) {
1496
- const plugins = item.plugins ? await getPlugins(item.plugins) : void 0;
1497
- results.push({
1498
- ...item,
1499
- plugins
1500
- });
1501
- }
1502
- return results;
1503
- }
1504
-
1505
- //#endregion
1506
- //#region src/utils/getCosmiConfig.ts
1507
- const tsLoader = async (configFile) => {
1508
- return await createJiti(configFile, {
1509
- jsx: {
1510
- runtime: "automatic",
1511
- importSource: "@kubb/react-fabric"
1512
- },
1513
- sourceMaps: true,
1514
- interopDefault: true
1515
- }).import(configFile, { default: true });
1516
- };
1517
- async function getCosmiConfig(moduleName, config) {
1518
- let result;
1519
- const searchPlaces = [
1520
- "package.json",
1521
- `.${moduleName}rc`,
1522
- `.${moduleName}rc.json`,
1523
- `.${moduleName}rc.yaml`,
1524
- `.${moduleName}rc.yml`,
1525
- `.${moduleName}rc.ts`,
1526
- `.${moduleName}rc.js`,
1527
- `.${moduleName}rc.mjs`,
1528
- `.${moduleName}rc.cjs`,
1529
- `${moduleName}.config.ts`,
1530
- `${moduleName}.config.js`,
1531
- `${moduleName}.config.mjs`,
1532
- `${moduleName}.config.cjs`
1533
- ];
1534
- const explorer = cosmiconfig(moduleName, {
1535
- cache: false,
1536
- searchPlaces: [
1537
- ...searchPlaces.map((searchPlace) => {
1538
- return `.config/${searchPlace}`;
1539
- }),
1540
- ...searchPlaces.map((searchPlace) => {
1541
- return `configs/${searchPlace}`;
1542
- }),
1543
- ...searchPlaces
1544
- ],
1545
- loaders: { ".ts": tsLoader }
1546
- });
1547
- try {
1548
- result = config ? await explorer.load(config) : await explorer.search();
1549
- } catch (error) {
1550
- throw new Error("Config failed loading", { cause: error });
1551
- }
1552
- if (result?.isEmpty || !result || !result.config) throw new Error("Config not defined, create a kubb.config.js or pass through your config with the option --config");
1553
- return result;
1554
- }
1555
-
1556
- //#endregion
1557
- //#region src/loggers/streamLogger.ts
1558
- /**
1559
- * Stream logger for SSE (Server-Sent Events) streaming
1560
- * Sends Kubb events to HTTP client via SSE protocol
1561
- */
1562
- function createStreamLogger(res) {
1563
- return defineLogger({
1564
- name: "stream",
1565
- install(context, options) {
1566
- const logLevel = options?.logLevel || LogLevel.info;
1567
- const state = { res };
1568
- function send(type, ...data) {
1569
- if (!state.res.destroyed) state.res.write(`data: ${JSON.stringify({
1570
- type,
1571
- data
1572
- })}\n\n`);
1573
- }
1574
- context.on("plugin:start", (plugin) => {
1575
- send("plugin:start", { name: plugin.name });
1576
- });
1577
- context.on("plugin:end", (plugin, { duration, success }) => {
1578
- send("plugin:end", { name: plugin.name }, {
1579
- duration,
1580
- success
1581
- });
1582
- });
1583
- context.on("files:processing:start", (files) => {
1584
- send("files:processing:start", { total: files.length });
1585
- });
1586
- context.on("file:processing:update", ({ file, processed, total, percentage, config }) => {
1587
- send("file:processing:update", {
1588
- file: file.path ? relative(config.root, file.path) : file.baseName || "unknown",
1589
- processed,
1590
- total,
1591
- percentage
1592
- });
1593
- });
1594
- context.on("files:processing:end", (files) => {
1595
- send("files:processing:end", { total: files.length });
1596
- });
1597
- context.on("info", (message, info) => {
1598
- if (logLevel <= LogLevel.silent) return;
1599
- send("info", message, info);
1600
- });
1601
- context.on("success", (message, info) => {
1602
- if (logLevel <= LogLevel.silent) return;
1603
- send("success", message, info);
1604
- });
1605
- context.on("warn", (message, info) => {
1606
- if (logLevel < LogLevel.warn) return;
1607
- send("warn", message, info);
1608
- });
1609
- context.on("error", (error) => {
1610
- send("error", {
1611
- message: error.message,
1612
- stack: logLevel >= LogLevel.debug ? error.stack : void 0
1613
- });
1614
- });
1615
- context.on("generation:start", (config) => {
1616
- send("generation:start", {
1617
- name: config.name,
1618
- plugins: config.plugins?.length || 0
1619
- });
1620
- });
1621
- context.on("generation:end", (config, files, sources) => {
1622
- send("generation:end", config, files, Object.fromEntries(sources));
1623
- });
1624
- }
1625
- });
1626
- }
1627
-
1628
- //#endregion
1629
- //#region src/utils/streamServer.ts
1630
- async function startStreamServer({ port, host, configPath, config, input, args }) {
1631
- const server = createServer(async (req, res) => {
1632
- res.setHeader("Access-Control-Allow-Origin", "*");
1633
- res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
1634
- res.setHeader("Access-Control-Allow-Headers", "Content-Type");
1635
- if (req.method === "OPTIONS") {
1636
- res.writeHead(204);
1637
- res.end();
1638
- return;
1639
- }
1640
- if (req.url === "/api/health" && req.method === "GET") {
1641
- res.writeHead(200, { "Content-Type": "application/json" });
1642
- const body = {
1643
- status: "ok",
1644
- version,
1645
- configPath: path.relative(process$1.cwd(), configPath)
1646
- };
1647
- res.end(JSON.stringify(body));
1648
- return;
1649
- }
1650
- if (req.url === "/api/info" && req.method === "GET") {
1651
- res.writeHead(200, { "Content-Type": "application/json" });
1652
- let specContent;
1653
- if (config && "path" in config.input) {
1654
- const specPath = path.resolve(process$1.cwd(), config.root, config.input.path);
1655
- try {
1656
- specContent = readFileSync(specPath, "utf-8");
1657
- } catch {}
1658
- }
1659
- const body = {
1660
- version,
1661
- configPath: path.relative(process$1.cwd(), configPath),
1662
- spec: specContent,
1663
- config: {
1664
- name: config.name,
1665
- root: config.root,
1666
- input: { path: "path" in config.input ? config.input.path : void 0 },
1667
- output: {
1668
- path: config.output.path,
1669
- write: config.output.write,
1670
- extension: config.output.extension,
1671
- barrelType: config.output.barrelType
1672
- },
1673
- plugins: config.plugins?.map((plugin) => ({
1674
- name: `@kubb/${plugin.name}`,
1675
- options: serializePluginOptions(plugin.options)
1676
- }))
1677
- }
1678
- };
1679
- res.end(JSON.stringify(body));
1680
- return;
1681
- }
1682
- if (req.url === "/api/stream" && req.method === "POST") {
1683
- await handleGenerate(res, config, input, args);
1684
- return;
1685
- }
1686
- res.writeHead(404, { "Content-Type": "application/json" });
1687
- res.end(JSON.stringify({ error: "Not found" }));
1688
- });
1689
- server.listen(port, host, () => {
1690
- const address = server.address();
1691
- const serverUrl = `http://${host}:${typeof address === "object" && address ? address.port : port}`;
1692
- clack.log.success(pc.green(`Stream server started on ${pc.bold(serverUrl)}`));
1693
- clack.log.info(pc.dim(`Config: ${path.relative(process$1.cwd(), configPath)}`));
1694
- clack.log.info(pc.dim(`Connect: ${serverUrl}/api/info`));
1695
- clack.log.info(pc.dim(`Stream: ${serverUrl}/api/stream`));
1696
- clack.log.info(pc.dim(`Health: ${serverUrl}/api/health`));
1697
- clack.log.step(pc.yellow("Waiting for requests... (Press Ctrl+C to stop)"));
1698
- });
1699
- process$1.on("SIGINT", () => {
1700
- clack.log.info("Shutting down stream server...");
1701
- server.close(() => {
1702
- clack.log.success("Server stopped");
1703
- process$1.exit(0);
1704
- });
1705
- });
1706
- }
1707
- async function handleGenerate(res, config, input, args) {
1708
- const events = new AsyncEventEmitter();
1709
- const logLevel = LogLevel[args.logLevel] || 3;
1710
- res.writeHead(200, {
1711
- "Content-Type": "text/event-stream",
1712
- "Cache-Control": "no-cache",
1713
- Connection: "keep-alive"
1714
- });
1715
- function send(type, ...data) {
1716
- const streamEvent = {
1717
- type,
1718
- data
1719
- };
1720
- res.write(`data: ${JSON.stringify(streamEvent)}\n\n`);
1721
- }
1722
- await createStreamLogger(res).install(events, { logLevel });
1723
- try {
1724
- await generate({
1725
- input,
1726
- config,
1727
- logLevel,
1728
- events
1729
- });
1730
- send("lifecycle:end");
1731
- } catch (error) {
1732
- send("error", {
1733
- message: error instanceof Error ? error.message : "Unknown error",
1734
- stack: error instanceof Error ? error.stack : void 0
1735
- });
1736
- } finally {
1737
- res.end();
1738
- }
1739
- }
1740
-
1741
1145
  //#endregion
1742
1146
  //#region src/utils/watcher.ts
1743
1147
  async function startWatcher(path$1, cb) {
@@ -1799,21 +1203,6 @@ const command = defineCommand({
1799
1203
  alias: "s",
1800
1204
  default: false
1801
1205
  },
1802
- stream: {
1803
- type: "boolean",
1804
- description: "Start HTTP server with SSE streaming",
1805
- default: false
1806
- },
1807
- port: {
1808
- type: "string",
1809
- description: "Port for stream server (requires --stream). If not specified, an available port is automatically selected.",
1810
- alias: "p"
1811
- },
1812
- host: {
1813
- type: "string",
1814
- description: "Host for stream server (requires --stream)",
1815
- default: "localhost"
1816
- },
1817
1206
  help: {
1818
1207
  type: "boolean",
1819
1208
  description: "Show help",
@@ -1839,20 +1228,6 @@ const command = defineCommand({
1839
1228
  try {
1840
1229
  const result = await getCosmiConfig("kubb", args.config);
1841
1230
  const configs = await getConfigs(result, args);
1842
- if (args.stream) {
1843
- if (configs.length > 1) clack.log.warn(pc.yellow("Stream mode only supports a single configuration. Only the first config will be used."));
1844
- const port = args.port ? Number.parseInt(args.port, 10) : 0;
1845
- const host = args.host;
1846
- await startStreamServer({
1847
- port,
1848
- host,
1849
- configPath: result.filepath,
1850
- config: configs[0],
1851
- input,
1852
- args
1853
- });
1854
- return;
1855
- }
1856
1231
  await events.emit("config:start");
1857
1232
  await events.emit("info", "Config loaded", path.relative(process$2.cwd(), result.filepath));
1858
1233
  await events.emit("success", "Config loaded successfully", path.relative(process$2.cwd(), result.filepath));
@@ -1892,4 +1267,4 @@ var generate_default = command;
1892
1267
 
1893
1268
  //#endregion
1894
1269
  export { generate_default as default };
1895
- //# sourceMappingURL=generate-Bjj-OR20.js.map
1270
+ //# sourceMappingURL=generate-CBsaUUvV.js.map