@kubb/core 1.0.0-beta.13 → 1.0.0-beta.15

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,9 +6,10 @@ 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 uuid = require('uuid');
10
9
  var dirTree = require('directory-tree');
10
+ var uuid = require('uuid');
11
11
  var uniq = require('lodash.uniq');
12
+ var tsCodegen = require('@kubb/ts-codegen');
12
13
 
13
14
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
14
15
 
@@ -19,19 +20,6 @@ var uniq__default = /*#__PURE__*/_interopDefault(uniq);
19
20
 
20
21
  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
22
 
22
- // src/utils/isURL.ts
23
- function isURL(data) {
24
- try {
25
- const url = new URL(data);
26
- if (url?.href) {
27
- return true;
28
- }
29
- } catch (error) {
30
- return false;
31
- }
32
- return false;
33
- }
34
-
35
23
  // src/utils/isPromise.ts
36
24
  function isPromise(result) {
37
25
  return typeof result?.then === "function";
@@ -112,6 +100,19 @@ async function read(path) {
112
100
  }
113
101
  }
114
102
 
103
+ // src/utils/isURL.ts
104
+ function isURL(data) {
105
+ try {
106
+ const url = new URL(data);
107
+ if (url?.href) {
108
+ return true;
109
+ }
110
+ } catch (error) {
111
+ return false;
112
+ }
113
+ return false;
114
+ }
115
+
115
116
  // src/utils/objectToParameters.ts
116
117
  function objectToParameters(data, options = {}) {
117
118
  return data.reduce((acc, [key, value]) => {
@@ -223,6 +224,85 @@ async function clean(path) {
223
224
  });
224
225
  });
225
226
  }
227
+ var TreeNode = class {
228
+ data;
229
+ parent;
230
+ children = [];
231
+ constructor(data, parent) {
232
+ this.data = data;
233
+ this.parent = parent;
234
+ return this;
235
+ }
236
+ addChild(data) {
237
+ const child = new TreeNode(data, this);
238
+ if (!this.children) {
239
+ this.children = [];
240
+ }
241
+ this.children.push(child);
242
+ return child;
243
+ }
244
+ find(data) {
245
+ if (data === this.data) {
246
+ return this;
247
+ }
248
+ if (this.children) {
249
+ for (let i = 0, { length } = this.children, target = null; i < length; i++) {
250
+ target = this.children[i].find(data);
251
+ if (target) {
252
+ return target;
253
+ }
254
+ }
255
+ }
256
+ return null;
257
+ }
258
+ leaves() {
259
+ if (!this.children || this.children.length === 0) {
260
+ return [this];
261
+ }
262
+ const leaves = [];
263
+ if (this.children) {
264
+ for (let i = 0, { length } = this.children; i < length; i++) {
265
+ leaves.push.apply(leaves, this.children[i].leaves());
266
+ }
267
+ }
268
+ return leaves;
269
+ }
270
+ root() {
271
+ if (!this.parent) {
272
+ return this;
273
+ }
274
+ return this.parent.root();
275
+ }
276
+ forEach(callback) {
277
+ if (typeof callback !== "function") {
278
+ throw new TypeError("forEach() callback must be a function");
279
+ }
280
+ callback(this);
281
+ if (this.children) {
282
+ for (let i = 0, { length } = this.children; i < length; i++) {
283
+ this.children[i].forEach(callback);
284
+ }
285
+ }
286
+ return this;
287
+ }
288
+ static build(path, options = {}) {
289
+ const filteredTree = dirTree__default.default(path, { extensions: options?.extensions, exclude: options.exclude });
290
+ if (!filteredTree) {
291
+ return null;
292
+ }
293
+ const treeNode = new TreeNode({ name: filteredTree.name, path: filteredTree.path, type: filteredTree.type });
294
+ const recurse = (node, item) => {
295
+ const subNode = node.addChild({ name: item.name, path: item.path, type: item.type });
296
+ if (item.children?.length) {
297
+ item.children?.forEach((child) => {
298
+ recurse(subNode, child);
299
+ });
300
+ }
301
+ };
302
+ filteredTree.children?.forEach((child) => recurse(treeNode, child));
303
+ return treeNode;
304
+ }
305
+ };
226
306
 
227
307
  // src/plugin.ts
228
308
  function createPlugin(factory) {
@@ -248,7 +328,7 @@ var definePlugin = createPlugin((options) => {
248
328
  },
249
329
  fileManager,
250
330
  async addFile(...files) {
251
- return Promise.all(files.map((file) => fileManager.addOrAppend(file)));
331
+ return Promise.all(files.map((file) => file.override ? fileManager.add(file) : fileManager.addOrAppend(file)));
252
332
  },
253
333
  resolvePath,
254
334
  resolveName,
@@ -311,7 +391,7 @@ var FileManager = class {
311
391
  if (!file.path.endsWith(file.fileName)) ;
312
392
  const previousCache = this.getCacheByPath(file.path);
313
393
  if (previousCache) {
314
- const sourceAlreadyExists = previousCache.file.source.includes(file.source);
394
+ const sourceAlreadyExists = file.source && previousCache.file.source.includes(file.source);
315
395
  if (sourceAlreadyExists) {
316
396
  return Promise.resolve(file);
317
397
  }
@@ -320,7 +400,8 @@ var FileManager = class {
320
400
  ...file,
321
401
  source: `${previousCache.file.source}
322
402
  ${file.source}`,
323
- imports: [...previousCache.file.imports || [], ...file.imports || []]
403
+ imports: [...previousCache.file.imports || [], ...file.imports || []],
404
+ exports: [...previousCache.file.exports || [], ...file.exports || []]
324
405
  });
325
406
  }
326
407
  return this.add(file);
@@ -351,85 +432,53 @@ ${file.source}`,
351
432
  return read(...params);
352
433
  }
353
434
  };
354
- var TreeNode = class {
355
- data;
356
- parent;
357
- children = [];
358
- constructor(data, parent) {
359
- this.data = data;
360
- this.parent = parent;
361
- return this;
362
- }
363
- addChild(data) {
364
- const child = new TreeNode(data, this);
365
- if (!this.children) {
366
- this.children = [];
367
- }
368
- this.children.push(child);
369
- return child;
370
- }
371
- find(data) {
372
- if (data === this.data) {
373
- return this;
374
- }
375
- if (this.children) {
376
- for (let i = 0, { length } = this.children, target = null; i < length; i++) {
377
- target = this.children[i].find(data);
378
- if (target) {
379
- return target;
435
+ function writeIndexes(root, options) {
436
+ const tree = TreeNode.build(root, { extensions: /\.ts/, ...options });
437
+ if (!tree) {
438
+ return void 0;
439
+ }
440
+ const fileReducer = (files2, item) => {
441
+ if (!item.children) {
442
+ return [];
443
+ }
444
+ if (item.children?.length > 1) {
445
+ const path = pathParser2__default.default.resolve(item.data.path, "index.ts");
446
+ const exports = item.children.map((file) => {
447
+ if (!file) {
448
+ return void 0;
380
449
  }
381
- }
382
- }
383
- return null;
384
- }
385
- leaves() {
386
- if (!this.children || this.children.length === 0) {
387
- return [this];
388
- }
389
- const leaves = [];
390
- if (this.children) {
391
- for (let i = 0, { length } = this.children; i < length; i++) {
392
- leaves.push.apply(leaves, this.children[i].leaves());
393
- }
394
- }
395
- return leaves;
396
- }
397
- root() {
398
- if (!this.parent) {
399
- return this;
400
- }
401
- return this.parent.root();
402
- }
403
- forEach(callback) {
404
- if (typeof callback !== "function") {
405
- throw new TypeError("forEach() callback must be a function");
406
- }
407
- callback(this);
408
- if (this.children) {
409
- for (let i = 0, { length } = this.children; i < length; i++) {
410
- this.children[i].forEach(callback);
411
- }
412
- }
413
- return this;
414
- }
415
- static build(path, options = {}) {
416
- const filteredTree = dirTree__default.default(path, { extensions: options?.extensions, exclude: options.exclude });
417
- if (!filteredTree) {
418
- return null;
419
- }
420
- const treeNode = new TreeNode({ name: filteredTree.name, path: filteredTree.path, type: filteredTree.type });
421
- const recurse = (node, item) => {
422
- const subNode = node.addChild({ name: item.name, path: item.path, type: item.type });
423
- if (item.children?.length) {
424
- item.children?.forEach((child) => {
425
- recurse(subNode, child);
450
+ const importPath = file.data.type === "directory" ? `./${file.data.name}` : `./${file.data.name.replace(/\.[^.]*$/, "")}`;
451
+ if (importPath.includes("index") && path.includes("index")) {
452
+ return void 0;
453
+ }
454
+ return { path: importPath };
455
+ }).filter(Boolean);
456
+ files2.push({
457
+ path,
458
+ fileName: "index.ts",
459
+ source: "",
460
+ exports
461
+ });
462
+ } else {
463
+ item.children?.forEach((child) => {
464
+ const path = pathParser2__default.default.resolve(item.data.path, "index.ts");
465
+ const importPath = child.data.type === "directory" ? `./${child.data.name}` : `./${child.data.name.replace(/\.[^.]*$/, "")}`;
466
+ files2.push({
467
+ path,
468
+ fileName: "index.ts",
469
+ source: "",
470
+ exports: [{ path: importPath }]
426
471
  });
427
- }
428
- };
429
- filteredTree.children?.forEach((child) => recurse(treeNode, child));
430
- return treeNode;
431
- }
432
- };
472
+ });
473
+ }
474
+ item.children.forEach((childItem) => {
475
+ fileReducer(files2, childItem);
476
+ });
477
+ return files2;
478
+ };
479
+ const files = fileReducer([], tree);
480
+ return files;
481
+ }
433
482
  function combineFiles(files) {
434
483
  return files.filter(Boolean).reduce((acc, curr) => {
435
484
  if (!curr) {
@@ -442,7 +491,8 @@ function combineFiles(files) {
442
491
  ...curr,
443
492
  source: `${prev.source}
444
493
  ${curr.source}`,
445
- imports: [...prev.imports || [], ...curr.imports || []]
494
+ imports: [...prev.imports || [], ...curr.imports || []],
495
+ exports: [...prev.exports || [], ...curr.exports || []]
446
496
  };
447
497
  } else {
448
498
  acc.push(curr);
@@ -451,10 +501,12 @@ ${curr.source}`,
451
501
  }, []);
452
502
  }
453
503
  function getFileSource(file) {
504
+ let { source } = file;
454
505
  if (!file.fileName.endsWith(".ts")) {
455
506
  return file.source;
456
507
  }
457
508
  const imports = [];
509
+ const exports = [];
458
510
  file.imports?.forEach((curr) => {
459
511
  const exists = imports.find((imp) => imp.path === curr.path);
460
512
  if (!exists) {
@@ -472,19 +524,40 @@ function getFileSource(file) {
472
524
  }
473
525
  }
474
526
  });
475
- const importSource = imports.reduce((prev, curr) => {
476
- if (Array.isArray(curr.name)) {
477
- return `${prev}
478
- import ${curr.isTypeOnly ? "type " : ""}{ ${curr.name.join(", ")} } from "${curr.path}";`;
479
- }
480
- return `${prev}
481
- import ${curr.isTypeOnly ? "type " : ""}${curr.name} from "${curr.path}";`;
482
- }, "");
527
+ file.exports?.forEach((curr) => {
528
+ const exists = exports.find((imp) => imp.path === curr.path);
529
+ if (!exists) {
530
+ exports.push({
531
+ ...curr,
532
+ name: Array.isArray(curr.name) ? uniq__default.default(curr.name) : curr.name
533
+ });
534
+ }
535
+ if (exists && !Array.isArray(exists.name) && exists.name !== curr.name && exists.asAlias === curr.asAlias) {
536
+ exports.push(curr);
537
+ }
538
+ if (exists && Array.isArray(exists.name)) {
539
+ if (Array.isArray(curr.name)) {
540
+ exists.name = uniq__default.default([...exists.name, ...curr.name]);
541
+ }
542
+ }
543
+ });
544
+ const importNodes = imports.reduce((prev, curr) => {
545
+ return [...prev, tsCodegen.createImportDeclaration({ name: curr.name, path: curr.path, asType: curr.asType })];
546
+ }, []);
547
+ const importSource = tsCodegen.print(importNodes);
548
+ const exportNodes = exports.reduce((prev, curr) => {
549
+ return [...prev, tsCodegen.createExportDeclaration({ name: curr.name, path: curr.path, asAlias: curr.asAlias })];
550
+ }, []);
551
+ const exportSource = tsCodegen.print(exportNodes);
483
552
  if (importSource) {
484
- return `${importSource}
485
- ${file.source}`;
553
+ source = `${importSource}
554
+ ${source}`;
555
+ }
556
+ if (exportSource) {
557
+ source = `${exportSource}
558
+ ${source}`;
486
559
  }
487
- return file.source;
560
+ return source;
488
561
  }
489
562
 
490
563
  // src/managers/pluginManager/PluginManager.ts
@@ -845,11 +918,6 @@ async function buildImplementation(options) {
845
918
  hookName: "buildStart",
846
919
  parameters: [config]
847
920
  });
848
- pluginManager.fileManager.add({
849
- path: isURL(config.input.path) ? config.input.path : pathParser2__default.default.resolve(config.root, config.input.path),
850
- fileName: isURL(config.input.path) ? "input" : config.input.path,
851
- source: isURL(config.input.path) ? config.input.path : await read(pathParser2__default.default.resolve(config.root, config.input.path))
852
- });
853
921
  await pluginManager.hookParallel({ hookName: "buildEnd" });
854
922
  return { files: fileManager.files.map((file) => ({ ...file, source: getFileSource(file) })) };
855
923
  }
@@ -924,5 +992,6 @@ exports.renderTemplate = renderTemplate;
924
992
  exports.timeout = timeout;
925
993
  exports.validatePlugins = validatePlugins;
926
994
  exports.write = write;
995
+ exports.writeIndexes = writeIndexes;
927
996
  //# sourceMappingURL=out.js.map
928
997
  //# sourceMappingURL=index.cjs.map