@kubb/core 1.0.0-beta.1 → 1.0.0-beta.10

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
@@ -6,16 +6,16 @@ var module$1 = require('module');
6
6
  var pathParser2 = require('path');
7
7
  var fs = require('fs');
8
8
  var rimraf = require('rimraf');
9
- var uniq = require('lodash.uniq');
10
9
  var uuid = require('uuid');
11
10
  var dirTree = require('directory-tree');
11
+ var uniq = require('lodash.uniq');
12
12
 
13
13
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
14
14
 
15
15
  var pathParser2__default = /*#__PURE__*/_interopDefault(pathParser2);
16
16
  var rimraf__default = /*#__PURE__*/_interopDefault(rimraf);
17
- var uniq__default = /*#__PURE__*/_interopDefault(uniq);
18
17
  var dirTree__default = /*#__PURE__*/_interopDefault(dirTree);
18
+ var uniq__default = /*#__PURE__*/_interopDefault(uniq);
19
19
 
20
20
  module$1.createRequire((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('out.js', document.baseURI).href)));
21
21
 
@@ -150,7 +150,7 @@ function nameSorter(a, b) {
150
150
  function createJSDocBlockText({ comments }) {
151
151
  const filteredComments = comments.filter(Boolean);
152
152
  if (!filteredComments.length) {
153
- return void 0;
153
+ return "";
154
154
  }
155
155
  const text = filteredComments.reduce((acc, comment) => {
156
156
  return `${acc}
@@ -208,6 +208,22 @@ var Queue = class {
208
208
  }
209
209
  };
210
210
 
211
+ // src/utils/getEncodedText.ts
212
+ function getEncodedText(text) {
213
+ return text ? text.replaceAll("`", "\\`") : "";
214
+ }
215
+
216
+ // src/utils/renderTemplate.ts
217
+ function renderTemplate(template, data = void 0) {
218
+ if (!data) {
219
+ return template.replace(/{{(.*?)}}/g, "");
220
+ }
221
+ return template.replace(/{{(.*?)}}/g, (match) => {
222
+ const value = data[match.split(/{{|}}/).filter(Boolean)[0].trim()];
223
+ return value || "";
224
+ });
225
+ }
226
+
211
227
  // src/plugin.ts
212
228
  function createPlugin(factory) {
213
229
  return (options) => {
@@ -225,7 +241,7 @@ function createPlugin(factory) {
225
241
  }
226
242
  var name = "core";
227
243
  var definePlugin = createPlugin((options) => {
228
- const { fileManager, resolveId, load } = options;
244
+ const { fileManager, resolvePath, resolveName, load } = options;
229
245
  const api = {
230
246
  get config() {
231
247
  return options.config;
@@ -234,7 +250,8 @@ var definePlugin = createPlugin((options) => {
234
250
  async addFile(file) {
235
251
  return fileManager.addOrAppend(file);
236
252
  },
237
- resolveId,
253
+ resolvePath,
254
+ resolveName,
238
255
  load,
239
256
  cache: createPluginCache(/* @__PURE__ */ Object.create(null))
240
257
  };
@@ -242,11 +259,14 @@ var definePlugin = createPlugin((options) => {
242
259
  name,
243
260
  options,
244
261
  api,
245
- resolveId(fileName, directory) {
262
+ resolvePath(fileName, directory) {
246
263
  if (!directory) {
247
264
  return null;
248
265
  }
249
266
  return pathParser2__default.default.resolve(directory, fileName);
267
+ },
268
+ resolveName(name2) {
269
+ return name2;
250
270
  }
251
271
  };
252
272
  });
@@ -272,35 +292,6 @@ var FileManager = class {
272
292
  });
273
293
  return cache;
274
294
  }
275
- getSource(file) {
276
- if (!file.fileName.endsWith(".ts")) {
277
- return file.source;
278
- }
279
- const imports = [];
280
- file.imports?.forEach((curr) => {
281
- const exists = imports.find((imp) => imp.path === curr.path);
282
- if (!exists) {
283
- imports.push(curr);
284
- return;
285
- }
286
- if (exists && Array.isArray(curr.name)) {
287
- exists.name = uniq__default.default([...exists.name, ...curr.name]);
288
- }
289
- });
290
- const importSource = imports.reduce((prev, curr) => {
291
- if (Array.isArray(curr.name)) {
292
- return `${prev}
293
- import ${curr.isTypeOnly ? "type " : ""}{ ${curr.name.join(", ")} } from "${curr.path}";`;
294
- }
295
- return `${prev}
296
- import ${curr.isTypeOnly ? "type " : ""}${curr.name} from "${curr.path}";`;
297
- }, "");
298
- if (importSource) {
299
- return `${importSource}
300
- ${file.source}`;
301
- }
302
- return file.source;
303
- }
304
295
  get files() {
305
296
  const files = [];
306
297
  this.cache.forEach((item) => {
@@ -331,26 +322,6 @@ ${file.source}`,
331
322
  }
332
323
  return this.add(file);
333
324
  }
334
- combine(files) {
335
- return files.filter(Boolean).reduce((acc, curr) => {
336
- if (!curr) {
337
- return acc;
338
- }
339
- const prevIndex = acc.findIndex((item) => item.path === curr.path);
340
- if (prevIndex !== -1) {
341
- const prev = acc[prevIndex];
342
- acc[prevIndex] = {
343
- ...curr,
344
- source: `${prev.source}
345
- ${curr.source}`,
346
- imports: [...prev.imports || [], ...curr.imports || []]
347
- };
348
- } else {
349
- acc.push(curr);
350
- }
351
- return acc;
352
- }, []);
353
- }
354
325
  setStatus(id, status) {
355
326
  const cacheItem = this.getCache(id);
356
327
  if (!cacheItem) {
@@ -456,12 +427,69 @@ var TreeNode = class {
456
427
  return treeNode;
457
428
  }
458
429
  };
430
+ function combineFiles(files) {
431
+ return files.filter(Boolean).reduce((acc, curr) => {
432
+ if (!curr) {
433
+ return acc;
434
+ }
435
+ const prevIndex = acc.findIndex((item) => item.path === curr.path);
436
+ if (prevIndex !== -1) {
437
+ const prev = acc[prevIndex];
438
+ acc[prevIndex] = {
439
+ ...curr,
440
+ source: `${prev.source}
441
+ ${curr.source}`,
442
+ imports: [...prev.imports || [], ...curr.imports || []]
443
+ };
444
+ } else {
445
+ acc.push(curr);
446
+ }
447
+ return acc;
448
+ }, []);
449
+ }
450
+ function getFileSource(file) {
451
+ if (!file.fileName.endsWith(".ts")) {
452
+ return file.source;
453
+ }
454
+ const imports = [];
455
+ file.imports?.forEach((curr) => {
456
+ const exists = imports.find((imp) => imp.path === curr.path);
457
+ if (!exists) {
458
+ imports.push({
459
+ ...curr,
460
+ name: Array.isArray(curr.name) ? uniq__default.default(curr.name) : curr.name
461
+ });
462
+ }
463
+ if (exists && !Array.isArray(exists.name) && exists.name !== curr.name) {
464
+ imports.push(curr);
465
+ }
466
+ if (exists && Array.isArray(exists.name)) {
467
+ if (Array.isArray(curr.name)) {
468
+ exists.name = uniq__default.default([...exists.name, ...curr.name]);
469
+ }
470
+ }
471
+ });
472
+ const importSource = imports.reduce((prev, curr) => {
473
+ if (Array.isArray(curr.name)) {
474
+ return `${prev}
475
+ import ${curr.isTypeOnly ? "type " : ""}{ ${curr.name.join(", ")} } from "${curr.path}";`;
476
+ }
477
+ return `${prev}
478
+ import ${curr.isTypeOnly ? "type " : ""}${curr.name} from "${curr.path}";`;
479
+ }, "");
480
+ if (importSource) {
481
+ return `${importSource}
482
+ ${file.source}`;
483
+ }
484
+ return file.source;
485
+ }
459
486
 
460
487
  // src/managers/pluginManager/PluginManager.ts
461
488
  var hookNames = {
462
489
  validate: 1,
463
490
  buildStart: 1,
464
- resolveId: 1,
491
+ resolvePath: 1,
492
+ resolveName: 1,
465
493
  load: 1,
466
494
  transform: 1,
467
495
  writeFile: 1,
@@ -484,95 +512,196 @@ var PluginManager = class {
484
512
  config,
485
513
  fileManager: this.fileManager,
486
514
  load: this.load,
487
- resolveId: this.resolveId
515
+ resolvePath: this.resolvePath,
516
+ resolveName: this.resolveName
488
517
  });
489
518
  this.plugins = [this.core, ...config.plugins || []];
490
519
  }
491
- resolveId = (params) => {
520
+ resolvePath = (params) => {
492
521
  if (params.pluginName) {
493
- return this.hookForPlugin(params.pluginName, "resolveId", [params.fileName, params.directory, params.options]);
522
+ return this.hookForPluginSync({
523
+ pluginName: params.pluginName,
524
+ hookName: "resolvePath",
525
+ parameters: [params.fileName, params.directory, params.options]
526
+ });
494
527
  }
495
- return this.hookFirst("resolveId", [params.fileName, params.directory, params.options]);
528
+ return this.hookFirstSync({
529
+ hookName: "resolvePath",
530
+ parameters: [params.fileName, params.directory, params.options]
531
+ });
532
+ };
533
+ resolveName = (params) => {
534
+ if (params.pluginName) {
535
+ return this.hookForPluginSync({
536
+ pluginName: params.pluginName,
537
+ hookName: "resolveName",
538
+ parameters: [params.name]
539
+ });
540
+ }
541
+ return this.hookFirstSync({
542
+ hookName: "resolveName",
543
+ parameters: [params.name]
544
+ });
496
545
  };
497
546
  load = async (id) => {
498
- return this.hookFirst("load", [id]);
547
+ return this.hookFirst({
548
+ hookName: "load",
549
+ parameters: [id]
550
+ });
499
551
  };
500
- // run only hook for a specific plugin name
501
- hookForPlugin(pluginName, hookName, parameters, skipped) {
552
+ /**
553
+ *
554
+ * Run only hook for a specific plugin name
555
+ */
556
+ hookForPlugin({
557
+ pluginName,
558
+ hookName,
559
+ parameters
560
+ }) {
561
+ const plugin = this.getPlugin(hookName, pluginName);
562
+ return this.run({
563
+ strategy: "hookFirst",
564
+ hookName,
565
+ parameters,
566
+ plugin
567
+ });
568
+ }
569
+ hookForPluginSync({
570
+ pluginName,
571
+ hookName,
572
+ parameters
573
+ }) {
574
+ const plugin = this.getPlugin(hookName, pluginName);
575
+ return this.runSync({
576
+ strategy: "hookFirst",
577
+ hookName,
578
+ parameters,
579
+ plugin
580
+ });
581
+ }
582
+ /**
583
+ *
584
+ * Chains, first non-null result stops and returns
585
+ */
586
+ hookFirst({
587
+ hookName,
588
+ parameters,
589
+ skipped
590
+ }) {
502
591
  let promise = Promise.resolve(null);
503
- for (const plugin of this.getSortedPlugins(hookName, pluginName)) {
592
+ for (const plugin of this.getSortedPlugins(hookName)) {
504
593
  if (skipped && skipped.has(plugin))
505
594
  continue;
506
595
  promise = promise.then((result) => {
507
596
  if (result != null)
508
597
  return result;
509
- return this.run("hookFirst", hookName, parameters, plugin);
598
+ return this.run({
599
+ strategy: "hookFirst",
600
+ hookName,
601
+ parameters,
602
+ plugin
603
+ });
510
604
  });
511
605
  }
512
606
  return promise;
513
607
  }
514
- // chains, first non-null result stops and returns
515
- hookFirst(hookName, parameters, skipped) {
516
- let promise = Promise.resolve(null);
608
+ /**
609
+ *
610
+ * Chains, first non-null result stops and returns
611
+ */
612
+ hookFirstSync({
613
+ hookName,
614
+ parameters,
615
+ skipped
616
+ }) {
617
+ let result = null;
517
618
  for (const plugin of this.getSortedPlugins(hookName)) {
518
619
  if (skipped && skipped.has(plugin))
519
620
  continue;
520
- promise = promise.then((result) => {
521
- if (result != null)
522
- return result;
523
- return this.run("hookFirst", hookName, parameters, plugin);
621
+ result = this.runSync({
622
+ strategy: "hookFirst",
623
+ hookName,
624
+ parameters,
625
+ plugin
524
626
  });
627
+ if (result != null) {
628
+ break;
629
+ }
525
630
  }
526
- return promise;
631
+ return result;
527
632
  }
528
633
  // parallel
529
- async hookParallel(hookName, parameters) {
634
+ async hookParallel({
635
+ hookName,
636
+ parameters
637
+ }) {
530
638
  const parallelPromises = [];
531
639
  for (const plugin of this.getSortedPlugins(hookName)) {
532
640
  if (plugin[hookName]?.sequential) {
533
641
  await Promise.all(parallelPromises);
534
642
  parallelPromises.length = 0;
535
- await this.run("hookParallel", hookName, parameters, plugin);
643
+ await this.run({
644
+ strategy: "hookParallel",
645
+ hookName,
646
+ parameters,
647
+ plugin
648
+ });
536
649
  } else {
537
- const promise = this.run("hookParallel", hookName, parameters, plugin);
650
+ const promise = this.run({ strategy: "hookParallel", hookName, parameters, plugin });
538
651
  parallelPromises.push(promise);
539
652
  }
540
653
  }
541
654
  return Promise.all(parallelPromises);
542
655
  }
543
656
  // chains, reduces returned value, handling the reduced value as the first hook argument
544
- hookReduceArg0(hookName, [argument0, ...rest], reduce) {
657
+ hookReduceArg0({
658
+ hookName,
659
+ parameters,
660
+ reduce
661
+ }) {
662
+ const [argument0, ...rest] = parameters;
545
663
  let promise = Promise.resolve(argument0);
546
664
  for (const plugin of this.getSortedPlugins(hookName)) {
547
665
  promise = promise.then(
548
- (argument02) => this.run("hookReduceArg0", hookName, [argument02, ...rest], plugin).then(
549
- (result) => reduce.call(this.core.api, argument02, result, plugin)
550
- )
666
+ (argument02) => this.run({
667
+ strategy: "hookReduceArg0",
668
+ hookName,
669
+ parameters: [argument02, ...rest],
670
+ plugin
671
+ }).then((result) => reduce.call(this.core.api, argument02, result, plugin))
551
672
  );
552
673
  }
553
674
  return promise;
554
675
  }
555
676
  // chains
556
- hookSeq(hookName, parameters) {
677
+ hookSeq({ hookName, parameters }) {
557
678
  let promise = Promise.resolve();
558
679
  for (const plugin of this.getSortedPlugins(hookName)) {
559
- promise = promise.then(() => this.run("hookSeq", hookName, parameters, plugin));
680
+ promise = promise.then(
681
+ () => this.run({
682
+ strategy: "hookSeq",
683
+ hookName,
684
+ parameters,
685
+ plugin
686
+ })
687
+ );
560
688
  }
561
689
  return promise.then(noReturn);
562
690
  }
563
- getSortedPlugins(hookName, pluginName) {
691
+ getSortedPlugins(_hookName) {
564
692
  const plugins = [...this.plugins];
565
- if (pluginName) {
566
- const pluginsByPluginName = plugins.filter((item) => item.name === pluginName && item[hookName]);
567
- if (pluginsByPluginName.length === 0) {
568
- if (this.config.logLevel === "warn" && this.logger?.spinner) {
569
- this.logger.spinner.info(`Plugin hook with ${hookName} not found for plugin ${pluginName}`);
570
- }
571
- return [this.core];
693
+ return plugins;
694
+ }
695
+ getPlugin(hookName, pluginName) {
696
+ const plugins = [...this.plugins];
697
+ const pluginByPluginName = plugins.find((item) => item.name === pluginName && item[hookName]);
698
+ if (!pluginByPluginName) {
699
+ if (this.config.logLevel === "warn" && this.logger?.spinner) {
700
+ this.logger.spinner.info(`Plugin hook with ${hookName} not found for plugin ${pluginName}`);
572
701
  }
573
- return pluginsByPluginName;
702
+ return this.core;
574
703
  }
575
- return plugins;
704
+ return pluginByPluginName;
576
705
  }
577
706
  /**
578
707
  * Run an async plugin hook and return the result.
@@ -581,7 +710,12 @@ var PluginManager = class {
581
710
  * @param plugin The actual pluginObject to run.
582
711
  */
583
712
  // Implementation signature
584
- run(strategy, hookName, parameters, plugin) {
713
+ run({
714
+ strategy,
715
+ hookName,
716
+ parameters,
717
+ plugin
718
+ }) {
585
719
  const hook = plugin[hookName];
586
720
  return Promise.resolve().then(() => {
587
721
  if (typeof hook !== "function") {
@@ -617,12 +751,21 @@ var PluginManager = class {
617
751
  * @param plugin The acutal plugin
618
752
  * @param replaceContext When passed, the plugin context can be overridden.
619
753
  */
620
- runSync(hookName, parameters, plugin) {
754
+ runSync({
755
+ strategy,
756
+ hookName,
757
+ parameters,
758
+ plugin
759
+ }) {
621
760
  const hook = plugin[hookName];
622
761
  try {
762
+ if (typeof hook !== "function") {
763
+ return hook;
764
+ }
623
765
  return hook.apply(this.core.api, parameters);
624
- } catch (error) {
625
- return error;
766
+ } catch (e) {
767
+ this.catcher(e, plugin, hookName);
768
+ return null;
626
769
  }
627
770
  }
628
771
  catcher(e, plugin, hookName) {
@@ -660,43 +803,60 @@ async function transformReducer(_previousCode, result, _plugin) {
660
803
  }
661
804
  return result;
662
805
  }
663
- async function buildImplementation(options, done) {
806
+ async function buildImplementation(options) {
664
807
  const { config, logger } = options;
665
808
  if (config.output.clean) {
666
809
  await clean(config.output.path);
667
810
  }
668
811
  const queueTask = async (id, file) => {
669
812
  const { path } = file;
670
- let code = fileManager.getSource(file);
671
- const loadedResult = await pluginManager.hookFirst("load", [path]);
813
+ let code = getFileSource(file);
814
+ const loadedResult = await pluginManager.hookFirst({
815
+ hookName: "load",
816
+ parameters: [path]
817
+ });
672
818
  if (loadedResult) {
673
819
  code = loadedResult;
674
820
  }
675
821
  if (code) {
676
- const transformedCode = await pluginManager.hookReduceArg0("transform", [code, path], transformReducer);
822
+ const transformedCode = await pluginManager.hookReduceArg0({
823
+ hookName: "transform",
824
+ parameters: [code, path],
825
+ reduce: transformReducer
826
+ });
677
827
  if (config.output.write || config.output.write === void 0) {
678
- await pluginManager.hookParallel("writeFile", [transformedCode, path]);
828
+ await pluginManager.hookParallel({
829
+ hookName: "writeFile",
830
+ parameters: [transformedCode, path]
831
+ });
679
832
  }
680
833
  }
681
834
  };
682
835
  const pluginManager = new PluginManager(config, { logger, task: queueTask });
683
836
  const { plugins, fileManager } = pluginManager;
684
- await pluginManager.hookParallel("validate", [plugins]);
685
- await pluginManager.hookParallel("buildStart", [config]);
686
- await pluginManager.hookParallel("buildEnd");
687
- setTimeout(() => {
688
- done({ files: fileManager.files.map((file) => ({ ...file, source: fileManager.getSource(file) })) });
689
- }, 500);
837
+ await pluginManager.hookParallel({
838
+ hookName: "validate",
839
+ parameters: [plugins]
840
+ });
841
+ await pluginManager.hookParallel({
842
+ hookName: "buildStart",
843
+ parameters: [config]
844
+ });
690
845
  pluginManager.fileManager.add({
691
846
  path: isURL(config.input.path) ? config.input.path : pathParser2__default.default.resolve(config.root, config.input.path),
692
847
  fileName: isURL(config.input.path) ? "input" : config.input.path,
693
848
  source: isURL(config.input.path) ? config.input.path : await read(pathParser2__default.default.resolve(config.root, config.input.path))
694
849
  });
850
+ await pluginManager.hookParallel({ hookName: "buildEnd" });
851
+ return { files: fileManager.files.map((file) => ({ ...file, source: getFileSource(file) })) };
695
852
  }
696
853
  function build(options) {
697
854
  return new Promise(async (resolve, reject) => {
698
855
  try {
699
- await buildImplementation(options, resolve);
856
+ const output = await buildImplementation(options);
857
+ setTimeout(() => {
858
+ resolve(output);
859
+ }, 500);
700
860
  } catch (e) {
701
861
  reject(e);
702
862
  }
@@ -739,11 +899,14 @@ exports.TreeNode = TreeNode;
739
899
  exports.ValidationPluginError = ValidationPluginError;
740
900
  exports.build = build;
741
901
  exports.clean = clean;
902
+ exports.combineFiles = combineFiles;
742
903
  exports.createJSDocBlockText = createJSDocBlockText;
743
904
  exports.createPlugin = createPlugin;
744
905
  exports.createPluginCache = createPluginCache;
745
906
  exports.default = src_default;
746
907
  exports.defineConfig = defineConfig;
908
+ exports.getEncodedText = getEncodedText;
909
+ exports.getFileSource = getFileSource;
747
910
  exports.getPathMode = getPathMode;
748
911
  exports.getRelativePath = getRelativePath;
749
912
  exports.getUniqueName = getUniqueName;
@@ -754,6 +917,7 @@ exports.name = name;
754
917
  exports.nameSorter = nameSorter;
755
918
  exports.objectToParameters = objectToParameters;
756
919
  exports.read = read;
920
+ exports.renderTemplate = renderTemplate;
757
921
  exports.timeout = timeout;
758
922
  exports.validatePlugins = validatePlugins;
759
923
  exports.write = write;