@kubb/fabric-core 0.1.5 → 0.1.6
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/{App-9ie0H1SF.d.ts → App-Cplfh8QA.d.cts} +12 -12
- package/dist/{App-KqAHuAyU.d.cts → App-Dvetv2V_.d.ts} +12 -12
- package/dist/createParser-17uGjfu3.js +11 -0
- package/dist/createParser-17uGjfu3.js.map +1 -0
- package/dist/createParser-C4IkyTs5.cjs +17 -0
- package/dist/createParser-C4IkyTs5.cjs.map +1 -0
- package/dist/{defaultParser-vwyTb1XT.js → defaultParser--HzU9LPa.js} +2 -2
- package/dist/{defaultParser-vwyTb1XT.js.map → defaultParser--HzU9LPa.js.map} +1 -1
- package/dist/{defaultParser-Dl-OrbH1.cjs → defaultParser-n9VW2iVf.cjs} +2 -2
- package/dist/{defaultParser-Dl-OrbH1.cjs.map → defaultParser-n9VW2iVf.cjs.map} +1 -1
- package/dist/defineProperty-3OJdpith.js +168 -0
- package/dist/defineProperty-3OJdpith.js.map +1 -0
- package/dist/defineProperty-CjCLDutJ.cjs +201 -0
- package/dist/defineProperty-CjCLDutJ.cjs.map +1 -0
- package/dist/getRelativePath-C6lvNCs7.cjs +26 -0
- package/dist/getRelativePath-C6lvNCs7.cjs.map +1 -0
- package/dist/getRelativePath-CERJmYkp.js +19 -0
- package/dist/getRelativePath-CERJmYkp.js.map +1 -0
- package/dist/{index-BpPNNyhl.d.ts → index-Agz-2M75.d.ts} +2 -2
- package/dist/{index-DLITiDO5.d.cts → index-C3GyFwE1.d.cts} +2 -2
- package/dist/index.cjs +54 -204
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +26 -173
- package/dist/index.js.map +1 -1
- package/dist/parsers/typescript.cjs +4 -2
- package/dist/parsers/typescript.d.cts +2 -2
- package/dist/parsers/typescript.d.ts +2 -2
- package/dist/parsers/typescript.js +4 -2
- package/dist/parsers.cjs +5 -3
- package/dist/parsers.cjs.map +1 -1
- package/dist/parsers.d.cts +3 -3
- package/dist/parsers.d.ts +3 -3
- package/dist/parsers.js +5 -3
- package/dist/parsers.js.map +1 -1
- package/dist/plugins.cjs +207 -19
- package/dist/plugins.cjs.map +1 -1
- package/dist/plugins.d.cts +29 -6
- package/dist/plugins.d.ts +29 -6
- package/dist/plugins.js +194 -6
- package/dist/plugins.js.map +1 -1
- package/dist/{chunk-CUT6urMc.cjs → trimExtName-Bb4zGVF1.cjs} +14 -1
- package/dist/trimExtName-Bb4zGVF1.cjs.map +1 -0
- package/dist/trimExtName-CeOVQVbu.js +8 -0
- package/dist/trimExtName-CeOVQVbu.js.map +1 -0
- package/dist/types.d.cts +2 -2
- package/dist/types.d.ts +2 -2
- package/dist/{typescriptParser-Dk1rwKyJ.d.ts → typescriptParser-B49WHoGL.d.ts} +2 -2
- package/dist/{typescriptParser-JawJ8wET.cjs → typescriptParser-CNHO6H2_.cjs} +10 -24
- package/dist/typescriptParser-CNHO6H2_.cjs.map +1 -0
- package/dist/{typescriptParser-CrzOv_Aw.js → typescriptParser-CWT7zCJy.js} +5 -18
- package/dist/typescriptParser-CWT7zCJy.js.map +1 -0
- package/dist/{typescriptParser-Du4RIToQ.d.cts → typescriptParser-CxGhFQXh.d.cts} +2 -2
- package/package.json +1 -1
- package/src/App.ts +19 -25
- package/src/FileManager.ts +8 -1
- package/src/FileProcessor.ts +7 -1
- package/src/defineApp.ts +2 -7
- package/src/parsers/createParser.ts +1 -1
- package/src/parsers/types.ts +2 -2
- package/src/plugins/barrelPlugin.ts +189 -0
- package/src/plugins/createPlugin.ts +1 -1
- package/src/plugins/fsPlugin.ts +5 -7
- package/src/plugins/index.ts +1 -0
- package/src/plugins/types.ts +3 -3
- package/src/utils/AsyncEventEmitter.ts +8 -0
- package/src/utils/EventEmitter.ts +8 -0
- package/src/utils/TreeNode.ts +118 -0
- package/dist/createParser-B_RpW6sx.js +0 -17
- package/dist/createParser-B_RpW6sx.js.map +0 -1
- package/dist/createParser-DZB5qExa.cjs +0 -29
- package/dist/createParser-DZB5qExa.cjs.map +0 -1
- package/dist/typescriptParser-CrzOv_Aw.js.map +0 -1
- package/dist/typescriptParser-JawJ8wET.cjs.map +0 -1
package/dist/plugins.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugins.js","names":["path","data"],"sources":["../src/plugins/createPlugin.ts","../src/plugins/fsPlugin.ts"],"sourcesContent":["import type { Plugin, UserPlugin } from './types.ts'\n\nexport function createPlugin<Options = any[], TAppExtension extends Record<string, any> = {}>(\n plugin: UserPlugin<Options, TAppExtension>,\n): Plugin<Options, TAppExtension> {\n return {\n type: 'plugin',\n ...plugin,\n }\n}\n","import { createPlugin } from './createPlugin.ts'\nimport { switcher } from 'js-runtime'\nimport fs from 'fs-extra'\nimport { resolve } from 'node:path'\nimport type * as KubbFile from '../KubbFile.ts'\n\ntype WriteOptions = {\n extension?: Record<KubbFile.Extname, KubbFile.Extname | ''>\n dryRun?: boolean\n}\n\ntype Options = {\n /**\n * Optional callback that is invoked whenever a file is written by the plugin.\n * Useful for tests to observe write operations without spying on internal functions.\n */\n onWrite?: (path: string, data: string) => void | Promise<void>\n clean?: {\n path: string\n }\n}\n\ntype ExtendOptions = {\n write(options?: WriteOptions): Promise<void>\n}\n\nexport async function write(path: string, data: string, options: { sanity?: boolean } = {}): Promise<string | undefined> {\n if (data.trim() === '') {\n return undefined\n }\n return switcher(\n {\n node: async (path: string, data: string, { sanity }: { sanity?: boolean }) => {\n try {\n const oldContent = await fs.readFile(resolve(path), {\n encoding: 'utf-8',\n })\n if (oldContent?.toString() === data?.toString()) {\n return\n }\n } catch (_err) {\n /* empty */\n }\n\n await fs.outputFile(resolve(path), data, { encoding: 'utf-8' })\n\n if (sanity) {\n const savedData = await fs.readFile(resolve(path), {\n encoding: 'utf-8',\n })\n\n if (savedData?.toString() !== data?.toString()) {\n throw new Error(`Sanity check failed for ${path}\\n\\nData[${data.length}]:\\n${data}\\n\\nSaved[${savedData.length}]:\\n${savedData}\\n`)\n }\n\n return savedData\n }\n\n return data\n },\n bun: async (path: string, data: string, { sanity }: { sanity?: boolean }) => {\n try {\n await Bun.write(resolve(path), data)\n\n if (sanity) {\n const file = Bun.file(resolve(path))\n const savedData = await file.text()\n\n if (savedData?.toString() !== data?.toString()) {\n throw new Error(`Sanity check failed for ${path}\\n\\nData[${path.length}]:\\n${path}\\n\\nSaved[${savedData.length}]:\\n${savedData}\\n`)\n }\n\n return savedData\n }\n\n return data\n } catch (e) {\n console.error(e)\n }\n },\n },\n 'node',\n )(path, data.trim(), options)\n}\n\n// biome-ignore lint/suspicious/noTsIgnore: production ready\n// @ts-ignore\ndeclare module '@kubb/fabric-core' {\n interface App {\n write(options?: WriteOptions): Promise<void>\n }\n}\n\ndeclare global {\n namespace Kubb {\n interface App {\n write(options?: WriteOptions): Promise<void>\n }\n }\n}\n\nexport const fsPlugin = createPlugin<Options, ExtendOptions>({\n name: 'fs',\n async install(app, options) {\n app.context.events.on('process:progress', async ({ file, source }) => {\n if (options?.onWrite) {\n await options.onWrite(file.path, source)\n }\n await write(file.path, source, { sanity: false })\n })\n\n app.context.events.on('start', async () => {\n if (options?.clean) {\n await fs.remove(options.clean.path)\n }\n })\n },\n inject(app) {\n return {\n async write(\n options = {\n extension: { '.ts': '.ts' },\n dryRun: false,\n },\n ) {\n await app.context.fileManager.write({\n extension: options.extension,\n dryRun: options.dryRun,\n parsers: app.context.installedParsers,\n })\n },\n }\n },\n})\n"],"mappings":";;;;;AAEA,SAAgB,aACd,QACgC;AAChC,QAAO;EACL,MAAM;EACN,GAAG;EACJ;;;;;ACkBH,eAAsB,MAAM,QAAc,MAAc,UAAgC,EAAE,EAA+B;AACvH,KAAI,KAAK,MAAM,KAAK,GAClB;AAEF,QAAO,SACL;EACE,MAAM,OAAO,QAAc,QAAc,EAAE,aAAmC;AAC5E,OAAI;IACF,MAAM,aAAa,MAAM,GAAG,SAAS,QA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YAAY;AACzC,yDAAI,QAAS,MACX,OAAM,GAAG,OAAO,QAAQ,MAAM,KAAK;IAErC;;CAEJ,OAAO,KAAK;AACV,SAAO,EACL,MAAM,MACJ,UAAU;GACR,WAAW,EAAE,OAAO,OAAO;GAC3B,QAAQ;GACT,EACD;AACA,SAAM,IAAI,QAAQ,YAAY,MAAM;IAClC,WAAW,QAAQ;IACnB,QAAQ,QAAQ;IAChB,SAAS,IAAI,QAAQ;IACtB,CAAC;KAEL;;CAEJ,CAAC"}
|
|
1
|
+
{"version":3,"file":"plugins.js","names":["path","data","stack: Array<TreeNode<TData>>","result: Array<TreeNode<TData>>","next: TreeNode<BarrelData> | undefined"],"sources":["../src/plugins/createPlugin.ts","../src/plugins/fsPlugin.ts","../src/utils/TreeNode.ts","../src/plugins/barrelPlugin.ts"],"sourcesContent":["import type { Plugin, UserPlugin } from './types.ts'\n\nexport function createPlugin<Options = unknown, TAppExtension extends Record<string, any> = {}>(\n plugin: UserPlugin<Options, TAppExtension>,\n): Plugin<Options, TAppExtension> {\n return {\n type: 'plugin',\n ...plugin,\n }\n}\n","import { createPlugin } from './createPlugin.ts'\nimport { switcher } from 'js-runtime'\nimport fs from 'fs-extra'\nimport { resolve } from 'node:path'\nimport type * as KubbFile from '../KubbFile.ts'\n\ntype WriteOptions = {\n extension?: Record<KubbFile.Extname, KubbFile.Extname | ''>\n dryRun?: boolean\n}\n\ntype Options = {\n /**\n * Optional callback that is invoked whenever a file is written by the plugin.\n * Useful for tests to observe write operations without spying on internal functions.\n */\n onWrite?: (path: string, data: string) => void | Promise<void>\n clean?: {\n path: string\n }\n}\n\ntype ExtendOptions = {\n write(options?: WriteOptions): Promise<void>\n}\n\nexport async function write(path: string, data: string, options: { sanity?: boolean } = {}): Promise<string | undefined> {\n if (data.trim() === '') {\n return undefined\n }\n return switcher(\n {\n node: async (path: string, data: string, { sanity }: { sanity?: boolean }) => {\n try {\n const oldContent = await fs.readFile(resolve(path), {\n encoding: 'utf-8',\n })\n if (oldContent?.toString() === data?.toString()) {\n return\n }\n } catch (_err) {\n /* empty */\n }\n\n await fs.outputFile(resolve(path), data, { encoding: 'utf-8' })\n\n if (sanity) {\n const savedData = await fs.readFile(resolve(path), {\n encoding: 'utf-8',\n })\n\n if (savedData?.toString() !== data?.toString()) {\n throw new Error(`Sanity check failed for ${path}\\n\\nData[${data.length}]:\\n${data}\\n\\nSaved[${savedData.length}]:\\n${savedData}\\n`)\n }\n\n return savedData\n }\n\n return data\n },\n bun: async (path: string, data: string, { sanity }: { sanity?: boolean }) => {\n try {\n await Bun.write(resolve(path), data)\n\n if (sanity) {\n const file = Bun.file(resolve(path))\n const savedData = await file.text()\n\n if (savedData?.toString() !== data?.toString()) {\n throw new Error(`Sanity check failed for ${path}\\n\\nData[${path.length}]:\\n${path}\\n\\nSaved[${savedData.length}]:\\n${savedData}\\n`)\n }\n\n return savedData\n }\n\n return data\n } catch (e) {\n console.error(e)\n }\n },\n },\n 'node',\n )(path, data.trim(), options)\n}\n\n// biome-ignore lint/suspicious/noTsIgnore: production ready\n// @ts-ignore\ndeclare module '@kubb/fabric-core' {\n interface App {\n write(options?: WriteOptions): Promise<void>\n }\n}\n\ndeclare global {\n namespace Kubb {\n interface App {\n write(options?: WriteOptions): Promise<void>\n }\n }\n}\n\nexport const fsPlugin = createPlugin<Options, ExtendOptions>({\n name: 'fs',\n install(app, options) {\n if (options?.clean) {\n fs.removeSync(options.clean.path)\n }\n\n app.context.events.on('process:progress', async ({ file, source }) => {\n if (options?.onWrite) {\n await options.onWrite(file.path, source)\n }\n await write(file.path, source, { sanity: false })\n })\n },\n inject(app) {\n return {\n async write(\n options = {\n extension: { '.ts': '.ts' },\n dryRun: false,\n },\n ) {\n await app.context.fileManager.write({\n extension: options.extension,\n dryRun: options.dryRun,\n parsers: app.context.installedParsers,\n })\n },\n }\n },\n})\n","import type * as KubbFile from '../KubbFile.ts'\n\ntype BarrelData = {\n file?: KubbFile.File\n path: string\n name: string\n}\n\nexport class TreeNode<TData = unknown> {\n data: TData\n parent?: TreeNode<TData>\n children: Array<TreeNode<TData>> = []\n #cachedLeaves?: Array<TreeNode<TData>>\n\n constructor(data: TData, parent?: TreeNode<TData>) {\n this.data = data\n this.parent = parent\n }\n\n addChild(data: TData): TreeNode<TData> {\n const child = new TreeNode(data, this)\n this.children.push(child)\n this.#cachedLeaves = undefined // invalidate cached leaves\n return child\n }\n\n get leaves(): Array<TreeNode<TData>> {\n if (this.#cachedLeaves) return this.#cachedLeaves\n if (this.children.length === 0) return [this]\n\n const stack: Array<TreeNode<TData>> = [...this.children]\n const result: Array<TreeNode<TData>> = []\n\n for (const node of stack) {\n if (node.children.length) {\n for (const child of node.children) stack.push(child)\n } else {\n result.push(node)\n }\n }\n\n this.#cachedLeaves = result\n return result\n }\n\n forEach(callback: (node: TreeNode<TData>) => void): this {\n const stack: Array<TreeNode<TData>> = [this]\n\n for (const node of stack) {\n callback(node)\n if (node.children.length) {\n for (const child of node.children) stack.push(child)\n }\n }\n return this\n }\n\n findDeep(predicate: (node: TreeNode<TData>) => boolean): TreeNode<TData> | undefined {\n for (const leaf of this.leaves) {\n if (predicate(leaf)) return leaf\n }\n return undefined\n }\n\n static fromFiles(files: Array<KubbFile.File>, rootFolder = ''): TreeNode<BarrelData> | null {\n const normalizePath = (p: string): string => p.replace(/\\\\/g, '/')\n const normalizedRoot = normalizePath(rootFolder)\n const rootPrefix = normalizedRoot.endsWith('/') ? normalizedRoot : `${normalizedRoot}/`\n\n const filteredFiles = files.filter((file) => {\n const filePath = normalizePath(file.path)\n return !filePath.endsWith('.json') && (!rootFolder || filePath.startsWith(rootPrefix))\n })\n\n if (filteredFiles.length === 0) {\n return null\n }\n\n const treeNode = new TreeNode<BarrelData>({\n name: rootFolder || '',\n path: rootFolder || '',\n file: undefined,\n })\n\n for (const file of filteredFiles) {\n const relPath = normalizePath(file.path).slice(rootPrefix.length)\n const parts = relPath.split('/')\n\n let current = treeNode\n let currentPath = rootFolder\n\n for (const [index, part] of parts.entries()) {\n const isLast = index === parts.length - 1\n currentPath += (currentPath.endsWith('/') ? '' : '/') + part\n\n let next: TreeNode<BarrelData> | undefined\n for (const child of current.children) {\n if ((child.data as BarrelData).name === part) {\n next = child\n break\n }\n }\n\n if (!next) {\n next = current.addChild({\n name: part,\n path: currentPath,\n file: isLast ? file : undefined,\n })\n }\n\n current = next\n }\n }\n\n return treeNode\n }\n}\n","/** biome-ignore-all lint/suspicious/useIterableCallbackReturn: not needed */\n\nimport { createPlugin } from './createPlugin.ts'\nimport type * as KubbFile from '../KubbFile.ts'\nimport { TreeNode } from '../utils/TreeNode.ts'\nimport path, { resolve } from 'node:path'\nimport { getRelativePath } from '../utils/getRelativePath.ts'\nimport { createFile } from '../createFile.ts'\n\ntype Mode = 'all' | 'named' | 'propagate' | false\n\ntype Options = {\n root: string\n mode: Mode\n}\n\ntype ExtendOptions = {\n writeEntry(options: Options): Promise<void>\n}\n\n// biome-ignore lint/suspicious/noTsIgnore: production ready\n// @ts-ignore\ndeclare module '@kubb/fabric-core' {\n interface App {\n writeEntry(options: Options): Promise<void>\n }\n}\n\ndeclare global {\n namespace Kubb {\n interface App {\n writeEntry(options: Options): Promise<void>\n }\n }\n}\n\ntype GetBarrelFilesOptions = {\n files: KubbFile.File[]\n root: string\n mode: Mode\n}\n\nexport function getBarrelFiles({ files, root, mode }: GetBarrelFilesOptions): Array<KubbFile.File> {\n // Do not generate when propagating or disabled\n if (mode === 'propagate' || mode === false) {\n return []\n }\n\n const cachedFiles = new Map<KubbFile.Path, KubbFile.File>()\n const dedupe = new Map<KubbFile.Path, Set<string>>()\n\n const tree = TreeNode.fromFiles(files, root)\n tree?.forEach((node) => {\n // Only create a barrel for directory-like nodes that have a parent with a path\n if (!node?.children || !node.parent?.data.path) {\n return\n }\n\n const parentPath = node.parent.data.path as KubbFile.Path\n const barrelPath = path.join(parentPath, 'index.ts') as KubbFile.Path\n\n let barrelFile = cachedFiles.get(barrelPath)\n if (!barrelFile) {\n barrelFile = createFile({\n path: barrelPath,\n baseName: 'index.ts',\n exports: [],\n sources: [],\n })\n cachedFiles.set(barrelPath, barrelFile)\n dedupe.set(barrelPath, new Set<string>())\n }\n\n const seen = dedupe.get(barrelPath)!\n\n // Collect all leaves under the current directory node\n node.leaves.forEach((leaf) => {\n const file = leaf.data.file\n if (!file) {\n return\n }\n\n const sources = file.sources || []\n sources.forEach((source) => {\n if (!file.path || !source.isIndexable || !source.name) {\n return\n }\n\n const key = `${source.name}|${source.isTypeOnly ? '1' : '0'}`\n if (seen.has(key)) {\n return\n }\n seen.add(key)\n\n // Always compute relative path from the parent directory to the file path\n barrelFile!.exports!.push({\n name: [source.name],\n path: getRelativePath(parentPath, file.path),\n isTypeOnly: source.isTypeOnly,\n })\n\n barrelFile!.sources.push({\n name: source.name,\n isTypeOnly: source.isTypeOnly,\n value: '', // TODO use parser to generate import\n isExportable: mode === 'all' || mode === 'named',\n isIndexable: mode === 'all' || mode === 'named',\n })\n })\n })\n })\n\n const result = [...cachedFiles.values()]\n\n if (mode === 'all') {\n return result.map((file) => ({\n ...file,\n exports: file.exports?.map((e) => ({ ...e, name: undefined })),\n }))\n }\n\n return result\n}\n\nexport const barrelPlugin = createPlugin<Options, ExtendOptions>({\n name: 'barrel',\n install(app, options) {\n if (!options) {\n throw new Error('Barrel plugin requires options.root and options.mode')\n }\n\n if (!options.mode) {\n return undefined\n }\n\n app.context.events.onOnce('process:end', async ({ files }) => {\n const root = options.root\n const barrelFiles = getBarrelFiles({ files, root, mode: options.mode })\n\n await app.context.fileManager.add(...barrelFiles)\n\n await app.context.fileManager.write({\n parsers: app.context.installedParsers,\n })\n })\n },\n inject(app) {\n return {\n async writeEntry({ root, mode }) {\n if (!mode || mode === 'propagate') {\n return undefined\n }\n\n const rootPath = resolve(root, 'index.ts')\n\n const barrelFiles = app.files.filter((file) => {\n return file.sources.some((source) => source.isIndexable)\n })\n\n const rootFile = createFile({\n path: rootPath,\n baseName: 'index.ts',\n exports: barrelFiles\n .flatMap((file) => {\n const containsOnlyTypes = file.sources.every((source) => source.isTypeOnly)\n\n return file.sources\n ?.map((source) => {\n if (!file.path || !source.isIndexable) {\n return undefined\n }\n\n return {\n name: mode === 'all' ? undefined : [source.name],\n path: getRelativePath(rootPath, file.path),\n isTypeOnly: mode === 'all' ? containsOnlyTypes : source.isTypeOnly,\n } as KubbFile.Export\n })\n .filter(Boolean)\n })\n .filter(Boolean),\n sources: [],\n })\n\n await app.context.fileManager.add(rootFile)\n },\n }\n },\n})\n"],"mappings":";;;;;;;;AAEA,SAAgB,aACd,QACgC;AAChC,QAAO;EACL,MAAM;EACN,GAAG;EACJ;;;;;ACkBH,eAAsB,MAAM,QAAc,MAAc,UAAgC,EAAE,EAA+B;AACvH,KAAI,KAAK,MAAM,KAAK,GAClB;AAEF,QAAO,SACL;EACE,MAAM,OAAO,QAAc,QAAc,EAAE,aAAmC;AAC5E,OAAI;IACF,MAAM,aAAa,MAAM,GAAG,SAAS,QAAQA,OAAK,EAAE,EAClD,UAAU,SACX,CAAC;AACF,iEAAI,WAAY,UAAU,uDAAKC,OAAM,UAAU,EAC7C;YAEK,MAAM;AAIf,SAAM,GAAG,WAAW,QAAQD,OAAK,EAAEC,QAAM,EAAE,UAAU,SAAS,CAAC;AAE/D,OAAI,QAAQ;IACV,MAAM,YAAY,MAAM,GAAG,SAAS,QAAQD,OAAK,EAAE,EACjD,UAAU,SACX,CAAC;AAEF,+DAAI,UAAW,UAAU,uDAAKC,OAAM,UAAU,EAC5C,OAAM,IAAI,MAAM,2BAA2BD,OAAK,WAAWC,OAAK,OAAO,MAAMA,OAAK,YAAY,UAAU,OAAO,MAAM,UAAU,IAAI;AAGrI,WAAO;;AAGT,UAAOA;;EAET,KAAK,OAAO,QAAc,QAAc,EAAE,aAAmC;AAC3E,OAAI;AACF,UAAM,IAAI,MAAM,QAAQD,OAAK,EAAEC,OAAK;AAEpC,QAAI,QAAQ;KAEV,MAAM,YAAY,MADL,IAAI,KAAK,QAAQD,OAAK,CAAC,CACP,MA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;;KACjB,MAAM,oBAAoB,KAAK,QAAQ,OAAO,WAAW,OAAO,WAAW;AAE3E,6BAAO,KAAK,uEACR,KAAK,WAAW;AAChB,UAAI,CAAC,KAAK,QAAQ,CAAC,OAAO,YACxB;AAGF,aAAO;OACL,MAAM,SAAS,QAAQ,SAAY,CAAC,OAAO,KAAK;OAChD,MAAM,gBAAgB,UAAU,KAAK,KAAK;OAC1C,YAAY,SAAS,QAAQ,oBAAoB,OAAO;OACzD;OACD,CACD,OAAO,QAAQ;MAClB,CACD,OAAO,QAAQ;IAClB,SAAS,EAAE;IACZ,CAAC;AAEF,SAAM,IAAI,QAAQ,YAAY,IAAI,SAAS;KAE9C;;CAEJ,CAAC"}
|
|
@@ -22,9 +22,22 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
22
22
|
|
|
23
23
|
//#endregion
|
|
24
24
|
|
|
25
|
+
//#region src/utils/trimExtName.ts
|
|
26
|
+
function trimExtName(text) {
|
|
27
|
+
return text.replace(/\.[^/.]+$/, "");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
//#endregion
|
|
25
31
|
Object.defineProperty(exports, '__toESM', {
|
|
26
32
|
enumerable: true,
|
|
27
33
|
get: function () {
|
|
28
34
|
return __toESM;
|
|
29
35
|
}
|
|
30
|
-
});
|
|
36
|
+
});
|
|
37
|
+
Object.defineProperty(exports, 'trimExtName', {
|
|
38
|
+
enumerable: true,
|
|
39
|
+
get: function () {
|
|
40
|
+
return trimExtName;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
//# sourceMappingURL=trimExtName-Bb4zGVF1.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trimExtName-Bb4zGVF1.cjs","names":[],"sources":["../src/utils/trimExtName.ts"],"sourcesContent":["export function trimExtName(text: string): string {\n return text.replace(/\\.[^/.]+$/, '')\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,SAAgB,YAAY,MAAsB;AAChD,QAAO,KAAK,QAAQ,aAAa,GAAG"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trimExtName-CeOVQVbu.js","names":[],"sources":["../src/utils/trimExtName.ts"],"sourcesContent":["export function trimExtName(text: string): string {\n return text.replace(/\\.[^/.]+$/, '')\n}\n"],"mappings":";AAAA,SAAgB,YAAY,MAAsB;AAChD,QAAO,KAAK,QAAQ,aAAa,GAAG"}
|
package/dist/types.d.cts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { f as KubbFile_d_exports, n as AppContext, t as App } from "./App-
|
|
2
|
-
import { r as DefineApp } from "./index-
|
|
1
|
+
import { f as KubbFile_d_exports, n as AppContext, t as App } from "./App-Cplfh8QA.cjs";
|
|
2
|
+
import { r as DefineApp } from "./index-C3GyFwE1.cjs";
|
|
3
3
|
export { type App, type AppContext, type DefineApp, KubbFile_d_exports as KubbFile };
|
package/dist/types.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { f as KubbFile_d_exports, n as AppContext, t as App } from "./App-
|
|
2
|
-
import { r as DefineApp } from "./index-
|
|
1
|
+
import { f as KubbFile_d_exports, n as AppContext, t as App } from "./App-Dvetv2V_.js";
|
|
2
|
+
import { r as DefineApp } from "./index-Agz-2M75.js";
|
|
3
3
|
export { type App, type AppContext, type DefineApp, KubbFile_d_exports as KubbFile };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { o as Parser } from "./App-
|
|
1
|
+
import { o as Parser } from "./App-Dvetv2V_.js";
|
|
2
2
|
import ts from "typescript";
|
|
3
3
|
|
|
4
4
|
//#region src/parsers/typescriptParser.d.ts
|
|
@@ -47,4 +47,4 @@ declare function createExport({
|
|
|
47
47
|
declare const typescriptParser: Parser<[], any>;
|
|
48
48
|
//#endregion
|
|
49
49
|
export { typescriptParser as i, createImport as n, print as r, createExport as t };
|
|
50
|
-
//# sourceMappingURL=typescriptParser-
|
|
50
|
+
//# sourceMappingURL=typescriptParser-B49WHoGL.d.ts.map
|
|
@@ -1,25 +1,11 @@
|
|
|
1
|
-
const
|
|
2
|
-
const require_createParser = require('./createParser-
|
|
1
|
+
const require_trimExtName = require('./trimExtName-Bb4zGVF1.cjs');
|
|
2
|
+
const require_createParser = require('./createParser-C4IkyTs5.cjs');
|
|
3
|
+
const require_getRelativePath = require('./getRelativePath-C6lvNCs7.cjs');
|
|
3
4
|
let node_path = require("node:path");
|
|
4
|
-
node_path =
|
|
5
|
+
node_path = require_trimExtName.__toESM(node_path);
|
|
5
6
|
let typescript = require("typescript");
|
|
6
|
-
typescript =
|
|
7
|
+
typescript = require_trimExtName.__toESM(typescript);
|
|
7
8
|
|
|
8
|
-
//#region src/utils/getRelativePath.ts
|
|
9
|
-
function slash(path$1, platform = "linux") {
|
|
10
|
-
const isWindowsPath = /^\\\\\?\\/.test(path$1);
|
|
11
|
-
const normalizedPath = (0, node_path.normalize)(path$1);
|
|
12
|
-
if (["linux", "mac"].includes(platform) && !isWindowsPath) return normalizedPath.replaceAll(/\\/g, "/").replace("../", "");
|
|
13
|
-
return normalizedPath.replaceAll(/\\/g, "/").replace("../", "");
|
|
14
|
-
}
|
|
15
|
-
function getRelativePath(rootDir, filePath, platform = "linux") {
|
|
16
|
-
if (!rootDir || !filePath) throw new Error(`Root and file should be filled in when retrieving the relativePath, ${rootDir || ""} ${filePath || ""}`);
|
|
17
|
-
const slashedPath = slash((0, node_path.relative)(rootDir, filePath), platform);
|
|
18
|
-
if (slashedPath.startsWith("../")) return slashedPath;
|
|
19
|
-
return `./${slashedPath}`;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
//#endregion
|
|
23
9
|
//#region src/parsers/typescriptParser.ts
|
|
24
10
|
const { factory } = typescript.default;
|
|
25
11
|
/**
|
|
@@ -54,7 +40,7 @@ function print(elements = [], { source = "", baseName = "print.tsx", scriptKind
|
|
|
54
40
|
return restoreNewLines(output).replace(/\r\n/g, "\n");
|
|
55
41
|
}
|
|
56
42
|
function createImport({ name, path: path$1, root, isTypeOnly = false, isNameSpace = false }) {
|
|
57
|
-
const resolvePath = root ? getRelativePath(root, path$1) : path$1;
|
|
43
|
+
const resolvePath = root ? require_getRelativePath.getRelativePath(root, path$1) : path$1;
|
|
58
44
|
if (!Array.isArray(name)) {
|
|
59
45
|
let importPropertyName = factory.createIdentifier(name);
|
|
60
46
|
let importName;
|
|
@@ -90,11 +76,11 @@ const typescriptParser = require_createParser.createParser({
|
|
|
90
76
|
async parse(file, options = { extname: ".ts" }) {
|
|
91
77
|
const source = file.sources.map((item) => item.value).join("\n\n");
|
|
92
78
|
const importNodes = file.imports.map((item) => {
|
|
93
|
-
const importPath = item.root ? getRelativePath(item.root, item.path) : item.path;
|
|
79
|
+
const importPath = item.root ? require_getRelativePath.getRelativePath(item.root, item.path) : item.path;
|
|
94
80
|
const hasExtname = !!node_path.default.extname(importPath);
|
|
95
81
|
return createImport({
|
|
96
82
|
name: item.name,
|
|
97
|
-
path: options.extname && hasExtname ? `${
|
|
83
|
+
path: options.extname && hasExtname ? `${require_trimExtName.trimExtName(importPath)}${options.extname}` : item.root ? require_trimExtName.trimExtName(importPath) : importPath,
|
|
98
84
|
isTypeOnly: item.isTypeOnly
|
|
99
85
|
});
|
|
100
86
|
}).filter(Boolean);
|
|
@@ -103,7 +89,7 @@ const typescriptParser = require_createParser.createParser({
|
|
|
103
89
|
const hasExtname = !!node_path.default.extname(exportPath);
|
|
104
90
|
return createExport({
|
|
105
91
|
name: item.name,
|
|
106
|
-
path: options.extname && hasExtname ? `${
|
|
92
|
+
path: options.extname && hasExtname ? `${require_trimExtName.trimExtName(item.path)}${options.extname}` : require_trimExtName.trimExtName(item.path),
|
|
107
93
|
isTypeOnly: item.isTypeOnly,
|
|
108
94
|
asAlias: item.asAlias
|
|
109
95
|
});
|
|
@@ -142,4 +128,4 @@ Object.defineProperty(exports, 'typescriptParser', {
|
|
|
142
128
|
return typescriptParser;
|
|
143
129
|
}
|
|
144
130
|
});
|
|
145
|
-
//# sourceMappingURL=typescriptParser-
|
|
131
|
+
//# sourceMappingURL=typescriptParser-CNHO6H2_.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typescriptParser-CNHO6H2_.cjs","names":["ts","output: string","getRelativePath","path","importPropertyName: ts.Identifier | undefined","importName: ts.NamedImportBindings | undefined","createParser","trimExtName"],"sources":["../src/parsers/typescriptParser.ts"],"sourcesContent":["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":";;;;;;;;;AAMA,MAAM,EAAE,YAAYA;;;;AAWpB,MAAM,kBAAkB,SAAiB,KAAK,QAAQ,SAAS,oBAAoB;;;;AAKnF,MAAM,mBAAmB,SAAiB,KAAK,QAAQ,wBAAwB,KAAK;;;;;;AAOpF,SAAgB,MAAM,WAA2B,EAAE,EAAE,EAAE,SAAS,IAAI,WAAW,aAAa,aAAaA,mBAAG,WAAW,QAAsB,EAAE,EAAU;CACvJ,MAAM,aAAaA,mBAAG,iBAAiB,UAAU,eAAe,OAAO,EAAEA,mBAAG,aAAa,QAAQ,MAAM,WAAW;CAElH,MAAM,UAAUA,mBAAG,cAAc;EAC/B,uBAAuB;EACvB,SAASA,mBAAG,YAAY;EACxB,gBAAgB;EAChB,eAAe;EAChB,CAAC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AC,CAACJ,kBAAK,QAAQ,WAAW;AAE7C,UAAO,aAAa;IAClB,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;IACF,CACD,OAAO,QAAQ;AAElB,SAAO;GAAC,KAAK;GAAQ,MAAM,CAAC,GAAG,aAAa,GAAG,YAAY,CAAC;GAAE;GAAQ,KAAK;GAAO,CAAC,KAAK,KAAK;;CAEhG,CAAC"}
|
|
@@ -1,22 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
1
|
+
import { t as trimExtName } from "./trimExtName-CeOVQVbu.js";
|
|
2
|
+
import { t as createParser } from "./createParser-17uGjfu3.js";
|
|
3
|
+
import { t as getRelativePath } from "./getRelativePath-CERJmYkp.js";
|
|
4
|
+
import path from "node:path";
|
|
3
5
|
import ts from "typescript";
|
|
4
6
|
|
|
5
|
-
//#region src/utils/getRelativePath.ts
|
|
6
|
-
function slash(path$1, platform = "linux") {
|
|
7
|
-
const isWindowsPath = /^\\\\\?\\/.test(path$1);
|
|
8
|
-
const normalizedPath = normalize(path$1);
|
|
9
|
-
if (["linux", "mac"].includes(platform) && !isWindowsPath) return normalizedPath.replaceAll(/\\/g, "/").replace("../", "");
|
|
10
|
-
return normalizedPath.replaceAll(/\\/g, "/").replace("../", "");
|
|
11
|
-
}
|
|
12
|
-
function getRelativePath(rootDir, filePath, platform = "linux") {
|
|
13
|
-
if (!rootDir || !filePath) throw new Error(`Root and file should be filled in when retrieving the relativePath, ${rootDir || ""} ${filePath || ""}`);
|
|
14
|
-
const slashedPath = slash(relative(rootDir, filePath), platform);
|
|
15
|
-
if (slashedPath.startsWith("../")) return slashedPath;
|
|
16
|
-
return `./${slashedPath}`;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
//#endregion
|
|
20
7
|
//#region src/parsers/typescriptParser.ts
|
|
21
8
|
const { factory } = ts;
|
|
22
9
|
/**
|
|
@@ -116,4 +103,4 @@ const typescriptParser = createParser({
|
|
|
116
103
|
|
|
117
104
|
//#endregion
|
|
118
105
|
export { typescriptParser as i, createImport as n, print as r, createExport as t };
|
|
119
|
-
//# sourceMappingURL=typescriptParser-
|
|
106
|
+
//# sourceMappingURL=typescriptParser-CWT7zCJy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typescriptParser-CWT7zCJy.js","names":["output: string","path","importPropertyName: ts.Identifier | undefined","importName: ts.NamedImportBindings | undefined"],"sources":["../src/parsers/typescriptParser.ts"],"sourcesContent":["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":";;;;;;;AAMA,MAAM,EAAE,YAAY;;;;AAWpB,MAAM,kBAAkB,SAAiB,KAAK,QAAQ,SAAS,oBAAoB;;;;AAKnF,MAAM,mBAAmB,SAAiB,KAAK,QAAQ,wBAAwB,KAAK;;;;;;AAOpF,SAAgB,MAAM,WAA2B,EAAE,EAAE,EAAE,SAAS,IAAI,WAAW,aAAa,aAAa,GAAG,WAAW,QAAsB,EAAE,EAAU;CACvJ,MAAM,aAAa,GAAG,iBAAiB,UAAU,eAAe,OAAO,EAAE,GAAG,aAAa,QAAQ,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,CACD,OAAO,QAAQ;EAElB,MAAM,cAAc,KAAK,QACtB,KAAK,SAAS;GACb,MAAM,aAAa,KAAK;GAExB,MAAM,aAAa,CAAC,CAAC,KAAK,QAAQ,WAAW;AAE7C,UAAO,aAAa;IAClB,MAAM,KAAK;IACX,MAAM,QAAQ,WAAW,aAAa,GAAG,YAAY,KAAK,KAAK,GAAG,QAAQ,YAAY,YAAY,KAAK,KAAK;IAC5G,YAAY,KAAK;IACjB,SAAS,KAAK;IACf,CAAC;IACF,CACD,OAAO,QAAQ;AAElB,SAAO;GAAC,KAAK;GAAQ,MAAM,CAAC,GAAG,aAAa,GAAG,YAAY,CAAC;GAAE;GAAQ,KAAK;GAAO,CAAC,KAAK,KAAK;;CAEhG,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { o as Parser } from "./App-
|
|
1
|
+
import { o as Parser } from "./App-Cplfh8QA.cjs";
|
|
2
2
|
import ts from "typescript";
|
|
3
3
|
|
|
4
4
|
//#region src/parsers/typescriptParser.d.ts
|
|
@@ -47,4 +47,4 @@ declare function createExport({
|
|
|
47
47
|
declare const typescriptParser: Parser<[], any>;
|
|
48
48
|
//#endregion
|
|
49
49
|
export { typescriptParser as i, createImport as n, print as r, createExport as t };
|
|
50
|
-
//# sourceMappingURL=typescriptParser-
|
|
50
|
+
//# sourceMappingURL=typescriptParser-CxGhFQXh.d.cts.map
|
package/package.json
CHANGED
package/src/App.ts
CHANGED
|
@@ -68,36 +68,30 @@ export type AppContext<TOptions = unknown> = {
|
|
|
68
68
|
installedParsers: Set<Parser>
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
|
|
72
|
-
? (app: App, ...options: TOptions) => void
|
|
73
|
-
: TOptions extends object
|
|
74
|
-
? (app: App, options?: TOptions) => void
|
|
75
|
-
: (app: App) => void
|
|
71
|
+
type AllOptional<T> = {} extends T ? true : false
|
|
76
72
|
|
|
77
|
-
export type
|
|
73
|
+
export type Install<TOptions = unknown> = TOptions extends any[]
|
|
74
|
+
? (app: App, ...options: TOptions) => void | Promise<void>
|
|
75
|
+
: AllOptional<TOptions> extends true
|
|
76
|
+
? (app: App, options: TOptions | undefined) => void | Promise<void>
|
|
77
|
+
: (app: App, options: TOptions) => void | Promise<void>
|
|
78
|
+
|
|
79
|
+
export type Inject<TOptions = unknown, TAppExtension extends Record<string, any> = {}> = TOptions extends any[]
|
|
78
80
|
? (app: App, ...options: TOptions) => Partial<TAppExtension>
|
|
79
|
-
: TOptions extends
|
|
80
|
-
? (app: App, options
|
|
81
|
-
: (app: App) => Partial<TAppExtension>
|
|
81
|
+
: AllOptional<TOptions> extends true
|
|
82
|
+
? (app: App, options: TOptions | undefined) => Partial<TAppExtension>
|
|
83
|
+
: (app: App, options: TOptions) => Partial<TAppExtension>
|
|
82
84
|
|
|
83
85
|
export interface App<TOptions = unknown> extends Kubb.App {
|
|
84
86
|
context: AppContext<TOptions>
|
|
85
87
|
files: Array<KubbFile.ResolvedFile>
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
): this & TAppExtension
|
|
94
|
-
// async
|
|
95
|
-
use<TOptions extends any[] | object = any, TMeta extends object = object, TAppExtension extends Record<string, any> = {}>(
|
|
96
|
-
pluginOrParser: Plugin<TOptions, TAppExtension> | Parser<TOptions, TMeta>,
|
|
97
|
-
...options: TOptions extends any[] ? NoInfer<TOptions> : [NoInfer<TOptions>]
|
|
98
|
-
): Promise<this & TAppExtension>
|
|
99
|
-
use<TOptions extends any[] | object = any, TMeta extends object = object, TAppExtension extends Record<string, any> = {}>(
|
|
100
|
-
pluginOrParser: Plugin<TOptions, TAppExtension> | Parser<TOptions, TMeta>,
|
|
101
|
-
): Promise<this & TAppExtension>
|
|
88
|
+
use<TPluginOptions = unknown, TMeta extends object = object, TAppExtension extends Record<string, any> = {}>(
|
|
89
|
+
pluginOrParser: Plugin<TPluginOptions, TAppExtension> | Parser<TPluginOptions, TMeta>,
|
|
90
|
+
...options: TPluginOptions extends any[]
|
|
91
|
+
? NoInfer<TPluginOptions>
|
|
92
|
+
: AllOptional<TPluginOptions> extends true
|
|
93
|
+
? [NoInfer<TPluginOptions>?] // Optional when all props are optional
|
|
94
|
+
: [NoInfer<TPluginOptions>] // Required otherwise
|
|
95
|
+
): (this & TAppExtension) | Promise<this & TAppExtension>
|
|
102
96
|
addFile(...files: Array<KubbFile.File>): Promise<void>
|
|
103
97
|
}
|
package/src/FileManager.ts
CHANGED
|
@@ -22,10 +22,13 @@ type Options = {
|
|
|
22
22
|
|
|
23
23
|
export class FileManager {
|
|
24
24
|
#cache = new Cache<KubbFile.ResolvedFile>()
|
|
25
|
+
events: AsyncEventEmitter<AppEvents>
|
|
25
26
|
processor: FileProcessor
|
|
26
27
|
|
|
27
28
|
constructor({ events = new AsyncEventEmitter<AppEvents>() }: Options = {}) {
|
|
28
29
|
this.processor = new FileProcessor({ events })
|
|
30
|
+
|
|
31
|
+
this.events = events
|
|
29
32
|
return this
|
|
30
33
|
}
|
|
31
34
|
|
|
@@ -86,6 +89,10 @@ export class FileManager {
|
|
|
86
89
|
}
|
|
87
90
|
|
|
88
91
|
async write(options: ProcessFilesProps): Promise<KubbFile.ResolvedFile[]> {
|
|
89
|
-
|
|
92
|
+
const resolvedFiles = await this.processor.run(this.files, options)
|
|
93
|
+
|
|
94
|
+
this.clear()
|
|
95
|
+
|
|
96
|
+
return resolvedFiles
|
|
90
97
|
}
|
|
91
98
|
}
|
package/src/FileProcessor.ts
CHANGED
|
@@ -31,9 +31,15 @@ export class FileProcessor {
|
|
|
31
31
|
return this
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
async parse(file: KubbFile.ResolvedFile, { parsers
|
|
34
|
+
async parse(file: KubbFile.ResolvedFile, { parsers, extension }: GetParseOptions = {}): Promise<string> {
|
|
35
35
|
const parseExtName = extension?.[file.extname] || undefined
|
|
36
36
|
|
|
37
|
+
if (!parsers) {
|
|
38
|
+
console.warn('No parsers provided, using default parser. If you want to use a specific parser, please provide it in the options.')
|
|
39
|
+
|
|
40
|
+
return defaultParser.parse(file, { extname: parseExtName })
|
|
41
|
+
}
|
|
42
|
+
|
|
37
43
|
if (!file.extname) {
|
|
38
44
|
return defaultParser.parse(file, { extname: parseExtName })
|
|
39
45
|
}
|
package/src/defineApp.ts
CHANGED
|
@@ -14,8 +14,8 @@ export type DefineApp<TOptions> = (rootComponent?: Component, options?: TOptions
|
|
|
14
14
|
export function defineApp<TOptions = unknown>(instance?: RootRenderFunction<App<TOptions>>): DefineApp<TOptions> {
|
|
15
15
|
function createApp(options?: TOptions): App {
|
|
16
16
|
const events = new AsyncEventEmitter<AppEvents>()
|
|
17
|
-
const installedPlugins = new Set<Plugin
|
|
18
|
-
const installedParsers = new Set<Parser
|
|
17
|
+
const installedPlugins = new Set<Plugin<any>>()
|
|
18
|
+
const installedParsers = new Set<Parser<any>>()
|
|
19
19
|
const fileManager = new FileManager({ events })
|
|
20
20
|
const context = {
|
|
21
21
|
events,
|
|
@@ -68,15 +68,10 @@ export function defineApp<TOptions = unknown>(instance?: RootRenderFunction<App<
|
|
|
68
68
|
},
|
|
69
69
|
} as App<TOptions>
|
|
70
70
|
|
|
71
|
-
// start
|
|
72
|
-
events.emit('start')
|
|
73
71
|
if (instance) {
|
|
74
72
|
instance(app)
|
|
75
73
|
}
|
|
76
74
|
|
|
77
|
-
// end
|
|
78
|
-
events.emit('end')
|
|
79
|
-
|
|
80
75
|
return app
|
|
81
76
|
}
|
|
82
77
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Parser, UserParser } from './types.ts'
|
|
2
2
|
|
|
3
|
-
export function createParser<TOptions =
|
|
3
|
+
export function createParser<TOptions = unknown, TMeta extends object = any>(parser: UserParser<TOptions, TMeta>): Parser<TOptions, TMeta> {
|
|
4
4
|
return {
|
|
5
5
|
type: 'parser',
|
|
6
6
|
...parser,
|
package/src/parsers/types.ts
CHANGED
|
@@ -5,7 +5,7 @@ type PrintOptions = {
|
|
|
5
5
|
extname?: KubbFile.Extname
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
export type Parser<TOptions =
|
|
8
|
+
export type Parser<TOptions = unknown, TMeta extends object = any> = {
|
|
9
9
|
name: string
|
|
10
10
|
type: 'parser'
|
|
11
11
|
/**
|
|
@@ -19,4 +19,4 @@ export type Parser<TOptions = any[], TMeta extends object = any> = {
|
|
|
19
19
|
parse(file: KubbFile.ResolvedFile<TMeta>, options: PrintOptions): Promise<string>
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
export type UserParser<TOptions =
|
|
22
|
+
export type UserParser<TOptions = unknown, TMeta extends object = any> = Omit<Parser<TOptions, TMeta>, 'type'>
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/** biome-ignore-all lint/suspicious/useIterableCallbackReturn: not needed */
|
|
2
|
+
|
|
3
|
+
import { createPlugin } from './createPlugin.ts'
|
|
4
|
+
import type * as KubbFile from '../KubbFile.ts'
|
|
5
|
+
import { TreeNode } from '../utils/TreeNode.ts'
|
|
6
|
+
import path, { resolve } from 'node:path'
|
|
7
|
+
import { getRelativePath } from '../utils/getRelativePath.ts'
|
|
8
|
+
import { createFile } from '../createFile.ts'
|
|
9
|
+
|
|
10
|
+
type Mode = 'all' | 'named' | 'propagate' | false
|
|
11
|
+
|
|
12
|
+
type Options = {
|
|
13
|
+
root: string
|
|
14
|
+
mode: Mode
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type ExtendOptions = {
|
|
18
|
+
writeEntry(options: Options): Promise<void>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// biome-ignore lint/suspicious/noTsIgnore: production ready
|
|
22
|
+
// @ts-ignore
|
|
23
|
+
declare module '@kubb/fabric-core' {
|
|
24
|
+
interface App {
|
|
25
|
+
writeEntry(options: Options): Promise<void>
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
declare global {
|
|
30
|
+
namespace Kubb {
|
|
31
|
+
interface App {
|
|
32
|
+
writeEntry(options: Options): Promise<void>
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
type GetBarrelFilesOptions = {
|
|
38
|
+
files: KubbFile.File[]
|
|
39
|
+
root: string
|
|
40
|
+
mode: Mode
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function getBarrelFiles({ files, root, mode }: GetBarrelFilesOptions): Array<KubbFile.File> {
|
|
44
|
+
// Do not generate when propagating or disabled
|
|
45
|
+
if (mode === 'propagate' || mode === false) {
|
|
46
|
+
return []
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const cachedFiles = new Map<KubbFile.Path, KubbFile.File>()
|
|
50
|
+
const dedupe = new Map<KubbFile.Path, Set<string>>()
|
|
51
|
+
|
|
52
|
+
const tree = TreeNode.fromFiles(files, root)
|
|
53
|
+
tree?.forEach((node) => {
|
|
54
|
+
// Only create a barrel for directory-like nodes that have a parent with a path
|
|
55
|
+
if (!node?.children || !node.parent?.data.path) {
|
|
56
|
+
return
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const parentPath = node.parent.data.path as KubbFile.Path
|
|
60
|
+
const barrelPath = path.join(parentPath, 'index.ts') as KubbFile.Path
|
|
61
|
+
|
|
62
|
+
let barrelFile = cachedFiles.get(barrelPath)
|
|
63
|
+
if (!barrelFile) {
|
|
64
|
+
barrelFile = createFile({
|
|
65
|
+
path: barrelPath,
|
|
66
|
+
baseName: 'index.ts',
|
|
67
|
+
exports: [],
|
|
68
|
+
sources: [],
|
|
69
|
+
})
|
|
70
|
+
cachedFiles.set(barrelPath, barrelFile)
|
|
71
|
+
dedupe.set(barrelPath, new Set<string>())
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const seen = dedupe.get(barrelPath)!
|
|
75
|
+
|
|
76
|
+
// Collect all leaves under the current directory node
|
|
77
|
+
node.leaves.forEach((leaf) => {
|
|
78
|
+
const file = leaf.data.file
|
|
79
|
+
if (!file) {
|
|
80
|
+
return
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const sources = file.sources || []
|
|
84
|
+
sources.forEach((source) => {
|
|
85
|
+
if (!file.path || !source.isIndexable || !source.name) {
|
|
86
|
+
return
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const key = `${source.name}|${source.isTypeOnly ? '1' : '0'}`
|
|
90
|
+
if (seen.has(key)) {
|
|
91
|
+
return
|
|
92
|
+
}
|
|
93
|
+
seen.add(key)
|
|
94
|
+
|
|
95
|
+
// Always compute relative path from the parent directory to the file path
|
|
96
|
+
barrelFile!.exports!.push({
|
|
97
|
+
name: [source.name],
|
|
98
|
+
path: getRelativePath(parentPath, file.path),
|
|
99
|
+
isTypeOnly: source.isTypeOnly,
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
barrelFile!.sources.push({
|
|
103
|
+
name: source.name,
|
|
104
|
+
isTypeOnly: source.isTypeOnly,
|
|
105
|
+
value: '', // TODO use parser to generate import
|
|
106
|
+
isExportable: mode === 'all' || mode === 'named',
|
|
107
|
+
isIndexable: mode === 'all' || mode === 'named',
|
|
108
|
+
})
|
|
109
|
+
})
|
|
110
|
+
})
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
const result = [...cachedFiles.values()]
|
|
114
|
+
|
|
115
|
+
if (mode === 'all') {
|
|
116
|
+
return result.map((file) => ({
|
|
117
|
+
...file,
|
|
118
|
+
exports: file.exports?.map((e) => ({ ...e, name: undefined })),
|
|
119
|
+
}))
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return result
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export const barrelPlugin = createPlugin<Options, ExtendOptions>({
|
|
126
|
+
name: 'barrel',
|
|
127
|
+
install(app, options) {
|
|
128
|
+
if (!options) {
|
|
129
|
+
throw new Error('Barrel plugin requires options.root and options.mode')
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (!options.mode) {
|
|
133
|
+
return undefined
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
app.context.events.onOnce('process:end', async ({ files }) => {
|
|
137
|
+
const root = options.root
|
|
138
|
+
const barrelFiles = getBarrelFiles({ files, root, mode: options.mode })
|
|
139
|
+
|
|
140
|
+
await app.context.fileManager.add(...barrelFiles)
|
|
141
|
+
|
|
142
|
+
await app.context.fileManager.write({
|
|
143
|
+
parsers: app.context.installedParsers,
|
|
144
|
+
})
|
|
145
|
+
})
|
|
146
|
+
},
|
|
147
|
+
inject(app) {
|
|
148
|
+
return {
|
|
149
|
+
async writeEntry({ root, mode }) {
|
|
150
|
+
if (!mode || mode === 'propagate') {
|
|
151
|
+
return undefined
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const rootPath = resolve(root, 'index.ts')
|
|
155
|
+
|
|
156
|
+
const barrelFiles = app.files.filter((file) => {
|
|
157
|
+
return file.sources.some((source) => source.isIndexable)
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
const rootFile = createFile({
|
|
161
|
+
path: rootPath,
|
|
162
|
+
baseName: 'index.ts',
|
|
163
|
+
exports: barrelFiles
|
|
164
|
+
.flatMap((file) => {
|
|
165
|
+
const containsOnlyTypes = file.sources.every((source) => source.isTypeOnly)
|
|
166
|
+
|
|
167
|
+
return file.sources
|
|
168
|
+
?.map((source) => {
|
|
169
|
+
if (!file.path || !source.isIndexable) {
|
|
170
|
+
return undefined
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return {
|
|
174
|
+
name: mode === 'all' ? undefined : [source.name],
|
|
175
|
+
path: getRelativePath(rootPath, file.path),
|
|
176
|
+
isTypeOnly: mode === 'all' ? containsOnlyTypes : source.isTypeOnly,
|
|
177
|
+
} as KubbFile.Export
|
|
178
|
+
})
|
|
179
|
+
.filter(Boolean)
|
|
180
|
+
})
|
|
181
|
+
.filter(Boolean),
|
|
182
|
+
sources: [],
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
await app.context.fileManager.add(rootFile)
|
|
186
|
+
},
|
|
187
|
+
}
|
|
188
|
+
},
|
|
189
|
+
})
|