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

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) => {
@@ -319,8 +310,13 @@ ${file.source}`;
319
310
  return file;
320
311
  }
321
312
  addOrAppend(file) {
313
+ if (!file.path.endsWith(file.fileName)) ;
322
314
  const previousCache = this.getCacheByPath(file.path);
323
315
  if (previousCache) {
316
+ const sourceAlreadyExists = previousCache.file.source.includes(file.source);
317
+ if (sourceAlreadyExists) {
318
+ return Promise.resolve(file);
319
+ }
324
320
  this.cache.delete(previousCache.id);
325
321
  return this.add({
326
322
  ...file,
@@ -331,26 +327,6 @@ ${file.source}`,
331
327
  }
332
328
  return this.add(file);
333
329
  }
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
330
  setStatus(id, status) {
355
331
  const cacheItem = this.getCache(id);
356
332
  if (!cacheItem) {
@@ -456,12 +432,69 @@ var TreeNode = class {
456
432
  return treeNode;
457
433
  }
458
434
  };
435
+ function combineFiles(files) {
436
+ return files.filter(Boolean).reduce((acc, curr) => {
437
+ if (!curr) {
438
+ return acc;
439
+ }
440
+ const prevIndex = acc.findIndex((item) => item.path === curr.path);
441
+ if (prevIndex !== -1) {
442
+ const prev = acc[prevIndex];
443
+ acc[prevIndex] = {
444
+ ...curr,
445
+ source: `${prev.source}
446
+ ${curr.source}`,
447
+ imports: [...prev.imports || [], ...curr.imports || []]
448
+ };
449
+ } else {
450
+ acc.push(curr);
451
+ }
452
+ return acc;
453
+ }, []);
454
+ }
455
+ function getFileSource(file) {
456
+ if (!file.fileName.endsWith(".ts")) {
457
+ return file.source;
458
+ }
459
+ const imports = [];
460
+ file.imports?.forEach((curr) => {
461
+ const exists = imports.find((imp) => imp.path === curr.path);
462
+ if (!exists) {
463
+ imports.push({
464
+ ...curr,
465
+ name: Array.isArray(curr.name) ? uniq__default.default(curr.name) : curr.name
466
+ });
467
+ }
468
+ if (exists && !Array.isArray(exists.name) && exists.name !== curr.name) {
469
+ imports.push(curr);
470
+ }
471
+ if (exists && Array.isArray(exists.name)) {
472
+ if (Array.isArray(curr.name)) {
473
+ exists.name = uniq__default.default([...exists.name, ...curr.name]);
474
+ }
475
+ }
476
+ });
477
+ const importSource = imports.reduce((prev, curr) => {
478
+ if (Array.isArray(curr.name)) {
479
+ return `${prev}
480
+ import ${curr.isTypeOnly ? "type " : ""}{ ${curr.name.join(", ")} } from "${curr.path}";`;
481
+ }
482
+ return `${prev}
483
+ import ${curr.isTypeOnly ? "type " : ""}${curr.name} from "${curr.path}";`;
484
+ }, "");
485
+ if (importSource) {
486
+ return `${importSource}
487
+ ${file.source}`;
488
+ }
489
+ return file.source;
490
+ }
459
491
 
460
492
  // src/managers/pluginManager/PluginManager.ts
461
493
  var hookNames = {
462
494
  validate: 1,
463
495
  buildStart: 1,
464
- resolveId: 1,
496
+ resolvePath: 1,
497
+ resolveName: 1,
465
498
  load: 1,
466
499
  transform: 1,
467
500
  writeFile: 1,
@@ -484,95 +517,196 @@ var PluginManager = class {
484
517
  config,
485
518
  fileManager: this.fileManager,
486
519
  load: this.load,
487
- resolveId: this.resolveId
520
+ resolvePath: this.resolvePath,
521
+ resolveName: this.resolveName
488
522
  });
489
523
  this.plugins = [this.core, ...config.plugins || []];
490
524
  }
491
- resolveId = (params) => {
525
+ resolvePath = (params) => {
492
526
  if (params.pluginName) {
493
- return this.hookForPlugin(params.pluginName, "resolveId", [params.fileName, params.directory, params.options]);
527
+ return this.hookForPluginSync({
528
+ pluginName: params.pluginName,
529
+ hookName: "resolvePath",
530
+ parameters: [params.fileName, params.directory, params.options]
531
+ });
494
532
  }
495
- return this.hookFirst("resolveId", [params.fileName, params.directory, params.options]);
533
+ return this.hookFirstSync({
534
+ hookName: "resolvePath",
535
+ parameters: [params.fileName, params.directory, params.options]
536
+ });
537
+ };
538
+ resolveName = (params) => {
539
+ if (params.pluginName) {
540
+ return this.hookForPluginSync({
541
+ pluginName: params.pluginName,
542
+ hookName: "resolveName",
543
+ parameters: [params.name]
544
+ });
545
+ }
546
+ return this.hookFirstSync({
547
+ hookName: "resolveName",
548
+ parameters: [params.name]
549
+ });
496
550
  };
497
551
  load = async (id) => {
498
- return this.hookFirst("load", [id]);
552
+ return this.hookFirst({
553
+ hookName: "load",
554
+ parameters: [id]
555
+ });
499
556
  };
500
- // run only hook for a specific plugin name
501
- hookForPlugin(pluginName, hookName, parameters, skipped) {
557
+ /**
558
+ *
559
+ * Run only hook for a specific plugin name
560
+ */
561
+ hookForPlugin({
562
+ pluginName,
563
+ hookName,
564
+ parameters
565
+ }) {
566
+ const plugin = this.getPlugin(hookName, pluginName);
567
+ return this.run({
568
+ strategy: "hookFirst",
569
+ hookName,
570
+ parameters,
571
+ plugin
572
+ });
573
+ }
574
+ hookForPluginSync({
575
+ pluginName,
576
+ hookName,
577
+ parameters
578
+ }) {
579
+ const plugin = this.getPlugin(hookName, pluginName);
580
+ return this.runSync({
581
+ strategy: "hookFirst",
582
+ hookName,
583
+ parameters,
584
+ plugin
585
+ });
586
+ }
587
+ /**
588
+ *
589
+ * Chains, first non-null result stops and returns
590
+ */
591
+ hookFirst({
592
+ hookName,
593
+ parameters,
594
+ skipped
595
+ }) {
502
596
  let promise = Promise.resolve(null);
503
- for (const plugin of this.getSortedPlugins(hookName, pluginName)) {
597
+ for (const plugin of this.getSortedPlugins(hookName)) {
504
598
  if (skipped && skipped.has(plugin))
505
599
  continue;
506
600
  promise = promise.then((result) => {
507
601
  if (result != null)
508
602
  return result;
509
- return this.run("hookFirst", hookName, parameters, plugin);
603
+ return this.run({
604
+ strategy: "hookFirst",
605
+ hookName,
606
+ parameters,
607
+ plugin
608
+ });
510
609
  });
511
610
  }
512
611
  return promise;
513
612
  }
514
- // chains, first non-null result stops and returns
515
- hookFirst(hookName, parameters, skipped) {
516
- let promise = Promise.resolve(null);
613
+ /**
614
+ *
615
+ * Chains, first non-null result stops and returns
616
+ */
617
+ hookFirstSync({
618
+ hookName,
619
+ parameters,
620
+ skipped
621
+ }) {
622
+ let result = null;
517
623
  for (const plugin of this.getSortedPlugins(hookName)) {
518
624
  if (skipped && skipped.has(plugin))
519
625
  continue;
520
- promise = promise.then((result) => {
521
- if (result != null)
522
- return result;
523
- return this.run("hookFirst", hookName, parameters, plugin);
626
+ result = this.runSync({
627
+ strategy: "hookFirst",
628
+ hookName,
629
+ parameters,
630
+ plugin
524
631
  });
632
+ if (result != null) {
633
+ break;
634
+ }
525
635
  }
526
- return promise;
636
+ return result;
527
637
  }
528
638
  // parallel
529
- async hookParallel(hookName, parameters) {
639
+ async hookParallel({
640
+ hookName,
641
+ parameters
642
+ }) {
530
643
  const parallelPromises = [];
531
644
  for (const plugin of this.getSortedPlugins(hookName)) {
532
645
  if (plugin[hookName]?.sequential) {
533
646
  await Promise.all(parallelPromises);
534
647
  parallelPromises.length = 0;
535
- await this.run("hookParallel", hookName, parameters, plugin);
648
+ await this.run({
649
+ strategy: "hookParallel",
650
+ hookName,
651
+ parameters,
652
+ plugin
653
+ });
536
654
  } else {
537
- const promise = this.run("hookParallel", hookName, parameters, plugin);
655
+ const promise = this.run({ strategy: "hookParallel", hookName, parameters, plugin });
538
656
  parallelPromises.push(promise);
539
657
  }
540
658
  }
541
659
  return Promise.all(parallelPromises);
542
660
  }
543
661
  // chains, reduces returned value, handling the reduced value as the first hook argument
544
- hookReduceArg0(hookName, [argument0, ...rest], reduce) {
662
+ hookReduceArg0({
663
+ hookName,
664
+ parameters,
665
+ reduce
666
+ }) {
667
+ const [argument0, ...rest] = parameters;
545
668
  let promise = Promise.resolve(argument0);
546
669
  for (const plugin of this.getSortedPlugins(hookName)) {
547
670
  promise = promise.then(
548
- (argument02) => this.run("hookReduceArg0", hookName, [argument02, ...rest], plugin).then(
549
- (result) => reduce.call(this.core.api, argument02, result, plugin)
550
- )
671
+ (argument02) => this.run({
672
+ strategy: "hookReduceArg0",
673
+ hookName,
674
+ parameters: [argument02, ...rest],
675
+ plugin
676
+ }).then((result) => reduce.call(this.core.api, argument02, result, plugin))
551
677
  );
552
678
  }
553
679
  return promise;
554
680
  }
555
681
  // chains
556
- hookSeq(hookName, parameters) {
682
+ hookSeq({ hookName, parameters }) {
557
683
  let promise = Promise.resolve();
558
684
  for (const plugin of this.getSortedPlugins(hookName)) {
559
- promise = promise.then(() => this.run("hookSeq", hookName, parameters, plugin));
685
+ promise = promise.then(
686
+ () => this.run({
687
+ strategy: "hookSeq",
688
+ hookName,
689
+ parameters,
690
+ plugin
691
+ })
692
+ );
560
693
  }
561
694
  return promise.then(noReturn);
562
695
  }
563
- getSortedPlugins(hookName, pluginName) {
696
+ getSortedPlugins(_hookName) {
564
697
  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];
698
+ return plugins;
699
+ }
700
+ getPlugin(hookName, pluginName) {
701
+ const plugins = [...this.plugins];
702
+ const pluginByPluginName = plugins.find((item) => item.name === pluginName && item[hookName]);
703
+ if (!pluginByPluginName) {
704
+ if (this.config.logLevel === "warn" && this.logger?.spinner) {
705
+ this.logger.spinner.info(`Plugin hook with ${hookName} not found for plugin ${pluginName}`);
572
706
  }
573
- return pluginsByPluginName;
707
+ return this.core;
574
708
  }
575
- return plugins;
709
+ return pluginByPluginName;
576
710
  }
577
711
  /**
578
712
  * Run an async plugin hook and return the result.
@@ -581,7 +715,12 @@ var PluginManager = class {
581
715
  * @param plugin The actual pluginObject to run.
582
716
  */
583
717
  // Implementation signature
584
- run(strategy, hookName, parameters, plugin) {
718
+ run({
719
+ strategy,
720
+ hookName,
721
+ parameters,
722
+ plugin
723
+ }) {
585
724
  const hook = plugin[hookName];
586
725
  return Promise.resolve().then(() => {
587
726
  if (typeof hook !== "function") {
@@ -617,12 +756,21 @@ var PluginManager = class {
617
756
  * @param plugin The acutal plugin
618
757
  * @param replaceContext When passed, the plugin context can be overridden.
619
758
  */
620
- runSync(hookName, parameters, plugin) {
759
+ runSync({
760
+ strategy,
761
+ hookName,
762
+ parameters,
763
+ plugin
764
+ }) {
621
765
  const hook = plugin[hookName];
622
766
  try {
767
+ if (typeof hook !== "function") {
768
+ return hook;
769
+ }
623
770
  return hook.apply(this.core.api, parameters);
624
- } catch (error) {
625
- return error;
771
+ } catch (e) {
772
+ this.catcher(e, plugin, hookName);
773
+ return null;
626
774
  }
627
775
  }
628
776
  catcher(e, plugin, hookName) {
@@ -660,43 +808,60 @@ async function transformReducer(_previousCode, result, _plugin) {
660
808
  }
661
809
  return result;
662
810
  }
663
- async function buildImplementation(options, done) {
811
+ async function buildImplementation(options) {
664
812
  const { config, logger } = options;
665
813
  if (config.output.clean) {
666
814
  await clean(config.output.path);
667
815
  }
668
816
  const queueTask = async (id, file) => {
669
817
  const { path } = file;
670
- let code = fileManager.getSource(file);
671
- const loadedResult = await pluginManager.hookFirst("load", [path]);
818
+ let code = getFileSource(file);
819
+ const loadedResult = await pluginManager.hookFirst({
820
+ hookName: "load",
821
+ parameters: [path]
822
+ });
672
823
  if (loadedResult) {
673
824
  code = loadedResult;
674
825
  }
675
826
  if (code) {
676
- const transformedCode = await pluginManager.hookReduceArg0("transform", [code, path], transformReducer);
827
+ const transformedCode = await pluginManager.hookReduceArg0({
828
+ hookName: "transform",
829
+ parameters: [code, path],
830
+ reduce: transformReducer
831
+ });
677
832
  if (config.output.write || config.output.write === void 0) {
678
- await pluginManager.hookParallel("writeFile", [transformedCode, path]);
833
+ await pluginManager.hookParallel({
834
+ hookName: "writeFile",
835
+ parameters: [transformedCode, path]
836
+ });
679
837
  }
680
838
  }
681
839
  };
682
840
  const pluginManager = new PluginManager(config, { logger, task: queueTask });
683
841
  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);
842
+ await pluginManager.hookParallel({
843
+ hookName: "validate",
844
+ parameters: [plugins]
845
+ });
846
+ await pluginManager.hookParallel({
847
+ hookName: "buildStart",
848
+ parameters: [config]
849
+ });
690
850
  pluginManager.fileManager.add({
691
851
  path: isURL(config.input.path) ? config.input.path : pathParser2__default.default.resolve(config.root, config.input.path),
692
852
  fileName: isURL(config.input.path) ? "input" : config.input.path,
693
853
  source: isURL(config.input.path) ? config.input.path : await read(pathParser2__default.default.resolve(config.root, config.input.path))
694
854
  });
855
+ await pluginManager.hookParallel({ hookName: "buildEnd" });
856
+ return { files: fileManager.files.map((file) => ({ ...file, source: getFileSource(file) })) };
695
857
  }
696
858
  function build(options) {
697
859
  return new Promise(async (resolve, reject) => {
698
860
  try {
699
- await buildImplementation(options, resolve);
861
+ const output = await buildImplementation(options);
862
+ setTimeout(() => {
863
+ resolve(output);
864
+ }, 500);
700
865
  } catch (e) {
701
866
  reject(e);
702
867
  }
@@ -739,11 +904,14 @@ exports.TreeNode = TreeNode;
739
904
  exports.ValidationPluginError = ValidationPluginError;
740
905
  exports.build = build;
741
906
  exports.clean = clean;
907
+ exports.combineFiles = combineFiles;
742
908
  exports.createJSDocBlockText = createJSDocBlockText;
743
909
  exports.createPlugin = createPlugin;
744
910
  exports.createPluginCache = createPluginCache;
745
911
  exports.default = src_default;
746
912
  exports.defineConfig = defineConfig;
913
+ exports.getEncodedText = getEncodedText;
914
+ exports.getFileSource = getFileSource;
747
915
  exports.getPathMode = getPathMode;
748
916
  exports.getRelativePath = getRelativePath;
749
917
  exports.getUniqueName = getUniqueName;
@@ -754,6 +922,7 @@ exports.name = name;
754
922
  exports.nameSorter = nameSorter;
755
923
  exports.objectToParameters = objectToParameters;
756
924
  exports.read = read;
925
+ exports.renderTemplate = renderTemplate;
757
926
  exports.timeout = timeout;
758
927
  exports.validatePlugins = validatePlugins;
759
928
  exports.write = write;