@kubb/core 1.1.5 → 1.1.7

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.
package/dist/index.cjs CHANGED
@@ -3,42 +3,22 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var module$1 = require('module');
6
- var pathParser2 = require('path');
6
+ var crypto = require('crypto');
7
7
  var fs = require('fs');
8
+ var pathParser2 = require('path');
8
9
  var changeCase = require('change-case');
9
10
  var rimraf = require('rimraf');
10
11
  var dirTree = require('directory-tree');
11
- var crypto = require('crypto');
12
12
  var tsCodegen = require('@kubb/ts-codegen');
13
13
 
14
14
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
15
15
 
16
+ var crypto__default = /*#__PURE__*/_interopDefault(crypto);
16
17
  var pathParser2__default = /*#__PURE__*/_interopDefault(pathParser2);
17
18
  var dirTree__default = /*#__PURE__*/_interopDefault(dirTree);
18
- var crypto__default = /*#__PURE__*/_interopDefault(crypto);
19
19
 
20
20
  module$1.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (document.currentScript && document.currentScript.src || new URL('out.js', document.baseURI).href)));
21
21
 
22
- // src/managers/pluginManager/PluginError.ts
23
- var PluginError = class extends Error {
24
- pluginManager;
25
- constructor(message, options) {
26
- super(message, { cause: options.cause });
27
- this.pluginManager = options.pluginManager;
28
- }
29
- };
30
-
31
- // src/managers/pluginManager/ParallelPluginError.ts
32
- var ParallelPluginError = class extends Error {
33
- errors;
34
- pluginManager;
35
- constructor(message, options) {
36
- super(message, { cause: options.cause });
37
- this.errors = options.errors;
38
- this.pluginManager = options.pluginManager;
39
- }
40
- };
41
-
42
22
  // src/utils/isPromise.ts
43
23
  function isPromise(result) {
44
24
  return typeof result?.then === "function";
@@ -424,76 +404,7 @@ function getStackTrace(belowFn) {
424
404
  return v8StackTrace;
425
405
  }
426
406
 
427
- // src/plugin.ts
428
- function createPlugin(factory) {
429
- return (options) => {
430
- const plugin = factory(options);
431
- if (Array.isArray(plugin)) {
432
- throw new Error("Not implemented");
433
- }
434
- if (!plugin.transform) {
435
- plugin.transform = function transform(code) {
436
- return code;
437
- };
438
- }
439
- return plugin;
440
- };
441
- }
442
- var name = "core";
443
- var definePlugin = createPlugin((options) => {
444
- const { fileManager, resolvePath, resolveName, load } = options;
445
- const api = {
446
- get config() {
447
- return options.config;
448
- },
449
- fileManager,
450
- async addFile(...files) {
451
- const trace = getStackTrace();
452
- const plugins = options.config.plugins?.filter((plugin) => trace[1].getFileName()?.includes(plugin.name)).sort((a, b) => {
453
- if (a.name.length < b.name.length)
454
- return 1;
455
- if (a.name.length > b.name.length)
456
- return -1;
457
- return 0;
458
- });
459
- const pluginName = plugins?.[0].name;
460
- return Promise.all(
461
- files.map((file) => {
462
- const fileWithMeta = {
463
- ...file,
464
- meta: {
465
- ...file.meta || {},
466
- pluginName
467
- }
468
- };
469
- if (file.override) {
470
- return fileManager.add(fileWithMeta);
471
- }
472
- return fileManager.addOrAppend(fileWithMeta);
473
- })
474
- );
475
- },
476
- resolvePath,
477
- resolveName: (params) => {
478
- const name2 = resolveName(params);
479
- return transformReservedWord(name2);
480
- },
481
- load,
482
- cache: createPluginCache(/* @__PURE__ */ Object.create(null))
483
- };
484
- return {
485
- name,
486
- options,
487
- api,
488
- resolvePath(fileName) {
489
- const root = pathParser2__default.default.resolve(this.config.root, this.config.output.path);
490
- return pathParser2__default.default.resolve(root, fileName);
491
- },
492
- resolveName(name2) {
493
- return name2;
494
- }
495
- };
496
- });
407
+ // src/managers/fileManager/FileManager.ts
497
408
  var FileManager = class {
498
409
  cache = /* @__PURE__ */ new Map();
499
410
  task;
@@ -523,6 +434,13 @@ var FileManager = class {
523
434
  });
524
435
  return files;
525
436
  }
437
+ get cachedFiles() {
438
+ const files = [];
439
+ this.cache.forEach((item) => {
440
+ files.push(item);
441
+ });
442
+ return files;
443
+ }
526
444
  async add(file) {
527
445
  const cacheItem = { id: crypto__default.default.randomUUID(), file, status: "new" };
528
446
  this.cache.set(cacheItem.id, cacheItem);
@@ -578,6 +496,222 @@ ${file.source}`,
578
496
  return read(...params);
579
497
  }
580
498
  };
499
+ function writeIndexes(root, options) {
500
+ const tree = TreeNode.build(root, { extensions: /\.ts/, ...options });
501
+ if (!tree) {
502
+ return void 0;
503
+ }
504
+ const fileReducer = (files2, item) => {
505
+ if (!item.children) {
506
+ return [];
507
+ }
508
+ if (item.children?.length > 1) {
509
+ const path = pathParser2__default.default.resolve(item.data.path, "index.ts");
510
+ const exports = item.children.map((file) => {
511
+ if (!file) {
512
+ return void 0;
513
+ }
514
+ const importPath = file.data.type === "directory" ? `./${file.data.name}` : `./${file.data.name.replace(/\.[^.]*$/, "")}`;
515
+ if (importPath.includes("index") && path.includes("index")) {
516
+ return void 0;
517
+ }
518
+ return { path: importPath };
519
+ }).filter(Boolean);
520
+ files2.push({
521
+ path,
522
+ fileName: "index.ts",
523
+ source: "",
524
+ exports
525
+ });
526
+ } else {
527
+ item.children?.forEach((child) => {
528
+ const path = pathParser2__default.default.resolve(item.data.path, "index.ts");
529
+ const importPath = child.data.type === "directory" ? `./${child.data.name}` : `./${child.data.name.replace(/\.[^.]*$/, "")}`;
530
+ files2.push({
531
+ path,
532
+ fileName: "index.ts",
533
+ source: "",
534
+ exports: [{ path: importPath }]
535
+ });
536
+ });
537
+ }
538
+ item.children.forEach((childItem) => {
539
+ fileReducer(files2, childItem);
540
+ });
541
+ return files2;
542
+ };
543
+ const files = fileReducer([], tree);
544
+ return files;
545
+ }
546
+ function combineFiles(files) {
547
+ return files.filter(Boolean).reduce((acc, curr) => {
548
+ if (!curr) {
549
+ return acc;
550
+ }
551
+ const prevIndex = acc.findIndex((item) => item.path === curr.path);
552
+ if (prevIndex !== -1) {
553
+ const prev = acc[prevIndex];
554
+ acc[prevIndex] = {
555
+ ...curr,
556
+ source: `${prev.source}
557
+ ${curr.source}`,
558
+ imports: [...prev.imports || [], ...curr.imports || []],
559
+ exports: [...prev.exports || [], ...curr.exports || []]
560
+ };
561
+ } else {
562
+ acc.push(curr);
563
+ }
564
+ return acc;
565
+ }, []);
566
+ }
567
+ function getFileSource(file) {
568
+ let { source } = file;
569
+ if (!file.fileName.endsWith(".ts")) {
570
+ return file.source;
571
+ }
572
+ const imports = [];
573
+ const exports = [];
574
+ file.imports?.forEach((curr) => {
575
+ const exists = imports.find((imp) => imp.path === curr.path);
576
+ if (!exists) {
577
+ imports.push({
578
+ ...curr,
579
+ name: Array.isArray(curr.name) ? [...new Set(curr.name)] : curr.name
580
+ });
581
+ }
582
+ if (exists && !Array.isArray(exists.name) && exists.name !== curr.name) {
583
+ imports.push(curr);
584
+ }
585
+ if (exists && Array.isArray(exists.name)) {
586
+ if (Array.isArray(curr.name)) {
587
+ exists.name = [.../* @__PURE__ */ new Set([...exists.name, ...curr.name])];
588
+ }
589
+ }
590
+ });
591
+ file.exports?.forEach((curr) => {
592
+ const exists = exports.find((imp) => imp.path === curr.path);
593
+ if (!exists) {
594
+ exports.push({
595
+ ...curr,
596
+ name: Array.isArray(curr.name) ? [...new Set(curr.name)] : curr.name
597
+ });
598
+ }
599
+ if (exists && !Array.isArray(exists.name) && exists.name !== curr.name && exists.asAlias === curr.asAlias) {
600
+ exports.push(curr);
601
+ }
602
+ if (exists && Array.isArray(exists.name)) {
603
+ if (Array.isArray(curr.name)) {
604
+ exists.name = [.../* @__PURE__ */ new Set([...exists.name, ...curr.name])];
605
+ }
606
+ }
607
+ });
608
+ const importNodes = imports.reduce((prev, curr) => {
609
+ return [...prev, tsCodegen.createImportDeclaration({ name: curr.name, path: curr.path, isTypeOnly: curr.isTypeOnly })];
610
+ }, []);
611
+ const importSource = tsCodegen.print(importNodes);
612
+ const exportNodes = exports.reduce((prev, curr) => {
613
+ return [...prev, tsCodegen.createExportDeclaration({ name: curr.name, path: curr.path, asAlias: curr.asAlias })];
614
+ }, []);
615
+ const exportSource = tsCodegen.print(exportNodes);
616
+ if (importSource) {
617
+ source = `${importSource}
618
+ ${source}`;
619
+ }
620
+ if (exportSource) {
621
+ source = `${exportSource}
622
+ ${source}`;
623
+ }
624
+ return source;
625
+ }
626
+ function createPlugin(factory) {
627
+ return (options) => {
628
+ const plugin = factory(options);
629
+ if (Array.isArray(plugin)) {
630
+ throw new Error("Not implemented");
631
+ }
632
+ if (!plugin.transform) {
633
+ plugin.transform = function transform(code) {
634
+ return code;
635
+ };
636
+ }
637
+ return plugin;
638
+ };
639
+ }
640
+ var name = "core";
641
+ var definePlugin = createPlugin((options) => {
642
+ const { fileManager, resolvePath, resolveName, load } = options;
643
+ const api = {
644
+ get config() {
645
+ return options.config;
646
+ },
647
+ fileManager,
648
+ async addFile(...files) {
649
+ const trace = getStackTrace();
650
+ const plugins = options.config.plugins?.filter((plugin) => trace[1].getFileName()?.includes(plugin.name)).sort((a, b) => {
651
+ if (a.name.length < b.name.length)
652
+ return 1;
653
+ if (a.name.length > b.name.length)
654
+ return -1;
655
+ return 0;
656
+ });
657
+ const pluginName = plugins?.[0].name;
658
+ return Promise.all(
659
+ files.map((file) => {
660
+ const fileWithMeta = {
661
+ ...file,
662
+ meta: {
663
+ ...file.meta || {},
664
+ pluginName
665
+ }
666
+ };
667
+ if (file.override) {
668
+ return fileManager.add(fileWithMeta);
669
+ }
670
+ return fileManager.addOrAppend(fileWithMeta);
671
+ })
672
+ );
673
+ },
674
+ resolvePath,
675
+ resolveName: (params) => {
676
+ const name2 = resolveName(params);
677
+ return transformReservedWord(name2);
678
+ },
679
+ load,
680
+ cache: createPluginCache(/* @__PURE__ */ Object.create(null))
681
+ };
682
+ return {
683
+ name,
684
+ options,
685
+ api,
686
+ resolvePath(fileName) {
687
+ const root = pathParser2__default.default.resolve(this.config.root, this.config.output.path);
688
+ return pathParser2__default.default.resolve(root, fileName);
689
+ },
690
+ resolveName(name2) {
691
+ return name2;
692
+ }
693
+ };
694
+ });
695
+
696
+ // src/managers/pluginManager/ParallelPluginError.ts
697
+ var ParallelPluginError = class extends Error {
698
+ errors;
699
+ pluginManager;
700
+ constructor(message, options) {
701
+ super(message, { cause: options.cause });
702
+ this.errors = options.errors;
703
+ this.pluginManager = options.pluginManager;
704
+ }
705
+ };
706
+
707
+ // src/managers/pluginManager/PluginError.ts
708
+ var PluginError = class extends Error {
709
+ pluginManager;
710
+ constructor(message, options) {
711
+ super(message, { cause: options.cause });
712
+ this.pluginManager = options.pluginManager;
713
+ }
714
+ };
581
715
 
582
716
  // src/managers/pluginManager/PluginManager.ts
583
717
  var hookNames = {
@@ -691,9 +825,9 @@ var PluginManager = class {
691
825
  for (const plugin of this.getSortedPlugins(hookName)) {
692
826
  if (skipped && skipped.has(plugin))
693
827
  continue;
694
- promise = promise.then(async (result) => {
695
- if (result != null) {
696
- return result;
828
+ promise = promise.then(async (parseResult) => {
829
+ if (parseResult?.result != null) {
830
+ return parseResult;
697
831
  }
698
832
  const value = await this.execute({
699
833
  strategy: "hookFirst",
@@ -718,45 +852,38 @@ var PluginManager = class {
718
852
  parameters,
719
853
  skipped
720
854
  }) {
721
- let result = null;
855
+ let parseResult = null;
722
856
  for (const plugin of this.getSortedPlugins(hookName)) {
723
857
  if (skipped && skipped.has(plugin))
724
858
  continue;
725
- result = {
859
+ parseResult = {
726
860
  result: this.executeSync({
727
861
  strategy: "hookFirst",
728
862
  hookName,
729
863
  parameters,
730
864
  plugin
731
- })
865
+ }),
866
+ plugin
732
867
  };
733
- if (result != null) {
868
+ if (parseResult?.result != null) {
734
869
  break;
735
870
  }
736
871
  }
737
- return result;
872
+ return parseResult;
738
873
  }
739
- // parallel
874
+ /**
875
+ *
876
+ * Parallel, runs all plugins
877
+ */
740
878
  async hookParallel({
741
879
  hookName,
742
880
  parameters
743
881
  }) {
744
882
  const parallelPromises = [];
745
883
  for (const plugin of this.getSortedPlugins(hookName)) {
746
- if (plugin[hookName]?.sequential) {
747
- await Promise.all(parallelPromises);
748
- parallelPromises.length = 0;
749
- await this.execute({
750
- strategy: "hookParallel",
751
- hookName,
752
- parameters,
753
- plugin
754
- });
755
- } else {
756
- const promise = this.execute({ strategy: "hookParallel", hookName, parameters, plugin });
757
- if (promise) {
758
- parallelPromises.push(promise);
759
- }
884
+ const promise = this.execute({ strategy: "hookParallel", hookName, parameters, plugin });
885
+ if (promise) {
886
+ parallelPromises.push(promise);
760
887
  }
761
888
  }
762
889
  const results = await Promise.allSettled(parallelPromises);
@@ -766,7 +893,10 @@ var PluginManager = class {
766
893
  }
767
894
  return results.filter((result) => result.status === "fulfilled").map((result) => result.value);
768
895
  }
769
- // chains, reduces returned value, handling the reduced value as the first hook argument
896
+ /**
897
+ *
898
+ * Chains, reduces returned value, handling the reduced value as the first hook argument
899
+ */
770
900
  hookReduceArg0({
771
901
  hookName,
772
902
  parameters,
@@ -775,11 +905,11 @@ var PluginManager = class {
775
905
  const [argument0, ...rest] = parameters;
776
906
  let promise = Promise.resolve(argument0);
777
907
  for (const plugin of this.getSortedPlugins(hookName)) {
778
- promise = promise.then((argument02) => {
908
+ promise = promise.then((arg0) => {
779
909
  const value = this.execute({
780
910
  strategy: "hookReduceArg0",
781
911
  hookName,
782
- parameters: [argument02, ...rest],
912
+ parameters: [arg0, ...rest],
783
913
  plugin
784
914
  });
785
915
  return value;
@@ -787,7 +917,9 @@ var PluginManager = class {
787
917
  }
788
918
  return promise;
789
919
  }
790
- // chains
920
+ /**
921
+ * Chains plugins
922
+ */
791
923
  hookSeq({ hookName, parameters }) {
792
924
  let promise = Promise.resolve();
793
925
  for (const plugin of this.getSortedPlugins(hookName)) {
@@ -937,139 +1069,9 @@ function validatePlugins(plugins, dependedPluginNames) {
937
1069
  });
938
1070
  return true;
939
1071
  }
940
- function writeIndexes(root, options) {
941
- const tree = TreeNode.build(root, { extensions: /\.ts/, ...options });
942
- if (!tree) {
943
- return void 0;
944
- }
945
- const fileReducer = (files2, item) => {
946
- if (!item.children) {
947
- return [];
948
- }
949
- if (item.children?.length > 1) {
950
- const path = pathParser2__default.default.resolve(item.data.path, "index.ts");
951
- const exports = item.children.map((file) => {
952
- if (!file) {
953
- return void 0;
954
- }
955
- const importPath = file.data.type === "directory" ? `./${file.data.name}` : `./${file.data.name.replace(/\.[^.]*$/, "")}`;
956
- if (importPath.includes("index") && path.includes("index")) {
957
- return void 0;
958
- }
959
- return { path: importPath };
960
- }).filter(Boolean);
961
- files2.push({
962
- path,
963
- fileName: "index.ts",
964
- source: "",
965
- exports
966
- });
967
- } else {
968
- item.children?.forEach((child) => {
969
- const path = pathParser2__default.default.resolve(item.data.path, "index.ts");
970
- const importPath = child.data.type === "directory" ? `./${child.data.name}` : `./${child.data.name.replace(/\.[^.]*$/, "")}`;
971
- files2.push({
972
- path,
973
- fileName: "index.ts",
974
- source: "",
975
- exports: [{ path: importPath }]
976
- });
977
- });
978
- }
979
- item.children.forEach((childItem) => {
980
- fileReducer(files2, childItem);
981
- });
982
- return files2;
983
- };
984
- const files = fileReducer([], tree);
985
- return files;
986
- }
987
- function combineFiles(files) {
988
- return files.filter(Boolean).reduce((acc, curr) => {
989
- if (!curr) {
990
- return acc;
991
- }
992
- const prevIndex = acc.findIndex((item) => item.path === curr.path);
993
- if (prevIndex !== -1) {
994
- const prev = acc[prevIndex];
995
- acc[prevIndex] = {
996
- ...curr,
997
- source: `${prev.source}
998
- ${curr.source}`,
999
- imports: [...prev.imports || [], ...curr.imports || []],
1000
- exports: [...prev.exports || [], ...curr.exports || []]
1001
- };
1002
- } else {
1003
- acc.push(curr);
1004
- }
1005
- return acc;
1006
- }, []);
1007
- }
1008
- function getFileSource(file) {
1009
- let { source } = file;
1010
- if (!file.fileName.endsWith(".ts")) {
1011
- return file.source;
1012
- }
1013
- const imports = [];
1014
- const exports = [];
1015
- file.imports?.forEach((curr) => {
1016
- const exists = imports.find((imp) => imp.path === curr.path);
1017
- if (!exists) {
1018
- imports.push({
1019
- ...curr,
1020
- name: Array.isArray(curr.name) ? [...new Set(curr.name)] : curr.name
1021
- });
1022
- }
1023
- if (exists && !Array.isArray(exists.name) && exists.name !== curr.name) {
1024
- imports.push(curr);
1025
- }
1026
- if (exists && Array.isArray(exists.name)) {
1027
- if (Array.isArray(curr.name)) {
1028
- exists.name = [.../* @__PURE__ */ new Set([...exists.name, ...curr.name])];
1029
- }
1030
- }
1031
- });
1032
- file.exports?.forEach((curr) => {
1033
- const exists = exports.find((imp) => imp.path === curr.path);
1034
- if (!exists) {
1035
- exports.push({
1036
- ...curr,
1037
- name: Array.isArray(curr.name) ? [...new Set(curr.name)] : curr.name
1038
- });
1039
- }
1040
- if (exists && !Array.isArray(exists.name) && exists.name !== curr.name && exists.asAlias === curr.asAlias) {
1041
- exports.push(curr);
1042
- }
1043
- if (exists && Array.isArray(exists.name)) {
1044
- if (Array.isArray(curr.name)) {
1045
- exists.name = [.../* @__PURE__ */ new Set([...exists.name, ...curr.name])];
1046
- }
1047
- }
1048
- });
1049
- const importNodes = imports.reduce((prev, curr) => {
1050
- return [...prev, tsCodegen.createImportDeclaration({ name: curr.name, path: curr.path, isTypeOnly: curr.isTypeOnly })];
1051
- }, []);
1052
- const importSource = tsCodegen.print(importNodes);
1053
- const exportNodes = exports.reduce((prev, curr) => {
1054
- return [...prev, tsCodegen.createExportDeclaration({ name: curr.name, path: curr.path, asAlias: curr.asAlias })];
1055
- }, []);
1056
- const exportSource = tsCodegen.print(exportNodes);
1057
- if (importSource) {
1058
- source = `${importSource}
1059
- ${source}`;
1060
- }
1061
- if (exportSource) {
1062
- source = `${exportSource}
1063
- ${source}`;
1064
- }
1065
- return source;
1066
- }
1067
1072
 
1068
1073
  // src/build.ts
1069
1074
  async function transformReducer(_previousCode, result, _plugin) {
1070
- if (result === null) {
1071
- return null;
1072
- }
1073
1075
  return result;
1074
1076
  }
1075
1077
  async function build(options) {
package/dist/index.d.ts CHANGED
@@ -124,11 +124,12 @@ declare class FileManager {
124
124
  private queue?;
125
125
  constructor(options?: {
126
126
  queue: Queue;
127
- task: QueueTask<unknown>;
127
+ task?: QueueTask<unknown>;
128
128
  });
129
129
  private getCache;
130
130
  getCacheByPath(path: string | undefined): CacheStore | undefined;
131
131
  get files(): File[];
132
+ get cachedFiles(): CacheStore[];
132
133
  add(file: File): Promise<File>;
133
134
  addOrAppend(file: File): Promise<File>;
134
135
  setStatus(id: UUID, status: Status): void;
@@ -210,21 +211,32 @@ declare class PluginManager {
210
211
  parameters: Parameters<PluginLifecycle[H]>;
211
212
  skipped?: ReadonlySet<KubbPlugin> | null;
212
213
  }): SafeParseResult<H>;
214
+ /**
215
+ *
216
+ * Parallel, runs all plugins
217
+ */
213
218
  hookParallel<H extends PluginLifecycleHooks, TOuput = void>({ hookName, parameters, }: {
214
219
  hookName: H;
215
220
  parameters?: Parameters<PluginLifecycle[H]> | undefined;
216
221
  }): Promise<Awaited<TOuput>[]>;
222
+ /**
223
+ *
224
+ * Chains, reduces returned value, handling the reduced value as the first hook argument
225
+ */
217
226
  hookReduceArg0<H extends PluginLifecycleHooks>({ hookName, parameters, reduce, }: {
218
227
  hookName: H;
219
228
  parameters: Parameters<PluginLifecycle[H]>;
220
229
  reduce: (reduction: Argument0<H>, result: ReturnType<ParseResult<H>>, plugin: KubbPlugin) => MaybePromise<Argument0<H> | null>;
221
230
  }): Promise<Argument0<H>>;
231
+ /**
232
+ * Chains plugins
233
+ */
222
234
  hookSeq<H extends PluginLifecycleHooks>({ hookName, parameters }: {
223
235
  hookName: H;
224
236
  parameters?: Parameters<PluginLifecycle[H]>;
225
237
  }): Promise<void>;
226
238
  private getSortedPlugins;
227
- private getPlugin;
239
+ getPlugin(hookName: keyof PluginLifecycle, pluginName: string): KubbPlugin;
228
240
  private addExecuter;
229
241
  /**
230
242
  * Run an async plugin hook and return the result.