@kubb/core 1.0.0-beta.3 → 1.0.0-beta.5

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}
@@ -225,7 +225,7 @@ function createPlugin(factory) {
225
225
  }
226
226
  var name = "core";
227
227
  var definePlugin = createPlugin((options) => {
228
- const { fileManager, resolveId, load } = options;
228
+ const { fileManager, resolvePath, resolveName, load } = options;
229
229
  const api = {
230
230
  get config() {
231
231
  return options.config;
@@ -234,7 +234,8 @@ var definePlugin = createPlugin((options) => {
234
234
  async addFile(file) {
235
235
  return fileManager.addOrAppend(file);
236
236
  },
237
- resolveId,
237
+ resolvePath,
238
+ resolveName,
238
239
  load,
239
240
  cache: createPluginCache(/* @__PURE__ */ Object.create(null))
240
241
  };
@@ -242,11 +243,14 @@ var definePlugin = createPlugin((options) => {
242
243
  name,
243
244
  options,
244
245
  api,
245
- resolveId(fileName, directory) {
246
+ resolvePath(fileName, directory) {
246
247
  if (!directory) {
247
248
  return null;
248
249
  }
249
250
  return pathParser2__default.default.resolve(directory, fileName);
251
+ },
252
+ resolveName(name2) {
253
+ return name2;
250
254
  }
251
255
  };
252
256
  });
@@ -272,42 +276,6 @@ var FileManager = class {
272
276
  });
273
277
  return cache;
274
278
  }
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({
284
- ...curr,
285
- name: Array.isArray(curr.name) ? uniq__default.default(curr.name) : curr.name
286
- });
287
- }
288
- if (exists && !Array.isArray(exists.name) && exists.name !== curr.name) {
289
- imports.push(curr);
290
- }
291
- if (exists && Array.isArray(exists.name)) {
292
- if (Array.isArray(curr.name)) {
293
- exists.name = uniq__default.default([...exists.name, ...curr.name]);
294
- }
295
- }
296
- });
297
- const importSource = imports.reduce((prev, curr) => {
298
- if (Array.isArray(curr.name)) {
299
- return `${prev}
300
- import ${curr.isTypeOnly ? "type " : ""}{ ${curr.name.join(", ")} } from "${curr.path}";`;
301
- }
302
- return `${prev}
303
- import ${curr.isTypeOnly ? "type " : ""}${curr.name} from "${curr.path}";`;
304
- }, "");
305
- if (importSource) {
306
- return `${importSource}
307
- ${file.source}`;
308
- }
309
- return file.source;
310
- }
311
279
  get files() {
312
280
  const files = [];
313
281
  this.cache.forEach((item) => {
@@ -338,26 +306,6 @@ ${file.source}`,
338
306
  }
339
307
  return this.add(file);
340
308
  }
341
- combine(files) {
342
- return files.filter(Boolean).reduce((acc, curr) => {
343
- if (!curr) {
344
- return acc;
345
- }
346
- const prevIndex = acc.findIndex((item) => item.path === curr.path);
347
- if (prevIndex !== -1) {
348
- const prev = acc[prevIndex];
349
- acc[prevIndex] = {
350
- ...curr,
351
- source: `${prev.source}
352
- ${curr.source}`,
353
- imports: [...prev.imports || [], ...curr.imports || []]
354
- };
355
- } else {
356
- acc.push(curr);
357
- }
358
- return acc;
359
- }, []);
360
- }
361
309
  setStatus(id, status) {
362
310
  const cacheItem = this.getCache(id);
363
311
  if (!cacheItem) {
@@ -463,12 +411,69 @@ var TreeNode = class {
463
411
  return treeNode;
464
412
  }
465
413
  };
414
+ function combineFiles(files) {
415
+ return files.filter(Boolean).reduce((acc, curr) => {
416
+ if (!curr) {
417
+ return acc;
418
+ }
419
+ const prevIndex = acc.findIndex((item) => item.path === curr.path);
420
+ if (prevIndex !== -1) {
421
+ const prev = acc[prevIndex];
422
+ acc[prevIndex] = {
423
+ ...curr,
424
+ source: `${prev.source}
425
+ ${curr.source}`,
426
+ imports: [...prev.imports || [], ...curr.imports || []]
427
+ };
428
+ } else {
429
+ acc.push(curr);
430
+ }
431
+ return acc;
432
+ }, []);
433
+ }
434
+ function getFileSource(file) {
435
+ if (!file.fileName.endsWith(".ts")) {
436
+ return file.source;
437
+ }
438
+ const imports = [];
439
+ file.imports?.forEach((curr) => {
440
+ const exists = imports.find((imp) => imp.path === curr.path);
441
+ if (!exists) {
442
+ imports.push({
443
+ ...curr,
444
+ name: Array.isArray(curr.name) ? uniq__default.default(curr.name) : curr.name
445
+ });
446
+ }
447
+ if (exists && !Array.isArray(exists.name) && exists.name !== curr.name) {
448
+ imports.push(curr);
449
+ }
450
+ if (exists && Array.isArray(exists.name)) {
451
+ if (Array.isArray(curr.name)) {
452
+ exists.name = uniq__default.default([...exists.name, ...curr.name]);
453
+ }
454
+ }
455
+ });
456
+ const importSource = imports.reduce((prev, curr) => {
457
+ if (Array.isArray(curr.name)) {
458
+ return `${prev}
459
+ import ${curr.isTypeOnly ? "type " : ""}{ ${curr.name.join(", ")} } from "${curr.path}";`;
460
+ }
461
+ return `${prev}
462
+ import ${curr.isTypeOnly ? "type " : ""}${curr.name} from "${curr.path}";`;
463
+ }, "");
464
+ if (importSource) {
465
+ return `${importSource}
466
+ ${file.source}`;
467
+ }
468
+ return file.source;
469
+ }
466
470
 
467
471
  // src/managers/pluginManager/PluginManager.ts
468
472
  var hookNames = {
469
473
  validate: 1,
470
474
  buildStart: 1,
471
- resolveId: 1,
475
+ resolvePath: 1,
476
+ resolveName: 1,
472
477
  load: 1,
473
478
  transform: 1,
474
479
  writeFile: 1,
@@ -491,95 +496,196 @@ var PluginManager = class {
491
496
  config,
492
497
  fileManager: this.fileManager,
493
498
  load: this.load,
494
- resolveId: this.resolveId
499
+ resolvePath: this.resolvePath,
500
+ resolveName: this.resolveName
495
501
  });
496
502
  this.plugins = [this.core, ...config.plugins || []];
497
503
  }
498
- resolveId = (params) => {
504
+ resolvePath = (params) => {
505
+ if (params.pluginName) {
506
+ return this.hookForPluginSync({
507
+ pluginName: params.pluginName,
508
+ hookName: "resolvePath",
509
+ parameters: [params.fileName, params.directory, params.options]
510
+ });
511
+ }
512
+ return this.hookFirstSync({
513
+ hookName: "resolvePath",
514
+ parameters: [params.fileName, params.directory, params.options]
515
+ });
516
+ };
517
+ resolveName = (params) => {
499
518
  if (params.pluginName) {
500
- return this.hookForPlugin(params.pluginName, "resolveId", [params.fileName, params.directory, params.options]);
519
+ return this.hookForPluginSync({
520
+ pluginName: params.pluginName,
521
+ hookName: "resolveName",
522
+ parameters: [params.name]
523
+ });
501
524
  }
502
- return this.hookFirst("resolveId", [params.fileName, params.directory, params.options]);
525
+ return this.hookFirstSync({
526
+ hookName: "resolveName",
527
+ parameters: [params.name]
528
+ });
503
529
  };
504
530
  load = async (id) => {
505
- return this.hookFirst("load", [id]);
531
+ return this.hookFirst({
532
+ hookName: "load",
533
+ parameters: [id]
534
+ });
506
535
  };
507
- // run only hook for a specific plugin name
508
- hookForPlugin(pluginName, hookName, parameters, skipped) {
536
+ /**
537
+ *
538
+ * Run only hook for a specific plugin name
539
+ */
540
+ hookForPlugin({
541
+ pluginName,
542
+ hookName,
543
+ parameters
544
+ }) {
545
+ const plugin = this.getPlugin(hookName, pluginName);
546
+ return this.run({
547
+ strategy: "hookFirst",
548
+ hookName,
549
+ parameters,
550
+ plugin
551
+ });
552
+ }
553
+ hookForPluginSync({
554
+ pluginName,
555
+ hookName,
556
+ parameters
557
+ }) {
558
+ const plugin = this.getPlugin(hookName, pluginName);
559
+ return this.runSync({
560
+ strategy: "hookFirst",
561
+ hookName,
562
+ parameters,
563
+ plugin
564
+ });
565
+ }
566
+ /**
567
+ *
568
+ * Chains, first non-null result stops and returns
569
+ */
570
+ hookFirst({
571
+ hookName,
572
+ parameters,
573
+ skipped
574
+ }) {
509
575
  let promise = Promise.resolve(null);
510
- for (const plugin of this.getSortedPlugins(hookName, pluginName)) {
576
+ for (const plugin of this.getSortedPlugins(hookName)) {
511
577
  if (skipped && skipped.has(plugin))
512
578
  continue;
513
579
  promise = promise.then((result) => {
514
580
  if (result != null)
515
581
  return result;
516
- return this.run("hookFirst", hookName, parameters, plugin);
582
+ return this.run({
583
+ strategy: "hookFirst",
584
+ hookName,
585
+ parameters,
586
+ plugin
587
+ });
517
588
  });
518
589
  }
519
590
  return promise;
520
591
  }
521
- // chains, first non-null result stops and returns
522
- hookFirst(hookName, parameters, skipped) {
523
- let promise = Promise.resolve(null);
592
+ /**
593
+ *
594
+ * Chains, first non-null result stops and returns
595
+ */
596
+ hookFirstSync({
597
+ hookName,
598
+ parameters,
599
+ skipped
600
+ }) {
601
+ let result = null;
524
602
  for (const plugin of this.getSortedPlugins(hookName)) {
525
603
  if (skipped && skipped.has(plugin))
526
604
  continue;
527
- promise = promise.then((result) => {
528
- if (result != null)
529
- return result;
530
- return this.run("hookFirst", hookName, parameters, plugin);
605
+ result = this.runSync({
606
+ strategy: "hookFirst",
607
+ hookName,
608
+ parameters,
609
+ plugin
531
610
  });
611
+ if (result != null) {
612
+ break;
613
+ }
532
614
  }
533
- return promise;
615
+ return result;
534
616
  }
535
617
  // parallel
536
- async hookParallel(hookName, parameters) {
618
+ async hookParallel({
619
+ hookName,
620
+ parameters
621
+ }) {
537
622
  const parallelPromises = [];
538
623
  for (const plugin of this.getSortedPlugins(hookName)) {
539
624
  if (plugin[hookName]?.sequential) {
540
625
  await Promise.all(parallelPromises);
541
626
  parallelPromises.length = 0;
542
- await this.run("hookParallel", hookName, parameters, plugin);
627
+ await this.run({
628
+ strategy: "hookParallel",
629
+ hookName,
630
+ parameters,
631
+ plugin
632
+ });
543
633
  } else {
544
- const promise = this.run("hookParallel", hookName, parameters, plugin);
634
+ const promise = this.run({ strategy: "hookParallel", hookName, parameters, plugin });
545
635
  parallelPromises.push(promise);
546
636
  }
547
637
  }
548
638
  return Promise.all(parallelPromises);
549
639
  }
550
640
  // chains, reduces returned value, handling the reduced value as the first hook argument
551
- hookReduceArg0(hookName, [argument0, ...rest], reduce) {
641
+ hookReduceArg0({
642
+ hookName,
643
+ parameters,
644
+ reduce
645
+ }) {
646
+ const [argument0, ...rest] = parameters;
552
647
  let promise = Promise.resolve(argument0);
553
648
  for (const plugin of this.getSortedPlugins(hookName)) {
554
649
  promise = promise.then(
555
- (argument02) => this.run("hookReduceArg0", hookName, [argument02, ...rest], plugin).then(
556
- (result) => reduce.call(this.core.api, argument02, result, plugin)
557
- )
650
+ (argument02) => this.run({
651
+ strategy: "hookReduceArg0",
652
+ hookName,
653
+ parameters: [argument02, ...rest],
654
+ plugin
655
+ }).then((result) => reduce.call(this.core.api, argument02, result, plugin))
558
656
  );
559
657
  }
560
658
  return promise;
561
659
  }
562
660
  // chains
563
- hookSeq(hookName, parameters) {
661
+ hookSeq({ hookName, parameters }) {
564
662
  let promise = Promise.resolve();
565
663
  for (const plugin of this.getSortedPlugins(hookName)) {
566
- promise = promise.then(() => this.run("hookSeq", hookName, parameters, plugin));
664
+ promise = promise.then(
665
+ () => this.run({
666
+ strategy: "hookSeq",
667
+ hookName,
668
+ parameters,
669
+ plugin
670
+ })
671
+ );
567
672
  }
568
673
  return promise.then(noReturn);
569
674
  }
570
- getSortedPlugins(hookName, pluginName) {
675
+ getSortedPlugins(_hookName) {
571
676
  const plugins = [...this.plugins];
572
- if (pluginName) {
573
- const pluginsByPluginName = plugins.filter((item) => item.name === pluginName && item[hookName]);
574
- if (pluginsByPluginName.length === 0) {
575
- if (this.config.logLevel === "warn" && this.logger?.spinner) {
576
- this.logger.spinner.info(`Plugin hook with ${hookName} not found for plugin ${pluginName}`);
577
- }
578
- return [this.core];
677
+ return plugins;
678
+ }
679
+ getPlugin(hookName, pluginName) {
680
+ const plugins = [...this.plugins];
681
+ const pluginByPluginName = plugins.find((item) => item.name === pluginName && item[hookName]);
682
+ if (!pluginByPluginName) {
683
+ if (this.config.logLevel === "warn" && this.logger?.spinner) {
684
+ this.logger.spinner.info(`Plugin hook with ${hookName} not found for plugin ${pluginName}`);
579
685
  }
580
- return pluginsByPluginName;
686
+ return this.core;
581
687
  }
582
- return plugins;
688
+ return pluginByPluginName;
583
689
  }
584
690
  /**
585
691
  * Run an async plugin hook and return the result.
@@ -588,7 +694,12 @@ var PluginManager = class {
588
694
  * @param plugin The actual pluginObject to run.
589
695
  */
590
696
  // Implementation signature
591
- run(strategy, hookName, parameters, plugin) {
697
+ run({
698
+ strategy,
699
+ hookName,
700
+ parameters,
701
+ plugin
702
+ }) {
592
703
  const hook = plugin[hookName];
593
704
  return Promise.resolve().then(() => {
594
705
  if (typeof hook !== "function") {
@@ -624,12 +735,21 @@ var PluginManager = class {
624
735
  * @param plugin The acutal plugin
625
736
  * @param replaceContext When passed, the plugin context can be overridden.
626
737
  */
627
- runSync(hookName, parameters, plugin) {
738
+ runSync({
739
+ strategy,
740
+ hookName,
741
+ parameters,
742
+ plugin
743
+ }) {
628
744
  const hook = plugin[hookName];
629
745
  try {
746
+ if (typeof hook !== "function") {
747
+ return hook;
748
+ }
630
749
  return hook.apply(this.core.api, parameters);
631
- } catch (error) {
632
- return error;
750
+ } catch (e) {
751
+ this.catcher(e, plugin, hookName);
752
+ return null;
633
753
  }
634
754
  }
635
755
  catcher(e, plugin, hookName) {
@@ -674,25 +794,41 @@ async function buildImplementation(options, done) {
674
794
  }
675
795
  const queueTask = async (id, file) => {
676
796
  const { path } = file;
677
- let code = fileManager.getSource(file);
678
- const loadedResult = await pluginManager.hookFirst("load", [path]);
797
+ let code = getFileSource(file);
798
+ const loadedResult = await pluginManager.hookFirst({
799
+ hookName: "load",
800
+ parameters: [path]
801
+ });
679
802
  if (loadedResult) {
680
803
  code = loadedResult;
681
804
  }
682
805
  if (code) {
683
- const transformedCode = await pluginManager.hookReduceArg0("transform", [code, path], transformReducer);
806
+ const transformedCode = await pluginManager.hookReduceArg0({
807
+ hookName: "transform",
808
+ parameters: [code, path],
809
+ reduce: transformReducer
810
+ });
684
811
  if (config.output.write || config.output.write === void 0) {
685
- await pluginManager.hookParallel("writeFile", [transformedCode, path]);
812
+ await pluginManager.hookParallel({
813
+ hookName: "writeFile",
814
+ parameters: [transformedCode, path]
815
+ });
686
816
  }
687
817
  }
688
818
  };
689
819
  const pluginManager = new PluginManager(config, { logger, task: queueTask });
690
820
  const { plugins, fileManager } = pluginManager;
691
- await pluginManager.hookParallel("validate", [plugins]);
692
- await pluginManager.hookParallel("buildStart", [config]);
693
- await pluginManager.hookParallel("buildEnd");
821
+ await pluginManager.hookParallel({
822
+ hookName: "validate",
823
+ parameters: [plugins]
824
+ });
825
+ await pluginManager.hookParallel({
826
+ hookName: "buildStart",
827
+ parameters: [config]
828
+ });
829
+ await pluginManager.hookParallel({ hookName: "buildEnd" });
694
830
  setTimeout(() => {
695
- done({ files: fileManager.files.map((file) => ({ ...file, source: fileManager.getSource(file) })) });
831
+ done({ files: fileManager.files.map((file) => ({ ...file, source: getFileSource(file) })) });
696
832
  }, 500);
697
833
  pluginManager.fileManager.add({
698
834
  path: isURL(config.input.path) ? config.input.path : pathParser2__default.default.resolve(config.root, config.input.path),
@@ -746,11 +882,13 @@ exports.TreeNode = TreeNode;
746
882
  exports.ValidationPluginError = ValidationPluginError;
747
883
  exports.build = build;
748
884
  exports.clean = clean;
885
+ exports.combineFiles = combineFiles;
749
886
  exports.createJSDocBlockText = createJSDocBlockText;
750
887
  exports.createPlugin = createPlugin;
751
888
  exports.createPluginCache = createPluginCache;
752
889
  exports.default = src_default;
753
890
  exports.defineConfig = defineConfig;
891
+ exports.getFileSource = getFileSource;
754
892
  exports.getPathMode = getPathMode;
755
893
  exports.getRelativePath = getRelativePath;
756
894
  exports.getUniqueName = getUniqueName;