@kubb/fabric-core 0.1.0 → 0.1.2
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-DZuROf6f.d.ts +292 -0
- package/dist/App-zyf9KG3p.d.cts +292 -0
- package/dist/chunk-CUT6urMc.cjs +30 -0
- package/dist/defineApp-D3B0bU-z.d.cts +14 -0
- package/dist/defineApp-DJVMk9lc.d.ts +14 -0
- package/dist/index.cjs +217 -73
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +10 -5
- package/dist/index.d.ts +10 -5
- package/dist/index.js +204 -63
- package/dist/index.js.map +1 -1
- package/dist/parsers/typescript.cjs +5 -5
- package/dist/parsers/typescript.d.cts +3 -51
- package/dist/parsers/typescript.d.ts +3 -51
- package/dist/parsers/typescript.js +2 -2
- package/dist/parsers.cjs +7 -0
- package/dist/parsers.d.cts +14 -0
- package/dist/parsers.d.ts +14 -0
- package/dist/parsers.js +4 -0
- package/dist/plugins.cjs +76 -0
- package/dist/plugins.cjs.map +1 -0
- package/dist/plugins.d.cts +28 -0
- package/dist/plugins.d.ts +28 -0
- package/dist/plugins.js +71 -0
- package/dist/plugins.js.map +1 -0
- package/dist/tsxParser-C741ZKCN.js +26 -0
- package/dist/tsxParser-C741ZKCN.js.map +1 -0
- package/dist/tsxParser-HDf_3TMc.cjs +37 -0
- package/dist/tsxParser-HDf_3TMc.cjs.map +1 -0
- package/dist/types.d.cts +2 -2
- package/dist/types.d.ts +2 -2
- package/dist/{parser-CWB_OBtr.js → typescriptParser-BBGeFKlP.js} +51 -98
- package/dist/typescriptParser-BBGeFKlP.js.map +1 -0
- package/dist/typescriptParser-BBbbmG5W.cjs +171 -0
- package/dist/typescriptParser-BBbbmG5W.cjs.map +1 -0
- package/dist/typescriptParser-C-sBy1iR.d.cts +50 -0
- package/dist/typescriptParser-CtMmz0UV.d.ts +50 -0
- package/package.json +13 -6
- package/src/App.ts +91 -0
- package/src/FileManager.ts +14 -193
- package/src/FileProcessor.ts +89 -0
- package/src/createFile.ts +167 -0
- package/src/defineApp.ts +49 -74
- package/src/index.ts +3 -1
- package/src/parsers/createParser.ts +8 -0
- package/src/parsers/defaultParser.ts +10 -0
- package/src/parsers/index.ts +5 -0
- package/src/parsers/tsxParser.ts +11 -0
- package/src/parsers/types.ts +22 -0
- package/src/parsers/{typescript.ts → typescriptParser.ts} +8 -4
- package/src/plugins/createPlugin.ts +10 -0
- package/src/plugins/fsPlugin.ts +112 -0
- package/src/plugins/index.ts +3 -0
- package/src/plugins/types.ts +15 -0
- package/src/types.ts +4 -1
- package/src/utils/AsyncEventEmitter.ts +37 -0
- package/src/utils/EventEmitter.ts +23 -0
- package/src/utils/getRelativePath.ts +32 -0
- package/src/utils/trimExtName.ts +3 -0
- package/dist/KubbFile-BrN7Wwp6.d.cts +0 -119
- package/dist/KubbFile-BzVkcu9M.d.ts +0 -119
- package/dist/defineApp-Bg7JewJQ.d.ts +0 -62
- package/dist/defineApp-DKW3IRO8.d.cts +0 -62
- package/dist/parser-CWB_OBtr.js.map +0 -1
- package/dist/parser-D64DdV1v.d.cts +0 -21
- package/dist/parser-QF8j8-pj.cjs +0 -260
- package/dist/parser-QF8j8-pj.cjs.map +0 -1
- package/dist/parser-yYqnryUV.d.ts +0 -21
- package/dist/parsers/tsx.cjs +0 -3
- package/dist/parsers/tsx.d.cts +0 -8
- package/dist/parsers/tsx.d.ts +0 -8
- package/dist/parsers/tsx.js +0 -3
- package/src/fs.ts +0 -167
- package/src/parsers/parser.ts +0 -56
- package/src/parsers/tsx.ts +0 -8
package/dist/plugins.cjs
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
const require_chunk = require('./chunk-CUT6urMc.cjs');
|
|
2
|
+
let node_path = require("node:path");
|
|
3
|
+
node_path = require_chunk.__toESM(node_path);
|
|
4
|
+
let js_runtime = require("js-runtime");
|
|
5
|
+
js_runtime = require_chunk.__toESM(js_runtime);
|
|
6
|
+
let fs_extra = require("fs-extra");
|
|
7
|
+
fs_extra = require_chunk.__toESM(fs_extra);
|
|
8
|
+
|
|
9
|
+
//#region src/plugins/createPlugin.ts
|
|
10
|
+
function createPlugin(plugin) {
|
|
11
|
+
return {
|
|
12
|
+
type: "plugin",
|
|
13
|
+
...plugin
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/plugins/fsPlugin.ts
|
|
19
|
+
async function write(path, data, options = {}) {
|
|
20
|
+
if (data.trim() === "") return;
|
|
21
|
+
return (0, js_runtime.switcher)({
|
|
22
|
+
node: async (path$1, data$1, { sanity }) => {
|
|
23
|
+
try {
|
|
24
|
+
const oldContent = await fs_extra.default.readFile((0, node_path.resolve)(path$1), { encoding: "utf-8" });
|
|
25
|
+
if ((oldContent === null || oldContent === void 0 ? void 0 : oldContent.toString()) === (data$1 === null || data$1 === void 0 ? void 0 : data$1.toString())) return;
|
|
26
|
+
} catch (_err) {}
|
|
27
|
+
await fs_extra.default.outputFile((0, node_path.resolve)(path$1), data$1, { encoding: "utf-8" });
|
|
28
|
+
if (sanity) {
|
|
29
|
+
const savedData = await fs_extra.default.readFile((0, node_path.resolve)(path$1), { encoding: "utf-8" });
|
|
30
|
+
if ((savedData === null || savedData === void 0 ? void 0 : savedData.toString()) !== (data$1 === null || data$1 === void 0 ? void 0 : data$1.toString())) throw new Error(`Sanity check failed for ${path$1}\n\nData[${data$1.length}]:\n${data$1}\n\nSaved[${savedData.length}]:\n${savedData}\n`);
|
|
31
|
+
return savedData;
|
|
32
|
+
}
|
|
33
|
+
return data$1;
|
|
34
|
+
},
|
|
35
|
+
bun: async (path$1, data$1, { sanity }) => {
|
|
36
|
+
try {
|
|
37
|
+
await Bun.write((0, node_path.resolve)(path$1), data$1);
|
|
38
|
+
if (sanity) {
|
|
39
|
+
const savedData = await Bun.file((0, node_path.resolve)(path$1)).text();
|
|
40
|
+
if ((savedData === null || savedData === void 0 ? void 0 : savedData.toString()) !== (data$1 === null || data$1 === void 0 ? void 0 : data$1.toString())) throw new Error(`Sanity check failed for ${path$1}\n\nData[${path$1.length}]:\n${path$1}\n\nSaved[${savedData.length}]:\n${savedData}\n`);
|
|
41
|
+
return savedData;
|
|
42
|
+
}
|
|
43
|
+
return data$1;
|
|
44
|
+
} catch (e) {
|
|
45
|
+
console.error(e);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}, "node")(path, data.trim(), options);
|
|
49
|
+
}
|
|
50
|
+
const fsPlugin = createPlugin({
|
|
51
|
+
name: "fs",
|
|
52
|
+
scope: "write",
|
|
53
|
+
async install(_app, context, options) {
|
|
54
|
+
context.events.on("process:progress", async ({ file, source }) => {
|
|
55
|
+
if (options === null || options === void 0 ? void 0 : options.onWrite) await options.onWrite(file.path, source);
|
|
56
|
+
await write(file.path, source, { sanity: false });
|
|
57
|
+
});
|
|
58
|
+
},
|
|
59
|
+
override(_app, context) {
|
|
60
|
+
return { async write(options = {
|
|
61
|
+
extension: { ".ts": ".ts" },
|
|
62
|
+
dryRun: false
|
|
63
|
+
}) {
|
|
64
|
+
await context.fileManager.write({
|
|
65
|
+
extension: options.extension,
|
|
66
|
+
dryRun: options.dryRun,
|
|
67
|
+
parsers: context.installedParsers
|
|
68
|
+
});
|
|
69
|
+
} };
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
//#endregion
|
|
74
|
+
exports.createPlugin = createPlugin;
|
|
75
|
+
exports.fsPlugin = fsPlugin;
|
|
76
|
+
//# sourceMappingURL=plugins.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugins.cjs","names":["fs","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 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}\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\ntype WriteOptions = {\n extension?: Record<KubbFile.Extname, KubbFile.Extname | ''>\n dryRun?: boolean\n}\n\ndeclare module '../index.ts' {\n interface App {\n write(options?: WriteOptions): Promise<void>\n }\n}\n\nexport const fsPlugin = createPlugin<Options, { write(options?: WriteOptions): Promise<void> }>({\n name: 'fs',\n scope: 'write',\n async install(_app, context, options) {\n 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 override(_app, context) {\n return {\n async write(\n options = {\n extension: { '.ts': '.ts' },\n dryRun: false,\n },\n ) {\n await context.fileManager.write({\n extension: options.extension,\n dryRun: options.dryRun,\n parsers: context.installedParsers,\n })\n },\n }\n },\n})\n"],"mappings":";;;;;;;;;AAEA,SAAgB,aACd,QACgC;AAChC,QAAO;EACL,MAAM;EACN,GAAG;EACJ;;;;;ACMH,eAAsB,MAAM,MAAc,MAAc,UAAgC,EAAE,EAA+B;AACvH,KAAI,KAAK,MAAM,KAAK,GAClB;AAEF,iCACE;EACE,MAAM,OAAO,QAAc,QAAc,EAAE,aAAmC;AAC5E,OAAI;IACF,MAAM,aAAa,MAAMA,iBAAG,gCAAiBC,OAAK,EAAE,EAClD,UAAU,SACX,CAAC;AACF,iEAAI,WAAY,UAAU,uDAAKC,OAAM,UAAU,EAC7C;YAEK,MAAM;AAIf,SAAMF,iBAAG,kCAAmBC,OAAK,EAAEC,QAAM,EAAE,UAAU,SAAS,CAAC;AAE/D,OAAI,QAAQ;IACV,MAAM,YAAY,MAAMF,iBAAG,gCAAiBC,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,6BAAcD,OAAK,EAAEC,OAAK;AAEpC,QAAI,QAAQ;KAEV,MAAM,YAAY,MADL,IAAI,4BAAaD,OAAK,CAAC,CACP,MAAM;AAEnC,gEAAI,UAAW,UAAU,uDAAKC,OAAM,UAAU,EAC5C,OAAM,IAAI,MAAM,2BAA2BD,OAAK,WAAWA,OAAK,OAAO,MAAMA,OAAK,YAAY,UAAU,OAAO,MAAM,UAAU,IAAI;AAGrI,YAAO;;AAGT,WAAOC;YACA,GAAG;AACV,YAAQ,MAAM,EAAE;;;EAGrB,EACD,OACD,CAAC,MAAM,KAAK,MAAM,EAAE,QAAQ;;AAc/B,MAAa,WAAW,aAAwE;CAC9F,MAAM;CACN,OAAO;CACP,MAAM,QAAQ,MAAM,SAAS,SAAS;AACpC,UAAQ,OAAO,GAAG,oBAAoB,OAAO,EAAE,MAAM,aAAa;AAChE,yDAAI,QAAS,QACX,OAAM,QAAQ,QAAQ,KAAK,MAAM,OAAO;AAE1C,SAAM,MAAM,KAAK,MAAM,QAAQ,EAAE,QAAQ,OAAO,CAAC;IACjD;;CAEJ,SAAS,MAAM,SAAS;AACtB,SAAO,EACL,MAAM,MACJ,UAAU;GACR,WAAW,EAAE,OAAO,OAAO;GAC3B,QAAQ;GACT,EACD;AACA,SAAM,QAAQ,YAAY,MAAM;IAC9B,WAAW,QAAQ;IACnB,QAAQ,QAAQ;IAChB,SAAS,QAAQ;IAClB,CAAC;KAEL;;CAEJ,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { c as Plugin, l as UserPlugin, u as Extname } from "./App-zyf9KG3p.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/plugins/createPlugin.d.ts
|
|
4
|
+
declare function createPlugin<Options$1 = any[], TAppExtension extends Record<string, any> = {}>(plugin: UserPlugin<Options$1, TAppExtension>): Plugin<Options$1, TAppExtension>;
|
|
5
|
+
//#endregion
|
|
6
|
+
//#region src/plugins/fsPlugin.d.ts
|
|
7
|
+
type Options = {
|
|
8
|
+
/**
|
|
9
|
+
* Optional callback that is invoked whenever a file is written by the plugin.
|
|
10
|
+
* Useful for tests to observe write operations without spying on internal functions.
|
|
11
|
+
*/
|
|
12
|
+
onWrite?: (path: string, data: string) => void | Promise<void>;
|
|
13
|
+
};
|
|
14
|
+
type WriteOptions = {
|
|
15
|
+
extension?: Record<Extname, Extname | ''>;
|
|
16
|
+
dryRun?: boolean;
|
|
17
|
+
};
|
|
18
|
+
declare module '../index.ts' {
|
|
19
|
+
interface App {
|
|
20
|
+
write(options?: WriteOptions): Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
declare const fsPlugin: Plugin<Options, {
|
|
24
|
+
write(options?: WriteOptions): Promise<void>;
|
|
25
|
+
}>;
|
|
26
|
+
//#endregion
|
|
27
|
+
export { createPlugin, fsPlugin };
|
|
28
|
+
//# sourceMappingURL=plugins.d.cts.map
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { c as Plugin, l as UserPlugin, u as Extname } from "./App-DZuROf6f.js";
|
|
2
|
+
|
|
3
|
+
//#region src/plugins/createPlugin.d.ts
|
|
4
|
+
declare function createPlugin<Options$1 = any[], TAppExtension extends Record<string, any> = {}>(plugin: UserPlugin<Options$1, TAppExtension>): Plugin<Options$1, TAppExtension>;
|
|
5
|
+
//#endregion
|
|
6
|
+
//#region src/plugins/fsPlugin.d.ts
|
|
7
|
+
type Options = {
|
|
8
|
+
/**
|
|
9
|
+
* Optional callback that is invoked whenever a file is written by the plugin.
|
|
10
|
+
* Useful for tests to observe write operations without spying on internal functions.
|
|
11
|
+
*/
|
|
12
|
+
onWrite?: (path: string, data: string) => void | Promise<void>;
|
|
13
|
+
};
|
|
14
|
+
type WriteOptions = {
|
|
15
|
+
extension?: Record<Extname, Extname | ''>;
|
|
16
|
+
dryRun?: boolean;
|
|
17
|
+
};
|
|
18
|
+
declare module '../index.ts' {
|
|
19
|
+
interface App {
|
|
20
|
+
write(options?: WriteOptions): Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
declare const fsPlugin: Plugin<Options, {
|
|
24
|
+
write(options?: WriteOptions): Promise<void>;
|
|
25
|
+
}>;
|
|
26
|
+
//#endregion
|
|
27
|
+
export { createPlugin, fsPlugin };
|
|
28
|
+
//# sourceMappingURL=plugins.d.ts.map
|
package/dist/plugins.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
import { switcher } from "js-runtime";
|
|
3
|
+
import fs from "fs-extra";
|
|
4
|
+
|
|
5
|
+
//#region src/plugins/createPlugin.ts
|
|
6
|
+
function createPlugin(plugin) {
|
|
7
|
+
return {
|
|
8
|
+
type: "plugin",
|
|
9
|
+
...plugin
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/plugins/fsPlugin.ts
|
|
15
|
+
async function write(path$1, data, options = {}) {
|
|
16
|
+
if (data.trim() === "") return;
|
|
17
|
+
return switcher({
|
|
18
|
+
node: async (path$2, data$1, { sanity }) => {
|
|
19
|
+
try {
|
|
20
|
+
const oldContent = await fs.readFile(resolve(path$2), { encoding: "utf-8" });
|
|
21
|
+
if ((oldContent === null || oldContent === void 0 ? void 0 : oldContent.toString()) === (data$1 === null || data$1 === void 0 ? void 0 : data$1.toString())) return;
|
|
22
|
+
} catch (_err) {}
|
|
23
|
+
await fs.outputFile(resolve(path$2), data$1, { encoding: "utf-8" });
|
|
24
|
+
if (sanity) {
|
|
25
|
+
const savedData = await fs.readFile(resolve(path$2), { encoding: "utf-8" });
|
|
26
|
+
if ((savedData === null || savedData === void 0 ? void 0 : savedData.toString()) !== (data$1 === null || data$1 === void 0 ? void 0 : data$1.toString())) throw new Error(`Sanity check failed for ${path$2}\n\nData[${data$1.length}]:\n${data$1}\n\nSaved[${savedData.length}]:\n${savedData}\n`);
|
|
27
|
+
return savedData;
|
|
28
|
+
}
|
|
29
|
+
return data$1;
|
|
30
|
+
},
|
|
31
|
+
bun: async (path$2, data$1, { sanity }) => {
|
|
32
|
+
try {
|
|
33
|
+
await Bun.write(resolve(path$2), data$1);
|
|
34
|
+
if (sanity) {
|
|
35
|
+
const savedData = await Bun.file(resolve(path$2)).text();
|
|
36
|
+
if ((savedData === null || savedData === void 0 ? void 0 : savedData.toString()) !== (data$1 === null || data$1 === void 0 ? void 0 : data$1.toString())) throw new Error(`Sanity check failed for ${path$2}\n\nData[${path$2.length}]:\n${path$2}\n\nSaved[${savedData.length}]:\n${savedData}\n`);
|
|
37
|
+
return savedData;
|
|
38
|
+
}
|
|
39
|
+
return data$1;
|
|
40
|
+
} catch (e) {
|
|
41
|
+
console.error(e);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}, "node")(path$1, data.trim(), options);
|
|
45
|
+
}
|
|
46
|
+
const fsPlugin = createPlugin({
|
|
47
|
+
name: "fs",
|
|
48
|
+
scope: "write",
|
|
49
|
+
async install(_app, context, options) {
|
|
50
|
+
context.events.on("process:progress", async ({ file, source }) => {
|
|
51
|
+
if (options === null || options === void 0 ? void 0 : options.onWrite) await options.onWrite(file.path, source);
|
|
52
|
+
await write(file.path, source, { sanity: false });
|
|
53
|
+
});
|
|
54
|
+
},
|
|
55
|
+
override(_app, context) {
|
|
56
|
+
return { async write(options = {
|
|
57
|
+
extension: { ".ts": ".ts" },
|
|
58
|
+
dryRun: false
|
|
59
|
+
}) {
|
|
60
|
+
await context.fileManager.write({
|
|
61
|
+
extension: options.extension,
|
|
62
|
+
dryRun: options.dryRun,
|
|
63
|
+
parsers: context.installedParsers
|
|
64
|
+
});
|
|
65
|
+
} };
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
//#endregion
|
|
70
|
+
export { createPlugin, fsPlugin };
|
|
71
|
+
//# sourceMappingURL=plugins.js.map
|
|
@@ -0,0 +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 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}\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\ntype WriteOptions = {\n extension?: Record<KubbFile.Extname, KubbFile.Extname | ''>\n dryRun?: boolean\n}\n\ndeclare module '../index.ts' {\n interface App {\n write(options?: WriteOptions): Promise<void>\n }\n}\n\nexport const fsPlugin = createPlugin<Options, { write(options?: WriteOptions): Promise<void> }>({\n name: 'fs',\n scope: 'write',\n async install(_app, context, options) {\n 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 override(_app, context) {\n return {\n async write(\n options = {\n extension: { '.ts': '.ts' },\n dryRun: false,\n },\n ) {\n await context.fileManager.write({\n extension: options.extension,\n dryRun: options.dryRun,\n parsers: context.installedParsers,\n })\n },\n }\n },\n})\n"],"mappings":";;;;;AAEA,SAAgB,aACd,QACgC;AAChC,QAAO;EACL,MAAM;EACN,GAAG;EACJ;;;;;ACMH,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,MAAM;AAEnC,gEAAI,UAAW,UAAU,uDAAKC,OAAM,UAAU,EAC5C,OAAM,IAAI,MAAM,2BAA2BD,OAAK,WAAWA,OAAK,OAAO,MAAMA,OAAK,YAAY,UAAU,OAAO,MAAM,UAAU,IAAI;AAGrI,YAAO;;AAGT,WAAOC;YACA,GAAG;AACV,YAAQ,MAAM,EAAE;;;EAGrB,EACD,OACD,CAACD,QAAM,KAAK,MAAM,EAAE,QAAQ;;AAc/B,MAAa,WAAW,aAAwE;CAC9F,MAAM;CACN,OAAO;CACP,MAAM,QAAQ,MAAM,SAAS,SAAS;AACpC,UAAQ,OAAO,GAAG,oBAAoB,OAAO,EAAE,MAAM,aAAa;AAChE,yDAAI,QAAS,QACX,OAAM,QAAQ,QAAQ,KAAK,MAAM,OAAO;AAE1C,SAAM,MAAM,KAAK,MAAM,QAAQ,EAAE,QAAQ,OAAO,CAAC;IACjD;;CAEJ,SAAS,MAAM,SAAS;AACtB,SAAO,EACL,MAAM,MACJ,UAAU;GACR,WAAW,EAAE,OAAO,OAAO;GAC3B,QAAQ;GACT,EACD;AACA,SAAM,QAAQ,YAAY,MAAM;IAC9B,WAAW,QAAQ;IACnB,QAAQ,QAAQ;IAChB,SAAS,QAAQ;IAClB,CAAC;KAEL;;CAEJ,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { a as createParser, i as typescriptParser } from "./typescriptParser-BBGeFKlP.js";
|
|
2
|
+
|
|
3
|
+
//#region src/parsers/defaultParser.ts
|
|
4
|
+
const defaultParser = createParser({
|
|
5
|
+
name: "default",
|
|
6
|
+
extNames: [".json"],
|
|
7
|
+
install() {},
|
|
8
|
+
async parse(file) {
|
|
9
|
+
return file.sources.map((item) => item.value).join("\n\n");
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/parsers/tsxParser.ts
|
|
15
|
+
const tsxParser = createParser({
|
|
16
|
+
name: "tsx",
|
|
17
|
+
extNames: [".tsx", ".jsx"],
|
|
18
|
+
install() {},
|
|
19
|
+
async parse(file, options = { extname: ".tsx" }) {
|
|
20
|
+
return typescriptParser.parse(file, options);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
export { defaultParser as n, tsxParser as t };
|
|
26
|
+
//# sourceMappingURL=tsxParser-C741ZKCN.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tsxParser-C741ZKCN.js","names":[],"sources":["../src/parsers/defaultParser.ts","../src/parsers/tsxParser.ts"],"sourcesContent":["import { createParser } from './createParser.ts'\n\nexport const defaultParser = createParser({\n name: 'default',\n extNames: ['.json'],\n install() {},\n async parse(file) {\n return file.sources.map((item) => item.value).join('\\n\\n')\n },\n})\n","import { typescriptParser } from './typescriptParser.ts'\nimport { createParser } from './createParser.ts'\n\nexport const tsxParser = createParser({\n name: 'tsx',\n extNames: ['.tsx', '.jsx'],\n install() {},\n async parse(file, options = { extname: '.tsx' }) {\n return typescriptParser.parse(file, options)\n },\n})\n"],"mappings":";;;AAEA,MAAa,gBAAgB,aAAa;CACxC,MAAM;CACN,UAAU,CAAC,QAAQ;CACnB,UAAU;CACV,MAAM,MAAM,MAAM;AAChB,SAAO,KAAK,QAAQ,KAAK,SAAS,KAAK,MAAM,CAAC,KAAK,OAAO;;CAE7D,CAAC;;;;ACNF,MAAa,YAAY,aAAa;CACpC,MAAM;CACN,UAAU,CAAC,QAAQ,OAAO;CAC1B,UAAU;CACV,MAAM,MAAM,MAAM,UAAU,EAAE,SAAS,QAAQ,EAAE;AAC/C,SAAO,iBAAiB,MAAM,MAAM,QAAQ;;CAE/C,CAAC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const require_typescriptParser = require('./typescriptParser-BBbbmG5W.cjs');
|
|
2
|
+
|
|
3
|
+
//#region src/parsers/defaultParser.ts
|
|
4
|
+
const defaultParser = require_typescriptParser.createParser({
|
|
5
|
+
name: "default",
|
|
6
|
+
extNames: [".json"],
|
|
7
|
+
install() {},
|
|
8
|
+
async parse(file) {
|
|
9
|
+
return file.sources.map((item) => item.value).join("\n\n");
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/parsers/tsxParser.ts
|
|
15
|
+
const tsxParser = require_typescriptParser.createParser({
|
|
16
|
+
name: "tsx",
|
|
17
|
+
extNames: [".tsx", ".jsx"],
|
|
18
|
+
install() {},
|
|
19
|
+
async parse(file, options = { extname: ".tsx" }) {
|
|
20
|
+
return require_typescriptParser.typescriptParser.parse(file, options);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
Object.defineProperty(exports, 'defaultParser', {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
get: function () {
|
|
28
|
+
return defaultParser;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
Object.defineProperty(exports, 'tsxParser', {
|
|
32
|
+
enumerable: true,
|
|
33
|
+
get: function () {
|
|
34
|
+
return tsxParser;
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
//# sourceMappingURL=tsxParser-HDf_3TMc.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tsxParser-HDf_3TMc.cjs","names":["createParser","createParser","typescriptParser"],"sources":["../src/parsers/defaultParser.ts","../src/parsers/tsxParser.ts"],"sourcesContent":["import { createParser } from './createParser.ts'\n\nexport const defaultParser = createParser({\n name: 'default',\n extNames: ['.json'],\n install() {},\n async parse(file) {\n return file.sources.map((item) => item.value).join('\\n\\n')\n },\n})\n","import { typescriptParser } from './typescriptParser.ts'\nimport { createParser } from './createParser.ts'\n\nexport const tsxParser = createParser({\n name: 'tsx',\n extNames: ['.tsx', '.jsx'],\n install() {},\n async parse(file, options = { extname: '.tsx' }) {\n return typescriptParser.parse(file, options)\n },\n})\n"],"mappings":";;;AAEA,MAAa,gBAAgBA,sCAAa;CACxC,MAAM;CACN,UAAU,CAAC,QAAQ;CACnB,UAAU;CACV,MAAM,MAAM,MAAM;AAChB,SAAO,KAAK,QAAQ,KAAK,SAAS,KAAK,MAAM,CAAC,KAAK,OAAO;;CAE7D,CAAC;;;;ACNF,MAAa,YAAYC,sCAAa;CACpC,MAAM;CACN,UAAU,CAAC,QAAQ,OAAO;CAC1B,UAAU;CACV,MAAM,MAAM,MAAM,UAAU,EAAE,SAAS,QAAQ,EAAE;AAC/C,SAAOC,0CAAiB,MAAM,MAAM,QAAQ;;CAE/C,CAAC"}
|
package/dist/types.d.cts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { f as KubbFile_d_exports, n as AppContext, t as App } from "./App-zyf9KG3p.cjs";
|
|
2
|
+
import { t as DefineApp } from "./defineApp-D3B0bU-z.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 {
|
|
2
|
-
import {
|
|
1
|
+
import { f as KubbFile_d_exports, n as AppContext, t as App } from "./App-DZuROf6f.js";
|
|
2
|
+
import { t as DefineApp } from "./defineApp-DJVMk9lc.js";
|
|
3
3
|
export { type App, type AppContext, type DefineApp, KubbFile_d_exports as KubbFile };
|
|
@@ -1,49 +1,28 @@
|
|
|
1
|
+
import path, { normalize, relative } from "node:path";
|
|
1
2
|
import ts from "typescript";
|
|
2
|
-
import path, { normalize, relative, resolve } from "node:path";
|
|
3
|
-
import fs from "fs-extra";
|
|
4
|
-
import { switcher } from "js-runtime";
|
|
5
3
|
|
|
6
|
-
//#region src/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
if ((savedData === null || savedData === void 0 ? void 0 : savedData.toString()) !== (data$1 === null || data$1 === void 0 ? void 0 : data$1.toString())) throw new Error(`Sanity check failed for ${path$2}\n\nData[${data$1.length}]:\n${data$1}\n\nSaved[${savedData.length}]:\n${savedData}\n`);
|
|
19
|
-
return savedData;
|
|
20
|
-
}
|
|
21
|
-
return data$1;
|
|
22
|
-
},
|
|
23
|
-
bun: async (path$2, data$1, { sanity }) => {
|
|
24
|
-
try {
|
|
25
|
-
await Bun.write(resolve(path$2), data$1);
|
|
26
|
-
if (sanity) {
|
|
27
|
-
const savedData = await Bun.file(resolve(path$2)).text();
|
|
28
|
-
if ((savedData === null || savedData === void 0 ? void 0 : savedData.toString()) !== (data$1 === null || data$1 === void 0 ? void 0 : data$1.toString())) throw new Error(`Sanity check failed for ${path$2}\n\nData[${path$2.length}]:\n${path$2}\n\nSaved[${savedData.length}]:\n${savedData}\n`);
|
|
29
|
-
return savedData;
|
|
30
|
-
}
|
|
31
|
-
return data$1;
|
|
32
|
-
} catch (e) {
|
|
33
|
-
console.error(e);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}, "node")(path$1, data.trim(), options);
|
|
4
|
+
//#region src/utils/trimExtName.ts
|
|
5
|
+
function trimExtName(text) {
|
|
6
|
+
return text.replace(/\.[^/.]+$/, "");
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
//#endregion
|
|
10
|
+
//#region src/parsers/createParser.ts
|
|
11
|
+
function createParser(parser) {
|
|
12
|
+
return {
|
|
13
|
+
type: "parser",
|
|
14
|
+
...parser
|
|
15
|
+
};
|
|
37
16
|
}
|
|
17
|
+
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/utils/getRelativePath.ts
|
|
38
20
|
function slash(path$1, platform = "linux") {
|
|
39
21
|
const isWindowsPath = /^\\\\\?\\/.test(path$1);
|
|
40
22
|
const normalizedPath = normalize(path$1);
|
|
41
23
|
if (["linux", "mac"].includes(platform) && !isWindowsPath) return normalizedPath.replaceAll(/\\/g, "/").replace("../", "");
|
|
42
24
|
return normalizedPath.replaceAll(/\\/g, "/").replace("../", "");
|
|
43
25
|
}
|
|
44
|
-
function trimExtName(text) {
|
|
45
|
-
return text.replace(/\.[^/.]+$/, "");
|
|
46
|
-
}
|
|
47
26
|
function getRelativePath(rootDir, filePath, platform = "linux") {
|
|
48
27
|
if (!rootDir || !filePath) throw new Error(`Root and file should be filled in when retrieving the relativePath, ${rootDir || ""} ${filePath || ""}`);
|
|
49
28
|
const slashedPath = slash(relative(rootDir, filePath), platform);
|
|
@@ -52,7 +31,7 @@ function getRelativePath(rootDir, filePath, platform = "linux") {
|
|
|
52
31
|
}
|
|
53
32
|
|
|
54
33
|
//#endregion
|
|
55
|
-
//#region src/parsers/
|
|
34
|
+
//#region src/parsers/typescriptParser.ts
|
|
56
35
|
const { factory } = ts;
|
|
57
36
|
/**
|
|
58
37
|
* Escaped new lines in code with block comments so they can be restored by {@link restoreNewLines}
|
|
@@ -115,66 +94,40 @@ function createExport({ path: path$1, asAlias, isTypeOnly = false, name }) {
|
|
|
115
94
|
return factory.createExportSpecifier(false, void 0, typeof propertyName === "string" ? factory.createIdentifier(propertyName) : propertyName);
|
|
116
95
|
})), factory.createStringLiteral(path$1), void 0);
|
|
117
96
|
}
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
const tsxParser = createFileParser({ async print(file, options = { extname: ".tsx" }) {
|
|
150
|
-
return typeScriptParser.print(file, options);
|
|
151
|
-
} });
|
|
152
|
-
|
|
153
|
-
//#endregion
|
|
154
|
-
//#region src/parsers/parser.ts
|
|
155
|
-
function createFileParser(parser) {
|
|
156
|
-
return parser;
|
|
157
|
-
}
|
|
158
|
-
const defaultParser = createFileParser({ async print(file) {
|
|
159
|
-
return file.sources.map((item) => item.value).join("\n\n");
|
|
160
|
-
} });
|
|
161
|
-
const parsers = {
|
|
162
|
-
".ts": typeScriptParser,
|
|
163
|
-
".js": typeScriptParser,
|
|
164
|
-
".jsx": tsxParser,
|
|
165
|
-
".tsx": tsxParser,
|
|
166
|
-
".json": defaultParser
|
|
167
|
-
};
|
|
168
|
-
async function parseFile(file, { extname } = {}) {
|
|
169
|
-
async function getFileParser(extname$1) {
|
|
170
|
-
if (!extname$1) return defaultParser;
|
|
171
|
-
const parser = parsers[extname$1];
|
|
172
|
-
if (!parser) console.warn(`[parser] No parser found for ${extname$1}, default parser will be used`);
|
|
173
|
-
return parser || defaultParser;
|
|
97
|
+
const typescriptParser = createParser({
|
|
98
|
+
name: "typescript",
|
|
99
|
+
extNames: [".ts", ".js"],
|
|
100
|
+
install() {},
|
|
101
|
+
async parse(file, options = { extname: ".ts" }) {
|
|
102
|
+
const source = file.sources.map((item) => item.value).join("\n\n");
|
|
103
|
+
const importNodes = file.imports.map((item) => {
|
|
104
|
+
const importPath = item.root ? getRelativePath(item.root, item.path) : item.path;
|
|
105
|
+
const hasExtname = !!path.extname(importPath);
|
|
106
|
+
return createImport({
|
|
107
|
+
name: item.name,
|
|
108
|
+
path: options.extname && hasExtname ? `${trimExtName(importPath)}${options.extname}` : item.root ? trimExtName(importPath) : importPath,
|
|
109
|
+
isTypeOnly: item.isTypeOnly
|
|
110
|
+
});
|
|
111
|
+
}).filter(Boolean);
|
|
112
|
+
const exportNodes = file.exports.map((item) => {
|
|
113
|
+
const exportPath = item.path;
|
|
114
|
+
const hasExtname = !!path.extname(exportPath);
|
|
115
|
+
return createExport({
|
|
116
|
+
name: item.name,
|
|
117
|
+
path: options.extname && hasExtname ? `${trimExtName(item.path)}${options.extname}` : trimExtName(item.path),
|
|
118
|
+
isTypeOnly: item.isTypeOnly,
|
|
119
|
+
asAlias: item.asAlias
|
|
120
|
+
});
|
|
121
|
+
}).filter(Boolean);
|
|
122
|
+
return [
|
|
123
|
+
file.banner,
|
|
124
|
+
print([...importNodes, ...exportNodes]),
|
|
125
|
+
source,
|
|
126
|
+
file.footer
|
|
127
|
+
].join("\n");
|
|
174
128
|
}
|
|
175
|
-
|
|
176
|
-
}
|
|
129
|
+
});
|
|
177
130
|
|
|
178
131
|
//#endregion
|
|
179
|
-
export {
|
|
180
|
-
//# sourceMappingURL=
|
|
132
|
+
export { createParser as a, typescriptParser as i, createImport as n, trimExtName as o, print as r, createExport as t };
|
|
133
|
+
//# sourceMappingURL=typescriptParser-BBGeFKlP.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typescriptParser-BBGeFKlP.js","names":["path","output: string","path","importPropertyName: ts.Identifier | undefined","importName: ts.NamedImportBindings | undefined"],"sources":["../src/utils/trimExtName.ts","../src/parsers/createParser.ts","../src/utils/getRelativePath.ts","../src/parsers/typescriptParser.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","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":";;;;AAAA,SAAgB,YAAY,MAAsB;AAChD,QAAO,KAAK,QAAQ,aAAa,GAAG;;;;;ACCtC,SAAgB,aAA2D,QAA8D;AACvI,QAAO;EACL,MAAM;EACN,GAAG;EACJ;;;;;ACJH,SAAS,MAAM,QAAc,WAAwC,SAAS;CAC5E,MAAM,gBAAgB,YAAY,KAAKA,OAAK;CAC5C,MAAM,iBAAiB,UAAUA,OAAK;AAEtC,KAAI,CAAC,SAAS,MAAM,CAAC,SAAS,SAAS,IAAI,CAAC,cAE1C,QAAO,eAAe,WAAW,OAAO,IAAI,CAAC,QAAQ,OAAO,GAAG;AAIjE,QAAO,eAAe,WAAW,OAAO,IAAI,CAAC,QAAQ,OAAO,GAA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