@kubb/fabric-core 0.1.5 → 0.1.7

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 (76) hide show
  1. package/dist/{App-9ie0H1SF.d.ts → App-DVWD6TgC.d.cts} +28 -17
  2. package/dist/{App-KqAHuAyU.d.cts → App-_vPNh477.d.ts} +28 -17
  3. package/dist/createParser-17uGjfu3.js +11 -0
  4. package/dist/createParser-17uGjfu3.js.map +1 -0
  5. package/dist/createParser-C4IkyTs5.cjs +17 -0
  6. package/dist/createParser-C4IkyTs5.cjs.map +1 -0
  7. package/dist/{defaultParser-vwyTb1XT.js → defaultParser--HzU9LPa.js} +2 -2
  8. package/dist/{defaultParser-vwyTb1XT.js.map → defaultParser--HzU9LPa.js.map} +1 -1
  9. package/dist/{defaultParser-Dl-OrbH1.cjs → defaultParser-n9VW2iVf.cjs} +2 -2
  10. package/dist/{defaultParser-Dl-OrbH1.cjs.map → defaultParser-n9VW2iVf.cjs.map} +1 -1
  11. package/dist/defineProperty-BtekiGIK.js +332 -0
  12. package/dist/defineProperty-BtekiGIK.js.map +1 -0
  13. package/dist/defineProperty-CspRhtP3.cjs +364 -0
  14. package/dist/defineProperty-CspRhtP3.cjs.map +1 -0
  15. package/dist/getRelativePath-C6lvNCs7.cjs +26 -0
  16. package/dist/getRelativePath-C6lvNCs7.cjs.map +1 -0
  17. package/dist/getRelativePath-CERJmYkp.js +19 -0
  18. package/dist/getRelativePath-CERJmYkp.js.map +1 -0
  19. package/dist/{index-BpPNNyhl.d.ts → index-CfV-59_M.d.ts} +5 -5
  20. package/dist/{index-DLITiDO5.d.cts → index-DVok6g82.d.cts} +5 -5
  21. package/dist/index.cjs +81 -225
  22. package/dist/index.cjs.map +1 -1
  23. package/dist/index.d.cts +2 -2
  24. package/dist/index.d.ts +2 -2
  25. package/dist/index.js +54 -194
  26. package/dist/index.js.map +1 -1
  27. package/dist/parsers/typescript.cjs +4 -2
  28. package/dist/parsers/typescript.d.cts +2 -2
  29. package/dist/parsers/typescript.d.ts +2 -2
  30. package/dist/parsers/typescript.js +4 -2
  31. package/dist/parsers.cjs +5 -3
  32. package/dist/parsers.cjs.map +1 -1
  33. package/dist/parsers.d.cts +3 -3
  34. package/dist/parsers.d.ts +3 -3
  35. package/dist/parsers.js +5 -3
  36. package/dist/parsers.js.map +1 -1
  37. package/dist/plugins.cjs +260 -33
  38. package/dist/plugins.cjs.map +1 -1
  39. package/dist/plugins.d.cts +48 -8
  40. package/dist/plugins.d.ts +48 -8
  41. package/dist/plugins.js +249 -25
  42. package/dist/plugins.js.map +1 -1
  43. package/dist/{chunk-CUT6urMc.cjs → trimExtName-Bb4zGVF1.cjs} +14 -1
  44. package/dist/trimExtName-Bb4zGVF1.cjs.map +1 -0
  45. package/dist/trimExtName-CeOVQVbu.js +8 -0
  46. package/dist/trimExtName-CeOVQVbu.js.map +1 -0
  47. package/dist/types.d.cts +2 -2
  48. package/dist/types.d.ts +2 -2
  49. package/dist/{typescriptParser-Du4RIToQ.d.cts → typescriptParser-BM90H8Tx.d.cts} +2 -2
  50. package/dist/{typescriptParser-JawJ8wET.cjs → typescriptParser-CNHO6H2_.cjs} +10 -24
  51. package/dist/typescriptParser-CNHO6H2_.cjs.map +1 -0
  52. package/dist/{typescriptParser-CrzOv_Aw.js → typescriptParser-CWT7zCJy.js} +5 -18
  53. package/dist/typescriptParser-CWT7zCJy.js.map +1 -0
  54. package/dist/{typescriptParser-Dk1rwKyJ.d.ts → typescriptParser-Chjs-RhT.d.ts} +2 -2
  55. package/package.json +4 -2
  56. package/src/App.ts +31 -28
  57. package/src/FileManager.ts +8 -1
  58. package/src/FileProcessor.ts +48 -18
  59. package/src/defineApp.ts +5 -10
  60. package/src/parsers/createParser.ts +1 -1
  61. package/src/parsers/types.ts +2 -2
  62. package/src/plugins/barrelPlugin.ts +207 -0
  63. package/src/plugins/createPlugin.ts +1 -1
  64. package/src/plugins/fsPlugin.ts +33 -34
  65. package/src/plugins/index.ts +2 -0
  66. package/src/plugins/progressPlugin.ts +48 -0
  67. package/src/plugins/types.ts +3 -3
  68. package/src/utils/AsyncEventEmitter.ts +8 -0
  69. package/src/utils/EventEmitter.ts +8 -0
  70. package/src/utils/TreeNode.ts +118 -0
  71. package/dist/createParser-B_RpW6sx.js +0 -17
  72. package/dist/createParser-B_RpW6sx.js.map +0 -1
  73. package/dist/createParser-DZB5qExa.cjs +0 -29
  74. package/dist/createParser-DZB5qExa.cjs.map +0 -1
  75. package/dist/typescriptParser-CrzOv_Aw.js.map +0 -1
  76. package/dist/typescriptParser-JawJ8wET.cjs.map +0 -1
@@ -0,0 +1,48 @@
1
+ import { Presets, SingleBar } from 'cli-progress'
2
+ import { createPlugin } from './createPlugin.ts'
3
+ import { relative } from 'node:path'
4
+ import process from 'node:process'
5
+
6
+ type Options = {}
7
+
8
+ // biome-ignore lint/suspicious/noTsIgnore: production ready
9
+ // @ts-ignore
10
+ declare module '@kubb/fabric-core' {
11
+ interface App {}
12
+ }
13
+
14
+ declare global {
15
+ namespace Kubb {
16
+ interface App {}
17
+ }
18
+ }
19
+
20
+ export const progressPlugin = createPlugin<Options>({
21
+ name: 'progress',
22
+ install(app) {
23
+ const progressBar = new SingleBar(
24
+ {
25
+ format: '{bar} {percentage}% | {value}/{total} | {message}',
26
+ barCompleteChar: '█',
27
+ barIncompleteChar: '░',
28
+ hideCursor: true,
29
+ clearOnComplete: true,
30
+ },
31
+ Presets.shades_grey,
32
+ )
33
+
34
+ app.context.events.on('process:start', async ({ files }) => {
35
+ progressBar.start(files.length, 0, { message: 'Starting...' })
36
+ })
37
+
38
+ app.context.events.on('process:progress', async ({ file }) => {
39
+ const message = `Writing ${relative(process.cwd(), file.path)}`
40
+ progressBar.increment(1, { message })
41
+ })
42
+
43
+ app.context.events.on('process:end', async ({ files }) => {
44
+ progressBar.update(files.length, { message: 'Done ✅' })
45
+ progressBar.stop()
46
+ })
47
+ },
48
+ })
@@ -1,9 +1,9 @@
1
1
  import type { Install, Inject } from '../App.ts'
2
2
 
3
- export type Plugin<TOptions = any[], TAppExtension extends Record<string, any> = {}> = {
3
+ export type Plugin<TOptions = unknown, TAppExtension extends Record<string, any> = {}> = {
4
4
  name: string
5
5
  type: 'plugin'
6
- install: Install<TOptions> | Promise<Install<TOptions>>
6
+ install: Install<TOptions>
7
7
  /**
8
8
  * Runtime app overrides or extensions.
9
9
  * Merged into the app instance after install.
@@ -12,4 +12,4 @@ export type Plugin<TOptions = any[], TAppExtension extends Record<string, any> =
12
12
  inject?: Inject<TOptions, TAppExtension>
13
13
  }
14
14
 
15
- export type UserPlugin<TOptions = any[], TAppExtension extends Record<string, any> = {}> = Omit<Plugin<TOptions, TAppExtension>, 'type'>
15
+ export type UserPlugin<TOptions = unknown, TAppExtension extends Record<string, any> = {}> = Omit<Plugin<TOptions, TAppExtension>, 'type'>
@@ -28,6 +28,14 @@ export class AsyncEventEmitter<TEvents extends Record<string, any>> {
28
28
  this.#emitter.on(eventName, handler as any)
29
29
  }
30
30
 
31
+ onOnce<TEventName extends keyof TEvents & string>(eventName: TEventName, handler: (...eventArgs: TEvents[TEventName]) => void): void {
32
+ const wrapper = (...args: TEvents[TEventName]) => {
33
+ this.off(eventName, wrapper)
34
+ handler(...args)
35
+ }
36
+ this.on(eventName, wrapper)
37
+ }
38
+
31
39
  off<TEventName extends keyof TEvents & string>(eventName: TEventName, handler: (...eventArg: TEvents[TEventName]) => void): void {
32
40
  this.#emitter.off(eventName, handler as any)
33
41
  }
@@ -14,6 +14,14 @@ export class EventEmitter<TEvents extends Record<string, any>> {
14
14
  this.#emitter.on(eventName, handler as any)
15
15
  }
16
16
 
17
+ onOnce<TEventName extends keyof TEvents & string>(eventName: TEventName, handler: (...eventArgs: TEvents[TEventName]) => void): void {
18
+ const wrapper = (...args: TEvents[TEventName]) => {
19
+ this.off(eventName, wrapper)
20
+ handler(...args)
21
+ }
22
+ this.on(eventName, wrapper)
23
+ }
24
+
17
25
  off<TEventName extends keyof TEvents & string>(eventName: TEventName, handler: (...eventArg: TEvents[TEventName]) => void): void {
18
26
  this.#emitter.off(eventName, handler as any)
19
27
  }
@@ -0,0 +1,118 @@
1
+ import type * as KubbFile from '../KubbFile.ts'
2
+
3
+ type BarrelData = {
4
+ file?: KubbFile.File
5
+ path: string
6
+ name: string
7
+ }
8
+
9
+ export class TreeNode<TData = unknown> {
10
+ data: TData
11
+ parent?: TreeNode<TData>
12
+ children: Array<TreeNode<TData>> = []
13
+ #cachedLeaves?: Array<TreeNode<TData>>
14
+
15
+ constructor(data: TData, parent?: TreeNode<TData>) {
16
+ this.data = data
17
+ this.parent = parent
18
+ }
19
+
20
+ addChild(data: TData): TreeNode<TData> {
21
+ const child = new TreeNode(data, this)
22
+ this.children.push(child)
23
+ this.#cachedLeaves = undefined // invalidate cached leaves
24
+ return child
25
+ }
26
+
27
+ get leaves(): Array<TreeNode<TData>> {
28
+ if (this.#cachedLeaves) return this.#cachedLeaves
29
+ if (this.children.length === 0) return [this]
30
+
31
+ const stack: Array<TreeNode<TData>> = [...this.children]
32
+ const result: Array<TreeNode<TData>> = []
33
+
34
+ for (const node of stack) {
35
+ if (node.children.length) {
36
+ for (const child of node.children) stack.push(child)
37
+ } else {
38
+ result.push(node)
39
+ }
40
+ }
41
+
42
+ this.#cachedLeaves = result
43
+ return result
44
+ }
45
+
46
+ forEach(callback: (node: TreeNode<TData>) => void): this {
47
+ const stack: Array<TreeNode<TData>> = [this]
48
+
49
+ for (const node of stack) {
50
+ callback(node)
51
+ if (node.children.length) {
52
+ for (const child of node.children) stack.push(child)
53
+ }
54
+ }
55
+ return this
56
+ }
57
+
58
+ findDeep(predicate: (node: TreeNode<TData>) => boolean): TreeNode<TData> | undefined {
59
+ for (const leaf of this.leaves) {
60
+ if (predicate(leaf)) return leaf
61
+ }
62
+ return undefined
63
+ }
64
+
65
+ static fromFiles(files: Array<KubbFile.File>, rootFolder = ''): TreeNode<BarrelData> | null {
66
+ const normalizePath = (p: string): string => p.replace(/\\/g, '/')
67
+ const normalizedRoot = normalizePath(rootFolder)
68
+ const rootPrefix = normalizedRoot.endsWith('/') ? normalizedRoot : `${normalizedRoot}/`
69
+
70
+ const filteredFiles = files.filter((file) => {
71
+ const filePath = normalizePath(file.path)
72
+ return !filePath.endsWith('.json') && (!rootFolder || filePath.startsWith(rootPrefix))
73
+ })
74
+
75
+ if (filteredFiles.length === 0) {
76
+ return null
77
+ }
78
+
79
+ const treeNode = new TreeNode<BarrelData>({
80
+ name: rootFolder || '',
81
+ path: rootFolder || '',
82
+ file: undefined,
83
+ })
84
+
85
+ for (const file of filteredFiles) {
86
+ const relPath = normalizePath(file.path).slice(rootPrefix.length)
87
+ const parts = relPath.split('/')
88
+
89
+ let current = treeNode
90
+ let currentPath = rootFolder
91
+
92
+ for (const [index, part] of parts.entries()) {
93
+ const isLast = index === parts.length - 1
94
+ currentPath += (currentPath.endsWith('/') ? '' : '/') + part
95
+
96
+ let next: TreeNode<BarrelData> | undefined
97
+ for (const child of current.children) {
98
+ if ((child.data as BarrelData).name === part) {
99
+ next = child
100
+ break
101
+ }
102
+ }
103
+
104
+ if (!next) {
105
+ next = current.addChild({
106
+ name: part,
107
+ path: currentPath,
108
+ file: isLast ? file : undefined,
109
+ })
110
+ }
111
+
112
+ current = next
113
+ }
114
+ }
115
+
116
+ return treeNode
117
+ }
118
+ }
@@ -1,17 +0,0 @@
1
- //#region src/utils/trimExtName.ts
2
- function trimExtName(text) {
3
- return text.replace(/\.[^/.]+$/, "");
4
- }
5
-
6
- //#endregion
7
- //#region src/parsers/createParser.ts
8
- function createParser(parser) {
9
- return {
10
- type: "parser",
11
- ...parser
12
- };
13
- }
14
-
15
- //#endregion
16
- export { trimExtName as n, createParser as t };
17
- //# sourceMappingURL=createParser-B_RpW6sx.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createParser-B_RpW6sx.js","names":[],"sources":["../src/utils/trimExtName.ts","../src/parsers/createParser.ts"],"sourcesContent":["export function trimExtName(text: string): string {\n return text.replace(/\\.[^/.]+$/, '')\n}\n","import type { Parser, UserParser } from './types.ts'\n\nexport function createParser<TOptions = any[], TMeta extends object = any>(parser: UserParser<TOptions, TMeta>): Parser<TOptions, TMeta> {\n return {\n type: 'parser',\n ...parser,\n }\n}\n"],"mappings":";AAAA,SAAgB,YAAY,MAAsB;AAChD,QAAO,KAAK,QAAQ,aAAa,GAAG;;;;;ACCtC,SAAgB,aAA2D,QAA8D;AACvI,QAAO;EACL,MAAM;EACN,GAAG;EACJ"}
@@ -1,29 +0,0 @@
1
-
2
- //#region src/utils/trimExtName.ts
3
- function trimExtName(text) {
4
- return text.replace(/\.[^/.]+$/, "");
5
- }
6
-
7
- //#endregion
8
- //#region src/parsers/createParser.ts
9
- function createParser(parser) {
10
- return {
11
- type: "parser",
12
- ...parser
13
- };
14
- }
15
-
16
- //#endregion
17
- Object.defineProperty(exports, 'createParser', {
18
- enumerable: true,
19
- get: function () {
20
- return createParser;
21
- }
22
- });
23
- Object.defineProperty(exports, 'trimExtName', {
24
- enumerable: true,
25
- get: function () {
26
- return trimExtName;
27
- }
28
- });
29
- //# sourceMappingURL=createParser-DZB5qExa.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createParser-DZB5qExa.cjs","names":[],"sources":["../src/utils/trimExtName.ts","../src/parsers/createParser.ts"],"sourcesContent":["export function trimExtName(text: string): string {\n return text.replace(/\\.[^/.]+$/, '')\n}\n","import type { Parser, UserParser } from './types.ts'\n\nexport function createParser<TOptions = any[], TMeta extends object = any>(parser: UserParser<TOptions, TMeta>): Parser<TOptions, TMeta> {\n return {\n type: 'parser',\n ...parser,\n }\n}\n"],"mappings":";;AAAA,SAAgB,YAAY,MAAsB;AAChD,QAAO,KAAK,QAAQ,aAAa,GAAG;;;;;ACCtC,SAAgB,aAA2D,QAA8D;AACvI,QAAO;EACL,MAAM;EACN,GAAG;EACJ"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"typescriptParser-CrzOv_Aw.js","names":["path","output: string","path","importPropertyName: ts.Identifier | undefined","importName: ts.NamedImportBindings | undefined"],"sources":["../src/utils/getRelativePath.ts","../src/parsers/typescriptParser.ts"],"sourcesContent":["import { normalize, relative } from 'node:path'\n\nfunction slash(path: string, platform: 'windows' | 'mac' | 'linux' = 'linux') {\n const isWindowsPath = /^\\\\\\\\\\?\\\\/.test(path)\n const normalizedPath = normalize(path)\n\n if (['linux', 'mac'].includes(platform) && !isWindowsPath) {\n // linux and mac\n return normalizedPath.replaceAll(/\\\\/g, '/').replace('../', '')\n }\n\n // windows\n return normalizedPath.replaceAll(/\\\\/g, '/').replace('../', '')\n}\n\nexport function getRelativePath(rootDir?: string | null, filePath?: string | null, platform: 'windows' | 'mac' | 'linux' = 'linux'): string {\n if (!rootDir || !filePath) {\n throw new Error(`Root and file should be filled in when retrieving the relativePath, ${rootDir || ''} ${filePath || ''}`)\n }\n\n const relativePath = relative(rootDir, filePath)\n\n // On Windows, paths are separated with a \"\\\"\n // However, web browsers use \"/\" no matter the platform\n const slashedPath = slash(relativePath, platform)\n\n if (slashedPath.startsWith('../')) {\n return slashedPath\n }\n\n return `./${slashedPath}`\n}\n","import ts from 'typescript'\nimport { getRelativePath } from '../utils/getRelativePath.ts'\nimport { trimExtName } from '../utils/trimExtName.ts'\nimport path from 'node:path'\nimport { createParser } from './createParser.ts'\n\nconst { factory } = ts\n\ntype PrintOptions = {\n source?: string\n baseName?: string\n scriptKind?: ts.ScriptKind\n}\n\n/**\n * Escaped new lines in code with block comments so they can be restored by {@link restoreNewLines}\n */\nconst escapeNewLines = (code: string) => code.replace(/\\n\\n/g, '\\n/* :newline: */')\n\n/**\n * Reverses {@link escapeNewLines} and restores new lines\n */\nconst restoreNewLines = (code: string) => code.replace(/\\/\\* :newline: \\*\\//g, '\\n')\n\n/**\n * Convert AST TypeScript/TSX nodes to a string based on the TypeScript printer.\n * Ensures consistent output across environments.\n * Also works as a formatter when `source` is provided without `elements`.\n */\nexport function print(elements: Array<ts.Node> = [], { source = '', baseName = 'print.tsx', scriptKind = ts.ScriptKind.TSX }: PrintOptions = {}): string {\n const sourceFile = ts.createSourceFile(baseName, escapeNewLines(source), ts.ScriptTarget.ES2022, true, scriptKind)\n\n const printer = ts.createPrinter({\n omitTrailingSemicolon: true,\n newLine: ts.NewLineKind.LineFeed,\n removeComments: false,\n noEmitHelpers: true,\n })\n\n let output: string\n\n if (elements.length > 0) {\n // Print only provided nodes\n const nodes = elements.filter(Boolean).sort((a, b) => (a.pos ?? 0) - (b.pos ?? 0))\n output = printer.printList(ts.ListFormat.MultiLine, factory.createNodeArray(nodes), sourceFile)\n } else {\n // Format the whole file\n output = printer.printFile(sourceFile)\n }\n\n return restoreNewLines(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 if (!Array.isArray(name)) {\n let importPropertyName: ts.Identifier | undefined = factory.createIdentifier(name)\n let importName: ts.NamedImportBindings | undefined\n\n if (isNameSpace) {\n importPropertyName = undefined\n importName = factory.createNamespaceImport(factory.createIdentifier(name))\n }\n\n return factory.createImportDeclaration(\n undefined,\n factory.createImportClause(isTypeOnly, importPropertyName, importName),\n factory.createStringLiteral(resolvePath),\n undefined,\n )\n }\n\n return factory.createImportDeclaration(\n undefined,\n factory.createImportClause(\n isTypeOnly,\n undefined,\n factory.createNamedImports(\n name.map((item) => {\n if (typeof item === 'object') {\n const obj = item as { propertyName: string; name?: string }\n if (obj.name) {\n return factory.createImportSpecifier(false, factory.createIdentifier(obj.propertyName), factory.createIdentifier(obj.name))\n }\n\n return factory.createImportSpecifier(false, undefined, factory.createIdentifier(obj.propertyName))\n }\n\n return factory.createImportSpecifier(false, undefined, factory.createIdentifier(item))\n }),\n ),\n ),\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 source = file.sources.map((item) => item.value).join('\\n\\n')\n\n const importNodes = file.imports\n .map((item) => {\n const importPath = item.root ? getRelativePath(item.root, item.path) : item.path\n const hasExtname = !!path.extname(importPath)\n\n return 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 .filter(Boolean)\n\n const exportNodes = file.exports\n .map((item) => {\n const exportPath = item.path\n\n const hasExtname = !!path.extname(exportPath)\n\n return 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 .filter(Boolean)\n\n return [file.banner, print([...importNodes, ...exportNodes]), source, file.footer].join('\\n')\n },\n})\n"],"mappings":";;;;;AAEA,SAAS,MAAM,QAAc,WAAwC,SAAS;CAC5E,MAAM,gBAAgB,YA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AAG,YAAY,CAAC;GAAE;GAAQ,KAAK;GAAO,CAAC,KAAK,KAAK;;CAEhG,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"typescriptParser-JawJ8wET.cjs","names":["path","ts","output: string","path","importPropertyName: ts.Identifier | undefined","importName: ts.NamedImportBindings | undefined","createParser","trimExtName"],"sources":["../src/utils/getRelativePath.ts","../src/parsers/typescriptParser.ts"],"sourcesContent":["import { normalize, relative } from 'node:path'\n\nfunction slash(path: string, platform: 'windows' | 'mac' | 'linux' = 'linux') {\n const isWindowsPath = /^\\\\\\\\\\?\\\\/.test(path)\n const normalizedPath = normalize(path)\n\n if (['linux', 'mac'].includes(platform) && !isWindowsPath) {\n // linux and mac\n return normalizedPath.replaceAll(/\\\\/g, '/').replace('../', '')\n }\n\n // windows\n return normalizedPath.replaceAll(/\\\\/g, '/').replace('../', '')\n}\n\nexport function getRelativePath(rootDir?: string | null, filePath?: string | null, platform: 'windows' | 'mac' | 'linux' = 'linux'): string {\n if (!rootDir || !filePath) {\n throw new Error(`Root and file should be filled in when retrieving the relativePath, ${rootDir || ''} ${filePath || ''}`)\n }\n\n const relativePath = relative(rootDir, filePath)\n\n // On Windows, paths are separated with a \"\\\"\n // However, web browsers use \"/\" no matter the platform\n const slashedPath = slash(relativePath, platform)\n\n if (slashedPath.startsWith('../')) {\n return slashedPath\n }\n\n return `./${slashedPath}`\n}\n","import ts from 'typescript'\nimport { getRelativePath } from '../utils/getRelativePath.ts'\nimport { trimExtName } from '../utils/trimExtName.ts'\nimport path from 'node:path'\nimport { createParser } from './createParser.ts'\n\nconst { factory } = ts\n\ntype PrintOptions = {\n source?: string\n baseName?: string\n scriptKind?: ts.ScriptKind\n}\n\n/**\n * Escaped new lines in code with block comments so they can be restored by {@link restoreNewLines}\n */\nconst escapeNewLines = (code: string) => code.replace(/\\n\\n/g, '\\n/* :newline: */')\n\n/**\n * Reverses {@link escapeNewLines} and restores new lines\n */\nconst restoreNewLines = (code: string) => code.replace(/\\/\\* :newline: \\*\\//g, '\\n')\n\n/**\n * Convert AST TypeScript/TSX nodes to a string based on the TypeScript printer.\n * Ensures consistent output across environments.\n * Also works as a formatter when `source` is provided without `elements`.\n */\nexport function print(elements: Array<ts.Node> = [], { source = '', baseName = 'print.tsx', scriptKind = ts.ScriptKind.TSX }: PrintOptions = {}): string {\n const sourceFile = ts.createSourceFile(baseName, escapeNewLines(source), ts.ScriptTarget.ES2022, true, scriptKind)\n\n const printer = ts.createPrinter({\n omitTrailingSemicolon: true,\n newLine: ts.NewLineKind.LineFeed,\n removeComments: false,\n noEmitHelpers: true,\n })\n\n let output: string\n\n if (elements.length > 0) {\n // Print only provided nodes\n const nodes = elements.filter(Boolean).sort((a, b) => (a.pos ?? 0) - (b.pos ?? 0))\n output = printer.printList(ts.ListFormat.MultiLine, factory.createNodeArray(nodes), sourceFile)\n } else {\n // Format the whole file\n output = printer.printFile(sourceFile)\n }\n\n return restoreNewLines(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 if (!Array.isArray(name)) {\n let importPropertyName: ts.Identifier | undefined = factory.createIdentifier(name)\n let importName: ts.NamedImportBindings | undefined\n\n if (isNameSpace) {\n importPropertyName = undefined\n importName = factory.createNamespaceImport(factory.createIdentifier(name))\n }\n\n return factory.createImportDeclaration(\n undefined,\n factory.createImportClause(isTypeOnly, importPropertyName, importName),\n factory.createStringLiteral(resolvePath),\n undefined,\n )\n }\n\n return factory.createImportDeclaration(\n undefined,\n factory.createImportClause(\n isTypeOnly,\n undefined,\n factory.createNamedImports(\n name.map((item) => {\n if (typeof item === 'object') {\n const obj = item as { propertyName: string; name?: string }\n if (obj.name) {\n return factory.createImportSpecifier(false, factory.createIdentifier(obj.propertyName), factory.createIdentifier(obj.name))\n }\n\n return factory.createImportSpecifier(false, undefined, factory.createIdentifier(obj.propertyName))\n }\n\n return factory.createImportSpecifier(false, undefined, factory.createIdentifier(item))\n }),\n ),\n ),\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 source = file.sources.map((item) => item.value).join('\\n\\n')\n\n const importNodes = file.imports\n .map((item) => {\n const importPath = item.root ? getRelativePath(item.root, item.path) : item.path\n const hasExtname = !!path.extname(importPath)\n\n return 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 .filter(Boolean)\n\n const exportNodes = file.exports\n .map((item) => {\n const exportPath = item.path\n\n const hasExtname = !!path.extname(exportPath)\n\n return createExport({\n name: item.name,\n path: options.extname && hasExtname ? `${trimExtName(item.path)}${options.extname}` : trimExtName(item.path),\n isTypeOnly: item.isTypeOnly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