@kubb/core 1.0.0-beta.8 → 1.0.0

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
 
@@ -17,20 +18,7 @@ var rimraf__default = /*#__PURE__*/_interopDefault(rimraf);
17
18
  var dirTree__default = /*#__PURE__*/_interopDefault(dirTree);
18
19
  var uniq__default = /*#__PURE__*/_interopDefault(uniq);
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
-
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
- }
21
+ module$1.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (document.currentScript && document.currentScript.src || new URL('out.js', document.baseURI).href)));
34
22
 
35
23
  // src/utils/isPromise.ts
36
24
  function isPromise(result) {
@@ -53,17 +41,6 @@ async function write(data, path) {
53
41
  }
54
42
  return safeWriteFileToPath(path, data);
55
43
  }
56
- async function clean(path) {
57
- return new Promise((resolve, reject) => {
58
- rimraf__default.default(path, (err) => {
59
- if (err) {
60
- reject(err);
61
- } else {
62
- resolve(true);
63
- }
64
- });
65
- });
66
- }
67
44
 
68
45
  // src/utils/cache.ts
69
46
  function createPluginCache(cache) {
@@ -99,7 +76,7 @@ function slash(path) {
99
76
  }
100
77
  function getRelativePath(rootDir, filePath) {
101
78
  if (!rootDir || !filePath) {
102
- throw new Error("Root and file should be filled in when retrieving the relativePath");
79
+ throw new Error(`Root and file should be filled in when retrieving the relativePath, ${rootDir} ${filePath}`);
103
80
  }
104
81
  const relativePath = pathParser2__default.default.relative(rootDir, filePath);
105
82
  const path = slash(relativePath).replace("../", "").trimEnd();
@@ -123,16 +100,30 @@ async function read(path) {
123
100
  }
124
101
  }
125
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
+
126
116
  // src/utils/objectToParameters.ts
127
117
  function objectToParameters(data, options = {}) {
118
+ const { typed } = options;
128
119
  return data.reduce((acc, [key, value]) => {
129
- if (options.typed) {
130
- acc.push(`${key}: ${value}["${key}"], `);
120
+ if (typed) {
121
+ acc.push(`${key}: ${value}["${key}"]`);
131
122
  } else {
132
- acc.push(`${key},`);
123
+ acc.push(`${key}`);
133
124
  }
134
125
  return acc;
135
- }, []).join("");
126
+ }, []).join(", ");
136
127
  }
137
128
 
138
129
  // src/utils/nameSorter.ts
@@ -180,7 +171,7 @@ async function timeout(ms) {
180
171
  });
181
172
  }
182
173
 
183
- // src/utils/queue.ts
174
+ // src/utils/Queue.ts
184
175
  var Queue = class {
185
176
  queue = [];
186
177
  workerCount = 0;
@@ -208,6 +199,205 @@ var Queue = class {
208
199
  }
209
200
  };
210
201
 
202
+ // src/utils/getEncodedText.ts
203
+ function getEncodedText(text) {
204
+ return text ? text.replaceAll("`", "\\`") : "";
205
+ }
206
+
207
+ // src/utils/renderTemplate.ts
208
+ function renderTemplate(template, data = void 0) {
209
+ if (!data) {
210
+ return template.replace(/{{(.*?)}}/g, "");
211
+ }
212
+ return template.replace(/{{(.*?)}}/g, (match) => {
213
+ const value = data[match.split(/{{|}}/).filter(Boolean)[0].trim()];
214
+ return value || "";
215
+ });
216
+ }
217
+ async function clean(path) {
218
+ return new Promise((resolve, reject) => {
219
+ rimraf__default.default(path, (err) => {
220
+ if (err) {
221
+ reject(err);
222
+ } else {
223
+ resolve(true);
224
+ }
225
+ });
226
+ });
227
+ }
228
+ var TreeNode = class {
229
+ data;
230
+ parent;
231
+ children = [];
232
+ constructor(data, parent) {
233
+ this.data = data;
234
+ this.parent = parent;
235
+ return this;
236
+ }
237
+ addChild(data) {
238
+ const child = new TreeNode(data, this);
239
+ if (!this.children) {
240
+ this.children = [];
241
+ }
242
+ this.children.push(child);
243
+ return child;
244
+ }
245
+ find(data) {
246
+ if (data === this.data) {
247
+ return this;
248
+ }
249
+ if (this.children) {
250
+ for (let i = 0, { length } = this.children, target = null; i < length; i++) {
251
+ target = this.children[i].find(data);
252
+ if (target) {
253
+ return target;
254
+ }
255
+ }
256
+ }
257
+ return null;
258
+ }
259
+ leaves() {
260
+ if (!this.children || this.children.length === 0) {
261
+ return [this];
262
+ }
263
+ const leaves = [];
264
+ if (this.children) {
265
+ for (let i = 0, { length } = this.children; i < length; i++) {
266
+ leaves.push.apply(leaves, this.children[i].leaves());
267
+ }
268
+ }
269
+ return leaves;
270
+ }
271
+ root() {
272
+ if (!this.parent) {
273
+ return this;
274
+ }
275
+ return this.parent.root();
276
+ }
277
+ forEach(callback) {
278
+ if (typeof callback !== "function") {
279
+ throw new TypeError("forEach() callback must be a function");
280
+ }
281
+ callback(this);
282
+ if (this.children) {
283
+ for (let i = 0, { length } = this.children; i < length; i++) {
284
+ this.children[i].forEach(callback);
285
+ }
286
+ }
287
+ return this;
288
+ }
289
+ static build(path, options = {}) {
290
+ const filteredTree = dirTree__default.default(path, { extensions: options?.extensions, exclude: options.exclude });
291
+ if (!filteredTree) {
292
+ return null;
293
+ }
294
+ const treeNode = new TreeNode({ name: filteredTree.name, path: filteredTree.path, type: filteredTree.type });
295
+ const recurse = (node, item) => {
296
+ const subNode = node.addChild({ name: item.name, path: item.path, type: item.type });
297
+ if (item.children?.length) {
298
+ item.children?.forEach((child) => {
299
+ recurse(subNode, child);
300
+ });
301
+ }
302
+ };
303
+ filteredTree.children?.forEach((child) => recurse(treeNode, child));
304
+ return treeNode;
305
+ }
306
+ };
307
+
308
+ // src/utils/transformReservedWord.ts
309
+ var reservedWords = [
310
+ "abstract",
311
+ "arguments",
312
+ "boolean",
313
+ "break",
314
+ "byte",
315
+ "case",
316
+ "catch",
317
+ "char",
318
+ "class",
319
+ "const",
320
+ "continue",
321
+ "debugger",
322
+ "default",
323
+ "delete",
324
+ "do",
325
+ "double",
326
+ "else",
327
+ "enum",
328
+ "eval",
329
+ "export",
330
+ "extends",
331
+ "false",
332
+ "final",
333
+ "finally",
334
+ "float",
335
+ "for",
336
+ "function",
337
+ "goto",
338
+ "if",
339
+ "implements",
340
+ "import",
341
+ "in",
342
+ "instanceof",
343
+ "int",
344
+ "interface",
345
+ "let",
346
+ "long",
347
+ "native",
348
+ "new",
349
+ "null",
350
+ "package",
351
+ "private",
352
+ "protected",
353
+ "public",
354
+ "return",
355
+ "short",
356
+ "static",
357
+ "super",
358
+ "switch",
359
+ "synchronized",
360
+ "this",
361
+ "throw",
362
+ "throws",
363
+ "transient",
364
+ "true",
365
+ "try",
366
+ "typeof",
367
+ "var",
368
+ "void",
369
+ "volatile",
370
+ "while",
371
+ "with",
372
+ "yield",
373
+ "Array",
374
+ "Date",
375
+ "eval",
376
+ "function",
377
+ "hasOwnProperty",
378
+ "Infinity",
379
+ "isFinite",
380
+ "isNaN",
381
+ "isPrototypeOf",
382
+ "length",
383
+ "Math",
384
+ "name",
385
+ "NaN",
386
+ "Number",
387
+ "Object",
388
+ "prototype",
389
+ "String",
390
+ "toString",
391
+ "undefined",
392
+ "valueOf"
393
+ ];
394
+ function transformReservedWord(word) {
395
+ if (reservedWords.includes(word)) {
396
+ return `_${word}`;
397
+ }
398
+ return word;
399
+ }
400
+
211
401
  // src/plugin.ts
212
402
  function createPlugin(factory) {
213
403
  return (options) => {
@@ -231,11 +421,14 @@ var definePlugin = createPlugin((options) => {
231
421
  return options.config;
232
422
  },
233
423
  fileManager,
234
- async addFile(file) {
235
- return fileManager.addOrAppend(file);
424
+ async addFile(...files) {
425
+ return Promise.all(files.map((file) => file.override ? fileManager.add(file) : fileManager.addOrAppend(file)));
236
426
  },
237
427
  resolvePath,
238
- resolveName,
428
+ resolveName: (params) => {
429
+ const name2 = resolveName(params);
430
+ return transformReservedWord(name2);
431
+ },
239
432
  load,
240
433
  cache: createPluginCache(/* @__PURE__ */ Object.create(null))
241
434
  };
@@ -243,11 +436,9 @@ var definePlugin = createPlugin((options) => {
243
436
  name,
244
437
  options,
245
438
  api,
246
- resolvePath(fileName, directory) {
247
- if (!directory) {
248
- return null;
249
- }
250
- return pathParser2__default.default.resolve(directory, fileName);
439
+ resolvePath(fileName) {
440
+ const root = pathParser2__default.default.resolve(this.config.root, this.config.output.path);
441
+ return pathParser2__default.default.resolve(root, fileName);
251
442
  },
252
443
  resolveName(name2) {
253
444
  return name2;
@@ -294,14 +485,20 @@ var FileManager = class {
294
485
  return file;
295
486
  }
296
487
  addOrAppend(file) {
488
+ if (!file.path.endsWith(file.fileName)) ;
297
489
  const previousCache = this.getCacheByPath(file.path);
298
490
  if (previousCache) {
491
+ const sourceAlreadyExists = file.source && previousCache.file.source.includes(file.source);
492
+ if (sourceAlreadyExists) {
493
+ return Promise.resolve(file);
494
+ }
299
495
  this.cache.delete(previousCache.id);
300
496
  return this.add({
301
497
  ...file,
302
498
  source: `${previousCache.file.source}
303
499
  ${file.source}`,
304
- imports: [...previousCache.file.imports || [], ...file.imports || []]
500
+ imports: [...previousCache.file.imports || [], ...file.imports || []],
501
+ exports: [...previousCache.file.exports || [], ...file.exports || []]
305
502
  });
306
503
  }
307
504
  return this.add(file);
@@ -332,85 +529,53 @@ ${file.source}`,
332
529
  return read(...params);
333
530
  }
334
531
  };
335
- var TreeNode = class {
336
- data;
337
- parent;
338
- children = [];
339
- constructor(data, parent) {
340
- this.data = data;
341
- this.parent = parent;
342
- return this;
343
- }
344
- addChild(data) {
345
- const child = new TreeNode(data, this);
346
- if (!this.children) {
347
- this.children = [];
348
- }
349
- this.children.push(child);
350
- return child;
532
+ function writeIndexes(root, options) {
533
+ const tree = TreeNode.build(root, { extensions: /\.ts/, ...options });
534
+ if (!tree) {
535
+ return void 0;
351
536
  }
352
- find(data) {
353
- if (data === this.data) {
354
- return this;
537
+ const fileReducer = (files2, item) => {
538
+ if (!item.children) {
539
+ return [];
355
540
  }
356
- if (this.children) {
357
- for (let i = 0, { length } = this.children, target = null; i < length; i++) {
358
- target = this.children[i].find(data);
359
- if (target) {
360
- return target;
541
+ if (item.children?.length > 1) {
542
+ const path = pathParser2__default.default.resolve(item.data.path, "index.ts");
543
+ const exports = item.children.map((file) => {
544
+ if (!file) {
545
+ return void 0;
361
546
  }
362
- }
363
- }
364
- return null;
365
- }
366
- leaves() {
367
- if (!this.children || this.children.length === 0) {
368
- return [this];
369
- }
370
- const leaves = [];
371
- if (this.children) {
372
- for (let i = 0, { length } = this.children; i < length; i++) {
373
- leaves.push.apply(leaves, this.children[i].leaves());
374
- }
375
- }
376
- return leaves;
377
- }
378
- root() {
379
- if (!this.parent) {
380
- return this;
381
- }
382
- return this.parent.root();
383
- }
384
- forEach(callback) {
385
- if (typeof callback !== "function") {
386
- throw new TypeError("forEach() callback must be a function");
387
- }
388
- callback(this);
389
- if (this.children) {
390
- for (let i = 0, { length } = this.children; i < length; i++) {
391
- this.children[i].forEach(callback);
392
- }
393
- }
394
- return this;
395
- }
396
- static build(path, options = {}) {
397
- const filteredTree = dirTree__default.default(path, { extensions: options?.extensions, exclude: options.exclude });
398
- if (!filteredTree) {
399
- return null;
400
- }
401
- const treeNode = new TreeNode({ name: filteredTree.name, path: filteredTree.path, type: filteredTree.type });
402
- const recurse = (node, item) => {
403
- const subNode = node.addChild({ name: item.name, path: item.path, type: item.type });
404
- if (item.children?.length) {
405
- item.children?.forEach((child) => {
406
- recurse(subNode, child);
547
+ const importPath = file.data.type === "directory" ? `./${file.data.name}` : `./${file.data.name.replace(/\.[^.]*$/, "")}`;
548
+ if (importPath.includes("index") && path.includes("index")) {
549
+ return void 0;
550
+ }
551
+ return { path: importPath };
552
+ }).filter(Boolean);
553
+ files2.push({
554
+ path,
555
+ fileName: "index.ts",
556
+ source: "",
557
+ exports
558
+ });
559
+ } else {
560
+ item.children?.forEach((child) => {
561
+ const path = pathParser2__default.default.resolve(item.data.path, "index.ts");
562
+ const importPath = child.data.type === "directory" ? `./${child.data.name}` : `./${child.data.name.replace(/\.[^.]*$/, "")}`;
563
+ files2.push({
564
+ path,
565
+ fileName: "index.ts",
566
+ source: "",
567
+ exports: [{ path: importPath }]
407
568
  });
408
- }
409
- };
410
- filteredTree.children?.forEach((child) => recurse(treeNode, child));
411
- return treeNode;
412
- }
413
- };
569
+ });
570
+ }
571
+ item.children.forEach((childItem) => {
572
+ fileReducer(files2, childItem);
573
+ });
574
+ return files2;
575
+ };
576
+ const files = fileReducer([], tree);
577
+ return files;
578
+ }
414
579
  function combineFiles(files) {
415
580
  return files.filter(Boolean).reduce((acc, curr) => {
416
581
  if (!curr) {
@@ -423,7 +588,8 @@ function combineFiles(files) {
423
588
  ...curr,
424
589
  source: `${prev.source}
425
590
  ${curr.source}`,
426
- imports: [...prev.imports || [], ...curr.imports || []]
591
+ imports: [...prev.imports || [], ...curr.imports || []],
592
+ exports: [...prev.exports || [], ...curr.exports || []]
427
593
  };
428
594
  } else {
429
595
  acc.push(curr);
@@ -432,10 +598,12 @@ ${curr.source}`,
432
598
  }, []);
433
599
  }
434
600
  function getFileSource(file) {
601
+ let { source } = file;
435
602
  if (!file.fileName.endsWith(".ts")) {
436
603
  return file.source;
437
604
  }
438
605
  const imports = [];
606
+ const exports = [];
439
607
  file.imports?.forEach((curr) => {
440
608
  const exists = imports.find((imp) => imp.path === curr.path);
441
609
  if (!exists) {
@@ -453,19 +621,40 @@ function getFileSource(file) {
453
621
  }
454
622
  }
455
623
  });
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
- }, "");
624
+ file.exports?.forEach((curr) => {
625
+ const exists = exports.find((imp) => imp.path === curr.path);
626
+ if (!exists) {
627
+ exports.push({
628
+ ...curr,
629
+ name: Array.isArray(curr.name) ? uniq__default.default(curr.name) : curr.name
630
+ });
631
+ }
632
+ if (exists && !Array.isArray(exists.name) && exists.name !== curr.name && exists.asAlias === curr.asAlias) {
633
+ exports.push(curr);
634
+ }
635
+ if (exists && Array.isArray(exists.name)) {
636
+ if (Array.isArray(curr.name)) {
637
+ exists.name = uniq__default.default([...exists.name, ...curr.name]);
638
+ }
639
+ }
640
+ });
641
+ const importNodes = imports.reduce((prev, curr) => {
642
+ return [...prev, tsCodegen.createImportDeclaration({ name: curr.name, path: curr.path, isTypeOnly: curr.isTypeOnly })];
643
+ }, []);
644
+ const importSource = tsCodegen.print(importNodes);
645
+ const exportNodes = exports.reduce((prev, curr) => {
646
+ return [...prev, tsCodegen.createExportDeclaration({ name: curr.name, path: curr.path, asAlias: curr.asAlias })];
647
+ }, []);
648
+ const exportSource = tsCodegen.print(exportNodes);
464
649
  if (importSource) {
465
- return `${importSource}
466
- ${file.source}`;
650
+ source = `${importSource}
651
+ ${source}`;
652
+ }
653
+ if (exportSource) {
654
+ source = `${exportSource}
655
+ ${source}`;
467
656
  }
468
- return file.source;
657
+ return source;
469
658
  }
470
659
 
471
660
  // src/managers/pluginManager/PluginManager.ts
@@ -787,7 +976,7 @@ async function transformReducer(_previousCode, result, _plugin) {
787
976
  }
788
977
  return result;
789
978
  }
790
- async function buildImplementation(options, done) {
979
+ async function buildImplementation(options) {
791
980
  const { config, logger } = options;
792
981
  if (config.output.clean) {
793
982
  await clean(config.output.path);
@@ -827,19 +1016,15 @@ async function buildImplementation(options, done) {
827
1016
  parameters: [config]
828
1017
  });
829
1018
  await pluginManager.hookParallel({ hookName: "buildEnd" });
830
- setTimeout(() => {
831
- done({ files: fileManager.files.map((file) => ({ ...file, source: getFileSource(file) })) });
832
- }, 500);
833
- pluginManager.fileManager.add({
834
- path: isURL(config.input.path) ? config.input.path : pathParser2__default.default.resolve(config.root, config.input.path),
835
- fileName: isURL(config.input.path) ? "input" : config.input.path,
836
- source: isURL(config.input.path) ? config.input.path : await read(pathParser2__default.default.resolve(config.root, config.input.path))
837
- });
1019
+ return { files: fileManager.files.map((file) => ({ ...file, source: getFileSource(file) })) };
838
1020
  }
839
1021
  function build(options) {
840
1022
  return new Promise(async (resolve, reject) => {
841
1023
  try {
842
- await buildImplementation(options, resolve);
1024
+ const output = await buildImplementation(options);
1025
+ setTimeout(() => {
1026
+ resolve(output);
1027
+ }, 500);
843
1028
  } catch (e) {
844
1029
  reject(e);
845
1030
  }
@@ -888,6 +1073,7 @@ exports.createPlugin = createPlugin;
888
1073
  exports.createPluginCache = createPluginCache;
889
1074
  exports.default = src_default;
890
1075
  exports.defineConfig = defineConfig;
1076
+ exports.getEncodedText = getEncodedText;
891
1077
  exports.getFileSource = getFileSource;
892
1078
  exports.getPathMode = getPathMode;
893
1079
  exports.getRelativePath = getRelativePath;
@@ -899,8 +1085,11 @@ exports.name = name;
899
1085
  exports.nameSorter = nameSorter;
900
1086
  exports.objectToParameters = objectToParameters;
901
1087
  exports.read = read;
1088
+ exports.renderTemplate = renderTemplate;
902
1089
  exports.timeout = timeout;
1090
+ exports.transformReservedWord = transformReservedWord;
903
1091
  exports.validatePlugins = validatePlugins;
904
1092
  exports.write = write;
1093
+ exports.writeIndexes = writeIndexes;
905
1094
  //# sourceMappingURL=out.js.map
906
1095
  //# sourceMappingURL=index.cjs.map