@kubb/fabric-core 0.2.19 → 0.3.1

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.
Files changed (51) hide show
  1. package/dist/{Fabric-CVe8cc8b.d.ts → Fabric-RmoYWGrr.d.cts} +4 -4
  2. package/dist/{Fabric-BezqNTQ9.d.cts → Fabric-cIhiQpgN.d.ts} +4 -4
  3. package/dist/defineProperty-DwFON4j7.cjs +367 -0
  4. package/dist/defineProperty-DwFON4j7.cjs.map +1 -0
  5. package/dist/defineProperty-fiNt9UhD.js +325 -0
  6. package/dist/defineProperty-fiNt9UhD.js.map +1 -0
  7. package/dist/{getRelativePath-C6lvNCs7.cjs → getRelativePath-eCdp2Z8M.cjs} +1 -2
  8. package/dist/{getRelativePath-C6lvNCs7.cjs.map → getRelativePath-eCdp2Z8M.cjs.map} +1 -1
  9. package/dist/index.cjs +20 -21
  10. package/dist/index.cjs.map +1 -1
  11. package/dist/index.d.cts +1 -1
  12. package/dist/index.d.ts +1 -1
  13. package/dist/index.js +20 -19
  14. package/dist/index.js.map +1 -1
  15. package/dist/parsers/typescript.cjs +2 -2
  16. package/dist/parsers/typescript.d.cts +2 -2
  17. package/dist/parsers/typescript.d.ts +2 -2
  18. package/dist/parsers/typescript.js +1 -1
  19. package/dist/parsers.cjs +2 -2
  20. package/dist/parsers.d.cts +2 -2
  21. package/dist/parsers.d.ts +2 -2
  22. package/dist/parsers.js +1 -1
  23. package/dist/plugins.cjs +79 -46
  24. package/dist/plugins.cjs.map +1 -1
  25. package/dist/plugins.d.cts +1 -1
  26. package/dist/plugins.d.ts +1 -1
  27. package/dist/plugins.js +78 -43
  28. package/dist/plugins.js.map +1 -1
  29. package/dist/types.d.cts +1 -1
  30. package/dist/types.d.ts +1 -1
  31. package/dist/{typescriptParser-B5SxjtvV.d.ts → typescriptParser-BjqVuRHF.d.cts} +3 -14
  32. package/dist/{typescriptParser-CWT7zCJy.js → typescriptParser-CvJg4PQJ.js} +27 -45
  33. package/dist/typescriptParser-CvJg4PQJ.js.map +1 -0
  34. package/dist/{typescriptParser-PfAO0SSm.d.cts → typescriptParser-Cy9_9o6I.d.ts} +3 -14
  35. package/dist/{typescriptParser-CNHO6H2_.cjs → typescriptParser-D6-3Z7Lj.cjs} +28 -46
  36. package/dist/typescriptParser-D6-3Z7Lj.cjs.map +1 -0
  37. package/package.json +1 -1
  38. package/src/Fabric.ts +1 -1
  39. package/src/FileManager.ts +2 -2
  40. package/src/FileProcessor.ts +8 -15
  41. package/src/createFile.ts +110 -57
  42. package/src/defineFabric.ts +15 -3
  43. package/src/parsers/typescriptParser.ts +55 -73
  44. package/src/plugins/barrelPlugin.ts +63 -36
  45. package/src/utils/TreeNode.ts +54 -27
  46. package/dist/defineProperty-DZi5DvrW.cjs +0 -390
  47. package/dist/defineProperty-DZi5DvrW.cjs.map +0 -1
  48. package/dist/defineProperty-DcP1vZ2K.js +0 -346
  49. package/dist/defineProperty-DcP1vZ2K.js.map +0 -1
  50. package/dist/typescriptParser-CNHO6H2_.cjs.map +0 -1
  51. package/dist/typescriptParser-CWT7zCJy.js.map +0 -1
@@ -7,55 +7,33 @@ import ts from "typescript";
7
7
  //#region src/parsers/typescriptParser.ts
8
8
  const { factory } = ts;
9
9
  /**
10
- * Escaped new lines in code with block comments so they can be restored by {@link restoreNewLines}
11
- */
12
- const escapeNewLines = (code) => code.replace(/\n\n/g, "\n/* :newline: */");
13
- /**
14
- * Reverses {@link escapeNewLines} and restores new lines
15
- */
16
- const restoreNewLines = (code) => code.replace(/\/\* :newline: \*\//g, "\n");
17
- /**
18
10
  * Convert AST TypeScript/TSX nodes to a string based on the TypeScript printer.
19
- * Ensures consistent output across environments.
20
- * Also works as a formatter when `source` is provided without `elements`.
21
11
  */
22
- function print(elements = [], { source = "", baseName = "print.tsx", scriptKind = ts.ScriptKind.TSX } = {}) {
23
- const sourceFile = ts.createSourceFile(baseName, escapeNewLines(source), ts.ScriptTarget.ES2022, true, scriptKind);
12
+ function print(...elements) {
13
+ const sourceFile = ts.createSourceFile("print.tsx", "", ts.ScriptTarget.ES2022, true, ts.ScriptKind.TSX);
24
14
  const printer = ts.createPrinter({
25
15
  omitTrailingSemicolon: true,
26
16
  newLine: ts.NewLineKind.LineFeed,
27
17
  removeComments: false,
28
18
  noEmitHelpers: true
29
19
  });
30
- let output;
31
- if (elements.length > 0) {
32
- const nodes = elements.filter(Boolean).sort((a, b) => {
33
- var _a$pos, _b$pos;
34
- return ((_a$pos = a.pos) !== null && _a$pos !== void 0 ? _a$pos : 0) - ((_b$pos = b.pos) !== null && _b$pos !== void 0 ? _b$pos : 0);
35
- });
36
- output = printer.printList(ts.ListFormat.MultiLine, factory.createNodeArray(nodes), sourceFile);
37
- } else output = printer.printFile(sourceFile);
38
- return restoreNewLines(output).replace(/\r\n/g, "\n");
20
+ for (const node of elements) if (node.kind === ts.SyntaxKind.Unknown) console.error("⚠️ Unknown node found:", node);
21
+ return printer.printList(ts.ListFormat.MultiLine, factory.createNodeArray(elements.filter(Boolean)), sourceFile).replace(/\r\n/g, "\n");
39
22
  }
40
23
  function createImport({ name, path: path$1, root, isTypeOnly = false, isNameSpace = false }) {
41
24
  const resolvePath = root ? getRelativePath(root, path$1) : path$1;
42
25
  if (!Array.isArray(name)) {
43
- let importPropertyName = factory.createIdentifier(name);
44
- let importName;
45
- if (isNameSpace) {
46
- importPropertyName = void 0;
47
- importName = factory.createNamespaceImport(factory.createIdentifier(name));
48
- }
49
- return factory.createImportDeclaration(void 0, factory.createImportClause(isTypeOnly, importPropertyName, importName), factory.createStringLiteral(resolvePath), void 0);
26
+ if (isNameSpace) return factory.createImportDeclaration(void 0, factory.createImportClause(isTypeOnly, void 0, factory.createNamespaceImport(factory.createIdentifier(name))), factory.createStringLiteral(resolvePath), void 0);
27
+ return factory.createImportDeclaration(void 0, factory.createImportClause(isTypeOnly, factory.createIdentifier(name), void 0), factory.createStringLiteral(resolvePath), void 0);
50
28
  }
51
- return factory.createImportDeclaration(void 0, factory.createImportClause(isTypeOnly, void 0, factory.createNamedImports(name.map((item) => {
29
+ const specifiers = name.map((item) => {
52
30
  if (typeof item === "object") {
53
- const obj = item;
54
- if (obj.name) return factory.createImportSpecifier(false, factory.createIdentifier(obj.propertyName), factory.createIdentifier(obj.name));
55
- return factory.createImportSpecifier(false, void 0, factory.createIdentifier(obj.propertyName));
31
+ const { propertyName, name: alias } = item;
32
+ return factory.createImportSpecifier(false, alias ? factory.createIdentifier(propertyName) : void 0, factory.createIdentifier(alias !== null && alias !== void 0 ? alias : propertyName));
56
33
  }
57
34
  return factory.createImportSpecifier(false, void 0, factory.createIdentifier(item));
58
- }))), factory.createStringLiteral(resolvePath), void 0);
35
+ });
36
+ return factory.createImportDeclaration(void 0, factory.createImportClause(isTypeOnly, void 0, factory.createNamedImports(specifiers)), factory.createStringLiteral(resolvePath), void 0);
59
37
  }
60
38
  function createExport({ path: path$1, asAlias, isTypeOnly = false, name }) {
61
39
  if (name && !Array.isArray(name) && !asAlias) console.warn(`When using name as string, asAlias should be true ${name}`);
@@ -72,35 +50,39 @@ const typescriptParser = createParser({
72
50
  extNames: [".ts", ".js"],
73
51
  install() {},
74
52
  async parse(file, options = { extname: ".ts" }) {
75
- const source = file.sources.map((item) => item.value).join("\n\n");
76
- const importNodes = file.imports.map((item) => {
53
+ const sourceParts = [];
54
+ for (const item of file.sources) if (item.value) sourceParts.push(item.value);
55
+ const source = sourceParts.join("\n\n");
56
+ const importNodes = [];
57
+ for (const item of file.imports) {
77
58
  const importPath = item.root ? getRelativePath(item.root, item.path) : item.path;
78
59
  const hasExtname = !!path.extname(importPath);
79
- return createImport({
60
+ importNodes.push(createImport({
80
61
  name: item.name,
81
62
  path: options.extname && hasExtname ? `${trimExtName(importPath)}${options.extname}` : item.root ? trimExtName(importPath) : importPath,
82
63
  isTypeOnly: item.isTypeOnly
83
- });
84
- }).filter(Boolean);
85
- const exportNodes = file.exports.map((item) => {
64
+ }));
65
+ }
66
+ const exportNodes = [];
67
+ for (const item of file.exports) {
86
68
  const exportPath = item.path;
87
69
  const hasExtname = !!path.extname(exportPath);
88
- return createExport({
70
+ exportNodes.push(createExport({
89
71
  name: item.name,
90
72
  path: options.extname && hasExtname ? `${trimExtName(item.path)}${options.extname}` : trimExtName(item.path),
91
73
  isTypeOnly: item.isTypeOnly,
92
74
  asAlias: item.asAlias
93
- });
94
- }).filter(Boolean);
75
+ }));
76
+ }
95
77
  return [
96
78
  file.banner,
97
- print([...importNodes, ...exportNodes]),
79
+ print(...importNodes, ...exportNodes),
98
80
  source,
99
81
  file.footer
100
- ].join("\n");
82
+ ].filter((segment) => segment != null).join("\n");
101
83
  }
102
84
  });
103
85
 
104
86
  //#endregion
105
87
  export { typescriptParser as i, createImport as n, print as r, createExport as t };
106
- //# sourceMappingURL=typescriptParser-CWT7zCJy.js.map
88
+ //# sourceMappingURL=typescriptParser-CvJg4PQJ.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"typescriptParser-CvJg4PQJ.js","names":["path","sourceParts: Array<string>","importNodes: Array<ts.ImportDeclaration>","exportNodes: Array<ts.ExportDeclaration>"],"sources":["../src/parsers/typescriptParser.ts"],"sourcesContent":["import path from 'node:path'\nimport ts from 'typescript'\nimport { getRelativePath } from '../utils/getRelativePath.ts'\nimport { trimExtName } from '../utils/trimExtName.ts'\nimport { createParser } from './createParser.ts'\n\nconst { factory } = ts\n\n/**\n * Convert AST TypeScript/TSX nodes to a string based on the TypeScript printer.\n */\nexport function print(...elements: Array<ts.Node>): string {\n const sourceFile = ts.createSourceFile('print.tsx', '', ts.ScriptTarget.ES2022, true, ts.ScriptKind.TSX)\n\n const printer = ts.createPrinter({\n omitTrailingSemicolon: true,\n newLine: ts.NewLineKind.LineFeed,\n removeComments: false,\n noEmitHelpers: true,\n })\n\n for (const node of elements) {\n if (node.kind === ts.SyntaxKind.Unknown) {\n console.error('⚠️ Unknown node found:', node)\n }\n }\n\n const output = printer.printList(ts.ListFormat.MultiLine, factory.createNodeArray(elements.filter(Boolean)), sourceFile)\n\n return output.replace(/\\r\\n/g, '\\n')\n}\n\nexport function createImport({\n name,\n path,\n root,\n isTypeOnly = false,\n isNameSpace = false,\n}: {\n name: string | Array<string | { propertyName: string; name?: string }>\n path: string\n root?: string\n isTypeOnly?: boolean\n isNameSpace?: boolean\n}) {\n const resolvePath = root ? getRelativePath(root, path) : path\n\n // Namespace or default import\n if (!Array.isArray(name)) {\n if (isNameSpace) {\n return factory.createImportDeclaration(\n undefined,\n factory.createImportClause(isTypeOnly, undefined, factory.createNamespaceImport(factory.createIdentifier(name))),\n factory.createStringLiteral(resolvePath),\n undefined,\n )\n }\n\n return factory.createImportDeclaration(\n undefined,\n factory.createImportClause(isTypeOnly, factory.createIdentifier(name), undefined),\n factory.createStringLiteral(resolvePath),\n undefined,\n )\n }\n\n // Named imports\n const specifiers = name.map((item) => {\n if (typeof item === 'object') {\n const { propertyName, name: alias } = item\n return factory.createImportSpecifier(false, alias ? factory.createIdentifier(propertyName) : undefined, factory.createIdentifier(alias ?? propertyName))\n }\n\n return factory.createImportSpecifier(false, undefined, factory.createIdentifier(item))\n })\n\n return factory.createImportDeclaration(\n undefined,\n factory.createImportClause(isTypeOnly, undefined, factory.createNamedImports(specifiers)),\n factory.createStringLiteral(resolvePath),\n undefined,\n )\n}\n\nexport function createExport({\n path,\n asAlias,\n isTypeOnly = false,\n name,\n}: {\n path: string\n asAlias?: boolean\n isTypeOnly?: boolean\n name?: string | Array<ts.Identifier | string>\n}) {\n if (name && !Array.isArray(name) && !asAlias) {\n console.warn(`When using name as string, asAlias should be true ${name}`)\n }\n\n if (!Array.isArray(name)) {\n const parsedName = name?.match(/^\\d/) ? `_${name?.slice(1)}` : name\n\n return factory.createExportDeclaration(\n undefined,\n isTypeOnly,\n asAlias && parsedName ? factory.createNamespaceExport(factory.createIdentifier(parsedName)) : undefined,\n factory.createStringLiteral(path),\n undefined,\n )\n }\n\n return factory.createExportDeclaration(\n undefined,\n isTypeOnly,\n factory.createNamedExports(\n name.map((propertyName) => {\n return factory.createExportSpecifier(false, undefined, typeof propertyName === 'string' ? factory.createIdentifier(propertyName) : propertyName)\n }),\n ),\n factory.createStringLiteral(path),\n undefined,\n )\n}\n\nexport const typescriptParser = createParser({\n name: 'typescript',\n extNames: ['.ts', '.js'],\n install() {},\n async parse(file, options = { extname: '.ts' }) {\n const sourceParts: Array<string> = []\n for (const item of file.sources) {\n if (item.value) {\n sourceParts.push(item.value)\n }\n }\n const source = sourceParts.join('\\n\\n')\n\n const importNodes: Array<ts.ImportDeclaration> = []\n for (const item of file.imports) {\n const importPath = item.root ? getRelativePath(item.root, item.path) : item.path\n const hasExtname = !!path.extname(importPath)\n\n importNodes.push(\n createImport({\n name: item.name,\n path: options.extname && hasExtname ? `${trimExtName(importPath)}${options.extname}` : item.root ? trimExtName(importPath) : importPath,\n isTypeOnly: item.isTypeOnly,\n }),\n )\n }\n\n const exportNodes: Array<ts.ExportDeclaration> = []\n for (const item of file.exports) {\n const exportPath = item.path\n const hasExtname = !!path.extname(exportPath)\n\n exportNodes.push(\n createExport({\n name: item.name,\n path: options.extname && hasExtname ? `${trimExtName(item.path)}${options.extname}` : trimExtName(item.path),\n isTypeOnly: item.isTypeOnly,\n asAlias: item.asAlias,\n }),\n )\n }\n\n const parts = [file.banner, print(...importNodes, ...exportNodes), source, file.footer].filter((segment): segment is string => segment != null)\n return parts.join('\\n')\n },\n})\n"],"mappings":";;;;;;;AAMA,MAAM,EAAE,YAAY;;;;AAKpB,SAAgB,MAAM,GAAG,UAAkC;CACzD,MAAM,aAAa,GAAG,iBAAiB,aAAa,IAAI,GAAG,aAAa,QAAQ,MAAM,GAAG,WAAW,IAAI;CAExG,MAAM,UAAU,GAAG,cAAc;EAC/B,uBAAuB;EACvB,SAAS,GAAG,YAAY;EACxB,gBAAgB;EAChB,eAAe;EAChB,CAAC;AAEF,MAAK,MAAM,QAAQ,SACjB,KAAI,KAAK,SAAS,GAAG,WAAW,QAC9B,SAAQ,MAAM,0BAA0B,KAA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KAAK,KAAK,GAAG,QAAQ,YAAY,YAAY,KAAK,KAAK;IAC5G,YAAY,KAAK;IACjB,SAAS,KAAK;IACf,CAAC,CACH;;AAIH,SADc;GAAC,KAAK;GAAQ,MAAM,GAAG,aAAa,GAAG,YAAY;GAAE;GAAQ,KAAK;GAAO,CAAC,QAAQ,YAA+B,WAAW,KAAK,CAClI,KAAK,KAAK;;CAE1B,CAAC"}
@@ -1,22 +1,11 @@
1
- import { u as Parser } from "./Fabric-BezqNTQ9.cjs";
1
+ import { u as Parser } from "./Fabric-cIhiQpgN.js";
2
2
  import ts from "typescript";
3
3
 
4
4
  //#region src/parsers/typescriptParser.d.ts
5
- type PrintOptions = {
6
- source?: string;
7
- baseName?: string;
8
- scriptKind?: ts.ScriptKind;
9
- };
10
5
  /**
11
6
  * Convert AST TypeScript/TSX nodes to a string based on the TypeScript printer.
12
- * Ensures consistent output across environments.
13
- * Also works as a formatter when `source` is provided without `elements`.
14
7
  */
15
- declare function print(elements?: Array<ts.Node>, {
16
- source,
17
- baseName,
18
- scriptKind
19
- }?: PrintOptions): string;
8
+ declare function print(...elements: Array<ts.Node>): string;
20
9
  declare function createImport({
21
10
  name,
22
11
  path,
@@ -47,4 +36,4 @@ declare function createExport({
47
36
  declare const typescriptParser: Parser<[], any>;
48
37
  //#endregion
49
38
  export { typescriptParser as i, createImport as n, print as r, createExport as t };
50
- //# sourceMappingURL=typescriptParser-PfAO0SSm.d.cts.map
39
+ //# sourceMappingURL=typescriptParser-Cy9_9o6I.d.ts.map
@@ -1,6 +1,6 @@
1
1
  const require_trimExtName = require('./trimExtName-Bb4zGVF1.cjs');
2
2
  const require_createParser = require('./createParser-C4IkyTs5.cjs');
3
- const require_getRelativePath = require('./getRelativePath-C6lvNCs7.cjs');
3
+ const require_getRelativePath = require('./getRelativePath-eCdp2Z8M.cjs');
4
4
  let node_path = require("node:path");
5
5
  node_path = require_trimExtName.__toESM(node_path);
6
6
  let typescript = require("typescript");
@@ -9,55 +9,33 @@ typescript = require_trimExtName.__toESM(typescript);
9
9
  //#region src/parsers/typescriptParser.ts
10
10
  const { factory } = typescript.default;
11
11
  /**
12
- * Escaped new lines in code with block comments so they can be restored by {@link restoreNewLines}
13
- */
14
- const escapeNewLines = (code) => code.replace(/\n\n/g, "\n/* :newline: */");
15
- /**
16
- * Reverses {@link escapeNewLines} and restores new lines
17
- */
18
- const restoreNewLines = (code) => code.replace(/\/\* :newline: \*\//g, "\n");
19
- /**
20
12
  * Convert AST TypeScript/TSX nodes to a string based on the TypeScript printer.
21
- * Ensures consistent output across environments.
22
- * Also works as a formatter when `source` is provided without `elements`.
23
13
  */
24
- function print(elements = [], { source = "", baseName = "print.tsx", scriptKind = typescript.default.ScriptKind.TSX } = {}) {
25
- const sourceFile = typescript.default.createSourceFile(baseName, escapeNewLines(source), typescript.default.ScriptTarget.ES2022, true, scriptKind);
14
+ function print(...elements) {
15
+ const sourceFile = typescript.default.createSourceFile("print.tsx", "", typescript.default.ScriptTarget.ES2022, true, typescript.default.ScriptKind.TSX);
26
16
  const printer = typescript.default.createPrinter({
27
17
  omitTrailingSemicolon: true,
28
18
  newLine: typescript.default.NewLineKind.LineFeed,
29
19
  removeComments: false,
30
20
  noEmitHelpers: true
31
21
  });
32
- let output;
33
- if (elements.length > 0) {
34
- const nodes = elements.filter(Boolean).sort((a, b) => {
35
- var _a$pos, _b$pos;
36
- return ((_a$pos = a.pos) !== null && _a$pos !== void 0 ? _a$pos : 0) - ((_b$pos = b.pos) !== null && _b$pos !== void 0 ? _b$pos : 0);
37
- });
38
- output = printer.printList(typescript.default.ListFormat.MultiLine, factory.createNodeArray(nodes), sourceFile);
39
- } else output = printer.printFile(sourceFile);
40
- return restoreNewLines(output).replace(/\r\n/g, "\n");
22
+ for (const node of elements) if (node.kind === typescript.default.SyntaxKind.Unknown) console.error("⚠️ Unknown node found:", node);
23
+ return printer.printList(typescript.default.ListFormat.MultiLine, factory.createNodeArray(elements.filter(Boolean)), sourceFile).replace(/\r\n/g, "\n");
41
24
  }
42
25
  function createImport({ name, path: path$1, root, isTypeOnly = false, isNameSpace = false }) {
43
26
  const resolvePath = root ? require_getRelativePath.getRelativePath(root, path$1) : path$1;
44
27
  if (!Array.isArray(name)) {
45
- let importPropertyName = factory.createIdentifier(name);
46
- let importName;
47
- if (isNameSpace) {
48
- importPropertyName = void 0;
49
- importName = factory.createNamespaceImport(factory.createIdentifier(name));
50
- }
51
- return factory.createImportDeclaration(void 0, factory.createImportClause(isTypeOnly, importPropertyName, importName), factory.createStringLiteral(resolvePath), void 0);
28
+ if (isNameSpace) return factory.createImportDeclaration(void 0, factory.createImportClause(isTypeOnly, void 0, factory.createNamespaceImport(factory.createIdentifier(name))), factory.createStringLiteral(resolvePath), void 0);
29
+ return factory.createImportDeclaration(void 0, factory.createImportClause(isTypeOnly, factory.createIdentifier(name), void 0), factory.createStringLiteral(resolvePath), void 0);
52
30
  }
53
- return factory.createImportDeclaration(void 0, factory.createImportClause(isTypeOnly, void 0, factory.createNamedImports(name.map((item) => {
31
+ const specifiers = name.map((item) => {
54
32
  if (typeof item === "object") {
55
- const obj = item;
56
- if (obj.name) return factory.createImportSpecifier(false, factory.createIdentifier(obj.propertyName), factory.createIdentifier(obj.name));
57
- return factory.createImportSpecifier(false, void 0, factory.createIdentifier(obj.propertyName));
33
+ const { propertyName, name: alias } = item;
34
+ return factory.createImportSpecifier(false, alias ? factory.createIdentifier(propertyName) : void 0, factory.createIdentifier(alias !== null && alias !== void 0 ? alias : propertyName));
58
35
  }
59
36
  return factory.createImportSpecifier(false, void 0, factory.createIdentifier(item));
60
- }))), factory.createStringLiteral(resolvePath), void 0);
37
+ });
38
+ return factory.createImportDeclaration(void 0, factory.createImportClause(isTypeOnly, void 0, factory.createNamedImports(specifiers)), factory.createStringLiteral(resolvePath), void 0);
61
39
  }
62
40
  function createExport({ path: path$1, asAlias, isTypeOnly = false, name }) {
63
41
  if (name && !Array.isArray(name) && !asAlias) console.warn(`When using name as string, asAlias should be true ${name}`);
@@ -74,32 +52,36 @@ const typescriptParser = require_createParser.createParser({
74
52
  extNames: [".ts", ".js"],
75
53
  install() {},
76
54
  async parse(file, options = { extname: ".ts" }) {
77
- const source = file.sources.map((item) => item.value).join("\n\n");
78
- const importNodes = file.imports.map((item) => {
55
+ const sourceParts = [];
56
+ for (const item of file.sources) if (item.value) sourceParts.push(item.value);
57
+ const source = sourceParts.join("\n\n");
58
+ const importNodes = [];
59
+ for (const item of file.imports) {
79
60
  const importPath = item.root ? require_getRelativePath.getRelativePath(item.root, item.path) : item.path;
80
61
  const hasExtname = !!node_path.default.extname(importPath);
81
- return createImport({
62
+ importNodes.push(createImport({
82
63
  name: item.name,
83
64
  path: options.extname && hasExtname ? `${require_trimExtName.trimExtName(importPath)}${options.extname}` : item.root ? require_trimExtName.trimExtName(importPath) : importPath,
84
65
  isTypeOnly: item.isTypeOnly
85
- });
86
- }).filter(Boolean);
87
- const exportNodes = file.exports.map((item) => {
66
+ }));
67
+ }
68
+ const exportNodes = [];
69
+ for (const item of file.exports) {
88
70
  const exportPath = item.path;
89
71
  const hasExtname = !!node_path.default.extname(exportPath);
90
- return createExport({
72
+ exportNodes.push(createExport({
91
73
  name: item.name,
92
74
  path: options.extname && hasExtname ? `${require_trimExtName.trimExtName(item.path)}${options.extname}` : require_trimExtName.trimExtName(item.path),
93
75
  isTypeOnly: item.isTypeOnly,
94
76
  asAlias: item.asAlias
95
- });
96
- }).filter(Boolean);
77
+ }));
78
+ }
97
79
  return [
98
80
  file.banner,
99
- print([...importNodes, ...exportNodes]),
81
+ print(...importNodes, ...exportNodes),
100
82
  source,
101
83
  file.footer
102
- ].join("\n");
84
+ ].filter((segment) => segment != null).join("\n");
103
85
  }
104
86
  });
105
87
 
@@ -128,4 +110,4 @@ Object.defineProperty(exports, 'typescriptParser', {
128
110
  return typescriptParser;
129
111
  }
130
112
  });
131
- //# sourceMappingURL=typescriptParser-CNHO6H2_.cjs.map
113
+ //# sourceMappingURL=typescriptParser-D6-3Z7Lj.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"typescriptParser-D6-3Z7Lj.cjs","names":["ts","getRelativePath","path","createParser","sourceParts: Array<string>","importNodes: Array<ts.ImportDeclaration>","trimExtName","exportNodes: Array<ts.ExportDeclaration>"],"sources":["../src/parsers/typescriptParser.ts"],"sourcesContent":["import path from 'node:path'\nimport ts from 'typescript'\nimport { getRelativePath } from '../utils/getRelativePath.ts'\nimport { trimExtName } from '../utils/trimExtName.ts'\nimport { createParser } from './createParser.ts'\n\nconst { factory } = ts\n\n/**\n * Convert AST TypeScript/TSX nodes to a string based on the TypeScript printer.\n */\nexport function print(...elements: Array<ts.Node>): string {\n const sourceFile = ts.createSourceFile('print.tsx', '', ts.ScriptTarget.ES2022, true, ts.ScriptKind.TSX)\n\n const printer = ts.createPrinter({\n omitTrailingSemicolon: true,\n newLine: ts.NewLineKind.LineFeed,\n removeComments: false,\n noEmitHelpers: true,\n })\n\n for (const node of elements) {\n if (node.kind === ts.SyntaxKind.Unknown) {\n console.error('⚠️ Unknown node found:', node)\n }\n }\n\n const output = printer.printList(ts.ListFormat.MultiLine, factory.createNodeArray(elements.filter(Boolean)), sourceFile)\n\n return output.replace(/\\r\\n/g, '\\n')\n}\n\nexport function createImport({\n name,\n path,\n root,\n isTypeOnly = false,\n isNameSpace = false,\n}: {\n name: string | Array<string | { propertyName: string; name?: string }>\n path: string\n root?: string\n isTypeOnly?: boolean\n isNameSpace?: boolean\n}) {\n const resolvePath = root ? getRelativePath(root, path) : path\n\n // Namespace or default import\n if (!Array.isArray(name)) {\n if (isNameSpace) {\n return factory.createImportDeclaration(\n undefined,\n factory.createImportClause(isTypeOnly, undefined, factory.createNamespaceImport(factory.createIdentifier(name))),\n factory.createStringLiteral(resolvePath),\n undefined,\n )\n }\n\n return factory.createImportDeclaration(\n undefined,\n factory.createImportClause(isTypeOnly, factory.createIdentifier(name), undefined),\n factory.createStringLiteral(resolvePath),\n undefined,\n )\n }\n\n // Named imports\n const specifiers = name.map((item) => {\n if (typeof item === 'object') {\n const { propertyName, name: alias } = item\n return factory.createImportSpecifier(false, alias ? factory.createIdentifier(propertyName) : undefined, factory.createIdentifier(alias ?? propertyName))\n }\n\n return factory.createImportSpecifier(false, undefined, factory.createIdentifier(item))\n })\n\n return factory.createImportDeclaration(\n undefined,\n factory.createImportClause(isTypeOnly, undefined, factory.createNamedImports(specifiers)),\n factory.createStringLiteral(resolvePath),\n undefined,\n )\n}\n\nexport function createExport({\n path,\n asAlias,\n isTypeOnly = false,\n name,\n}: {\n path: string\n asAlias?: boolean\n isTypeOnly?: boolean\n name?: string | Array<ts.Identifier | string>\n}) {\n if (name && !Array.isArray(name) && !asAlias) {\n console.warn(`When using name as string, asAlias should be true ${name}`)\n }\n\n if (!Array.isArray(name)) {\n const parsedName = name?.match(/^\\d/) ? `_${name?.slice(1)}` : name\n\n return factory.createExportDeclaration(\n undefined,\n isTypeOnly,\n asAlias && parsedName ? factory.createNamespaceExport(factory.createIdentifier(parsedName)) : undefined,\n factory.createStringLiteral(path),\n undefined,\n )\n }\n\n return factory.createExportDeclaration(\n undefined,\n isTypeOnly,\n factory.createNamedExports(\n name.map((propertyName) => {\n return factory.createExportSpecifier(false, undefined, typeof propertyName === 'string' ? factory.createIdentifier(propertyName) : propertyName)\n }),\n ),\n factory.createStringLiteral(path),\n undefined,\n )\n}\n\nexport const typescriptParser = createParser({\n name: 'typescript',\n extNames: ['.ts', '.js'],\n install() {},\n async parse(file, options = { extname: '.ts' }) {\n const sourceParts: Array<string> = []\n for (const item of file.sources) {\n if (item.value) {\n sourceParts.push(item.value)\n }\n }\n const source = sourceParts.join('\\n\\n')\n\n const importNodes: Array<ts.ImportDeclaration> = []\n for (const item of file.imports) {\n const importPath = item.root ? getRelativePath(item.root, item.path) : item.path\n const hasExtname = !!path.extname(importPath)\n\n importNodes.push(\n createImport({\n name: item.name,\n path: options.extname && hasExtname ? `${trimExtName(importPath)}${options.extname}` : item.root ? trimExtName(importPath) : importPath,\n isTypeOnly: item.isTypeOnly,\n }),\n )\n }\n\n const exportNodes: Array<ts.ExportDeclaration> = []\n for (const item of file.exports) {\n const exportPath = item.path\n const hasExtname = !!path.extname(exportPath)\n\n exportNodes.push(\n createExport({\n name: item.name,\n path: options.extname && hasExtname ? `${trimExtName(item.path)}${options.extname}` : trimExtName(item.path),\n isTypeOnly: item.isTypeOnly,\n asAlias: item.asAlias,\n }),\n )\n }\n\n const parts = [file.banner, print(...importNodes, ...exportNodes), source, file.footer].filter((segment): segment is string => segment != null)\n return parts.join('\\n')\n },\n})\n"],"mappings":";;;;;;;;;AAMA,MAAM,EAAE,YAAYA;;;;AAKpB,SAAgB,MAAM,GAAG,UAAkC;CACzD,MAAM,aAAaA,mBAAG,iBAAiB,aAAa,IAAIA,mBAAG,aAAa,QAAQ,MAAMA,mBAAG,WAAW,IAAI;CAExG,MAAM,UAAUA,mBAAG,cAAc;EAC/B,uBAAuB;EACvB,SAASA,mBAAG,YAAY;EACxB,gBAAgB;EAChB,eAAe;EAChB,CAAC;AAEF,MAAK,MAAM,QAAQ,SACjB,KAAI,KAAK,SAASA,mBAAG,WAAW,QAC9B,SAAQ,MAAM,0BAA0B,KAAK;AAMjD,QAFe,QAAQ,UAAUA,mBAAG,WAAW,WAAW,QAAQ,gBAAgB,SAAS,OAAO,QAAQ,CAAC,EAAE,WAAW,CAE1G,QAAQ,SAAS,KAAK;;AAGtC,SAAgB,aAAa,EAC3B,MACA,cACA,MACA,aAAa,OACb,cAAc,SAOb;CACD,MAAM,cAAc,OAAOC,wCAAgB,MAAMC,OAAK,GAAGA;AAGzD,KAAI,CAAC,MAAM,QAAQ,KAAK,EAAE;AACxB,MAAI,YACF,QAAO,QAAQ,wBACb,QACA,QAAQ,mBAAmB,YAAY,QAAW,QAAQ,sBAAsB,QAAQ,iBAAiB,KAAK,CAAC,CAAC,EAChH,QAAQ,oBAAoB,YAAY,EACxC,OACD;AAGH,SAAO,QAAQ,wBACb,QACA,QAAQ,mBAAmB,YAAY,QAAQ,iB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9C,MAAMC,cAA6B,EAAE;AACrC,OAAK,MAAM,QAAQ,KAAK,QACtB,KAAI,KAAK,MACP,aAAY,KAAK,KAAK,MAAM;EAGhC,MAAM,SAAS,YAAY,KAAK,OAAO;EAEvC,MAAMC,cAA2C,EAAE;AACnD,OAAK,MAAM,QAAQ,KAAK,SAAS;GAC/B,MAAM,aAAa,KAAK,OAAOJ,wCAAgB,KAAK,MAAM,KAAK,KAAK,GAAG,KAAK;GAC5E,MAAM,aAAa,CAAC,CAACC,kBAAK,QAAQ,WAAW;AAE7C,eAAY,KACV,aAAa;IACX,MAAM,KAAK;IACX,MAAM,QAAQ,WAAW,aAAa,GAAGI,gCAAY,WAAW,GAAG,QAAQ,YAAY,KAAK,OAAOA,gCAAY,WAAW,GAAG;IAC7H,YAAY,KAAK;IAClB,CAAC,CACH;;EAGH,MAAMC,cAA2C,EAAE;AACnD,OAAK,MAAM,QAAQ,KAAK,SAAS;GAC/B,MAAM,aAAa,KAAK;GACxB,MAAM,aAAa,CAAC,CAACL,kBAAK,QAAQ,WAAW;AAE7C,eAAY,KACV,aAAa;IACX,MAAM,KAAK;IACX,MAAM,QAAQ,WAAW,aAAa,GAAGI,gCAAY,KAAK,KAAK,GAAG,QAAQ,YAAYA,gCAAY,KAAK,KAAK;IAC5G,YAAY,KAAK;IACjB,SAAS,KAAK;IACf,CAAC,CACH;;AAIH,SADc;GAAC,KAAK;GAAQ,MAAM,GAAG,aAAa,GAAG,YAAY;GAAE;GAAQ,KAAK;GAAO,CAAC,QAAQ,YAA+B,WAAW,KAAK,CAClI,KAAK,KAAK;;CAE1B,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubb/fabric-core",
3
- "version": "0.2.19",
3
+ "version": "0.3.1",
4
4
  "description": "Core functionality for Kubb's plugin-based code generation system, providing the foundation for transforming OpenAPI specifications.",
5
5
  "keywords": [
6
6
  "typescript",
package/src/Fabric.ts CHANGED
@@ -101,7 +101,7 @@ export interface FabricContext<T extends FabricOptions = FabricOptions> extends
101
101
 
102
102
  /** Track installed plugins and parsers to prevent duplicates. */
103
103
  installedPlugins: Set<Plugin>
104
- installedParsers: Set<Parser>
104
+ installedParsers: Map<KubbFile.Extname, Parser>
105
105
  }
106
106
 
107
107
  /**
@@ -100,7 +100,7 @@ export class FileManager {
100
100
 
101
101
  get files(): Array<KubbFile.ResolvedFile> {
102
102
  if (this.#filesCache) {
103
- return [...this.#filesCache]
103
+ return this.#filesCache
104
104
  }
105
105
 
106
106
  const cachedKeys = this.#cache.keys()
@@ -119,7 +119,7 @@ export class FileManager {
119
119
 
120
120
  this.#filesCache = files
121
121
 
122
- return [...files]
122
+ return files
123
123
  }
124
124
 
125
125
  //TODO add test and check if write of FileManager contains the newly added file
@@ -6,7 +6,7 @@ import type { Parser } from './parsers/types.ts'
6
6
  import { AsyncEventEmitter } from './utils/AsyncEventEmitter.ts'
7
7
 
8
8
  export type ProcessFilesProps = {
9
- parsers?: Set<Parser>
9
+ parsers?: Map<KubbFile.Extname, Parser>
10
10
  extension?: Record<KubbFile.Extname, KubbFile.Extname | ''>
11
11
  dryRun?: boolean
12
12
  /**
@@ -16,7 +16,7 @@ export type ProcessFilesProps = {
16
16
  }
17
17
 
18
18
  type GetParseOptions = {
19
- parsers?: Set<Parser>
19
+ parsers?: Map<KubbFile.Extname, Parser>
20
20
  extension?: Record<KubbFile.Extname, KubbFile.Extname | ''>
21
21
  }
22
22
 
@@ -47,13 +47,7 @@ export class FileProcessor {
47
47
  return defaultParser.parse(file, { extname: parseExtName })
48
48
  }
49
49
 
50
- let parser: Parser | undefined
51
- for (const item of parsers) {
52
- if (item.extNames?.includes(file.extname)) {
53
- parser = item
54
- break
55
- }
56
- }
50
+ const parser = parsers.get(file.extname)
57
51
 
58
52
  if (!parser) {
59
53
  return defaultParser.parse(file, { extname: parseExtName })
@@ -68,26 +62,25 @@ export class FileProcessor {
68
62
  ): Promise<KubbFile.ResolvedFile[]> {
69
63
  await this.events.emit('process:start', { files })
70
64
 
71
- let processed = 0
72
65
  const total = files.length
66
+ let processed = 0
73
67
 
74
68
  const processOne = async (resolvedFile: KubbFile.ResolvedFile, index: number) => {
75
- const percentage = (processed / total) * 100
76
-
77
69
  await this.events.emit('file:start', { file: resolvedFile, index, total })
78
70
 
79
71
  const source = dryRun ? undefined : await this.parse(resolvedFile, { extension, parsers })
80
72
 
73
+ const currentProcessed = ++processed
74
+ const percentage = (currentProcessed / total) * 100
75
+
81
76
  await this.events.emit('process:progress', {
82
77
  file: resolvedFile,
83
78
  source,
84
- processed,
79
+ processed: currentProcessed,
85
80
  percentage,
86
81
  total,
87
82
  })
88
83
 
89
- processed++
90
-
91
84
  await this.events.emit('file:end', { file: resolvedFile, index, total })
92
85
  }
93
86