@zaikit/codegen-react 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Lionel Tay
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,140 @@
1
+ // src/extract.ts
2
+ import { Project, ts } from "ts-morph";
3
+ function extractToolTypes(options) {
4
+ const { tsConfigPath, agentPath, exportName } = options;
5
+ const project = new Project({ tsConfigFilePath: tsConfigPath });
6
+ const sourceFile = project.getSourceFileOrThrow(agentPath);
7
+ const declarations = sourceFile.getExportedDeclarations().get(exportName);
8
+ if (!declarations || declarations.length === 0) {
9
+ throw new Error(`Export "${exportName}" not found in ${agentPath}`);
10
+ }
11
+ const agentDecl = declarations[0];
12
+ const agentType = agentDecl.getType();
13
+ const toolsProp = agentType.getProperty("tools");
14
+ if (!toolsProp) {
15
+ throw new Error(
16
+ `Agent export "${exportName}" has no "tools" property. Make sure createAgent is generic (see @zaikit/core).`
17
+ );
18
+ }
19
+ const toolsType = toolsProp.getTypeAtLocation(agentDecl);
20
+ const result = [];
21
+ for (const prop of toolsType.getProperties()) {
22
+ const toolName = prop.getName();
23
+ const toolType = prop.getTypeAtLocation(agentDecl);
24
+ const brandProp = toolType.getProperty("__toolTypes");
25
+ if (!brandProp) {
26
+ console.warn(
27
+ `Tool "${toolName}" missing __toolTypes brand \u2014 skipping (not a ZaikitTool?)`
28
+ );
29
+ continue;
30
+ }
31
+ const brandType = brandProp.getTypeAtLocation(agentDecl);
32
+ const formatFlags = ts.TypeFormatFlags.NoTruncation | ts.TypeFormatFlags.UseFullyQualifiedType;
33
+ const inputProp = brandType.getProperty("input");
34
+ const outputProp = brandType.getProperty("output");
35
+ const suspendProp = brandType.getProperty("suspend");
36
+ const resumeProp = brandType.getProperty("resume");
37
+ if (!inputProp) {
38
+ console.warn(`Tool "${toolName}" missing __toolTypes.input \u2014 skipping`);
39
+ continue;
40
+ }
41
+ const inputType = inputProp.getTypeAtLocation(agentDecl);
42
+ const outputType = outputProp ? outputProp.getTypeAtLocation(agentDecl) : null;
43
+ const suspendType = suspendProp ? suspendProp.getTypeAtLocation(agentDecl) : null;
44
+ const resumeType = resumeProp ? resumeProp.getTypeAtLocation(agentDecl) : null;
45
+ result.push({
46
+ name: toolName,
47
+ input: inputType.getText(agentDecl, formatFlags),
48
+ output: outputType ? outputType.getText(agentDecl, formatFlags) : "unknown",
49
+ suspend: suspendType && !suspendType.isNever() ? suspendType.getText(agentDecl, formatFlags) : null,
50
+ resume: resumeType && !resumeType.isNever() ? resumeType.getText(agentDecl, formatFlags) : null
51
+ });
52
+ }
53
+ return result;
54
+ }
55
+
56
+ // src/generate.ts
57
+ function toPascalCase(s) {
58
+ return s.replace(/[-_](\w)/g, (_, c) => c.toUpperCase()).replace(/^\w/, (c) => c.toUpperCase());
59
+ }
60
+ function generateOutput(tools) {
61
+ const lines = [
62
+ "// AUTO-GENERATED by @zaikit/codegen-react \u2014 DO NOT EDIT",
63
+ "",
64
+ 'import type { ReactNode } from "react";',
65
+ 'import type { ToolRenderProps, ToolRenderFn } from "@zaikit/react";',
66
+ 'import { useToolRenderer as _useToolRenderer } from "@zaikit/react";',
67
+ "",
68
+ "// \u2500\u2500\u2500 Types \u2500\u2500\u2500"
69
+ ];
70
+ for (const tool of tools) {
71
+ const pascal = toPascalCase(tool.name);
72
+ lines.push("");
73
+ lines.push(`export type ${pascal}Input = ${tool.input};`);
74
+ lines.push(`export type ${pascal}Output = ${tool.output};`);
75
+ if (tool.suspend) {
76
+ lines.push(`export type ${pascal}Suspend = ${tool.suspend};`);
77
+ }
78
+ if (tool.resume) {
79
+ lines.push(`export type ${pascal}Resume = ${tool.resume};`);
80
+ }
81
+ }
82
+ lines.push("");
83
+ lines.push("// \u2500\u2500\u2500 Typed Props \u2500\u2500\u2500");
84
+ for (const tool of tools) {
85
+ const pascal = toPascalCase(tool.name);
86
+ lines.push("");
87
+ if (tool.suspend && tool.resume) {
88
+ lines.push(
89
+ `export type ${pascal}ToolProps = ToolRenderProps<${pascal}Input, ${pascal}Suspend, ${pascal}Resume>;`
90
+ );
91
+ } else {
92
+ lines.push(
93
+ `export type ${pascal}ToolProps = ToolRenderProps<${pascal}Input>;`
94
+ );
95
+ }
96
+ }
97
+ lines.push("");
98
+ lines.push("// \u2500\u2500\u2500 Typed useToolRenderer \u2500\u2500\u2500");
99
+ lines.push("");
100
+ if (tools.length > 0) {
101
+ lines.push("type ToolPropsMap = {");
102
+ for (const tool of tools) {
103
+ const pascal = toPascalCase(tool.name);
104
+ lines.push(` ${tool.name}: ${pascal}ToolProps;`);
105
+ }
106
+ lines.push("};");
107
+ lines.push("");
108
+ lines.push(
109
+ "export function useToolRenderer<T extends keyof ToolPropsMap>(name: T, render: (props: ToolPropsMap[T]) => ReactNode): void;"
110
+ );
111
+ lines.push(
112
+ "export function useToolRenderer(name: string, render: ToolRenderFn): void;"
113
+ );
114
+ lines.push(
115
+ "export function useToolRenderer(name: string, render: ToolRenderFn): void {"
116
+ );
117
+ lines.push(" _useToolRenderer(name, render);");
118
+ lines.push("}");
119
+ } else {
120
+ lines.push("export { _useToolRenderer as useToolRenderer };");
121
+ }
122
+ lines.push("");
123
+ lines.push("// \u2500\u2500\u2500 Tool Name Union \u2500\u2500\u2500");
124
+ lines.push("");
125
+ if (tools.length > 0) {
126
+ const union = tools.map((t) => ` | "${t.name}"`).join("\n");
127
+ lines.push(`export type ToolName =
128
+ ${union};`);
129
+ } else {
130
+ lines.push("export type ToolName = never;");
131
+ }
132
+ lines.push("");
133
+ return lines.join("\n");
134
+ }
135
+
136
+ export {
137
+ extractToolTypes,
138
+ generateOutput
139
+ };
140
+ //# sourceMappingURL=chunk-XWVGW5I6.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/extract.ts","../src/generate.ts"],"sourcesContent":["import { Project, ts } from \"ts-morph\";\n\nexport type ToolTypeInfo = {\n name: string;\n input: string;\n output: string;\n suspend: string | null;\n resume: string | null;\n};\n\nexport function extractToolTypes(options: {\n tsConfigPath: string;\n agentPath: string;\n exportName: string;\n}): ToolTypeInfo[] {\n const { tsConfigPath, agentPath, exportName } = options;\n\n const project = new Project({ tsConfigFilePath: tsConfigPath });\n const sourceFile = project.getSourceFileOrThrow(agentPath);\n\n const declarations = sourceFile.getExportedDeclarations().get(exportName);\n if (!declarations || declarations.length === 0) {\n throw new Error(`Export \"${exportName}\" not found in ${agentPath}`);\n }\n const agentDecl = declarations[0];\n const agentType = agentDecl.getType();\n\n const toolsProp = agentType.getProperty(\"tools\");\n if (!toolsProp) {\n throw new Error(\n `Agent export \"${exportName}\" has no \"tools\" property. ` +\n `Make sure createAgent is generic (see @zaikit/core).`,\n );\n }\n const toolsType = toolsProp.getTypeAtLocation(agentDecl);\n const result: ToolTypeInfo[] = [];\n\n for (const prop of toolsType.getProperties()) {\n const toolName = prop.getName();\n const toolType = prop.getTypeAtLocation(agentDecl);\n\n const brandProp = toolType.getProperty(\"__toolTypes\");\n if (!brandProp) {\n console.warn(\n `Tool \"${toolName}\" missing __toolTypes brand — skipping (not a ZaikitTool?)`,\n );\n continue;\n }\n const brandType = brandProp.getTypeAtLocation(agentDecl);\n\n const formatFlags =\n ts.TypeFormatFlags.NoTruncation |\n ts.TypeFormatFlags.UseFullyQualifiedType;\n\n const inputProp = brandType.getProperty(\"input\");\n const outputProp = brandType.getProperty(\"output\");\n const suspendProp = brandType.getProperty(\"suspend\");\n const resumeProp = brandType.getProperty(\"resume\");\n\n if (!inputProp) {\n console.warn(`Tool \"${toolName}\" missing __toolTypes.input — skipping`);\n continue;\n }\n\n const inputType = inputProp.getTypeAtLocation(agentDecl);\n const outputType = outputProp\n ? outputProp.getTypeAtLocation(agentDecl)\n : null;\n const suspendType = suspendProp\n ? suspendProp.getTypeAtLocation(agentDecl)\n : null;\n const resumeType = resumeProp\n ? resumeProp.getTypeAtLocation(agentDecl)\n : null;\n\n result.push({\n name: toolName,\n input: inputType.getText(agentDecl, formatFlags),\n output: outputType\n ? outputType.getText(agentDecl, formatFlags)\n : \"unknown\",\n suspend:\n suspendType && !suspendType.isNever()\n ? suspendType.getText(agentDecl, formatFlags)\n : null,\n resume:\n resumeType && !resumeType.isNever()\n ? resumeType.getText(agentDecl, formatFlags)\n : null,\n });\n }\n\n return result;\n}\n","import type { ToolTypeInfo } from \"./extract\";\n\nexport function toPascalCase(s: string): string {\n return s\n .replace(/[-_](\\w)/g, (_, c: string) => c.toUpperCase())\n .replace(/^\\w/, (c) => c.toUpperCase());\n}\n\nexport function generateOutput(tools: ToolTypeInfo[]): string {\n const lines: string[] = [\n \"// AUTO-GENERATED by @zaikit/codegen-react — DO NOT EDIT\",\n \"\",\n 'import type { ReactNode } from \"react\";',\n 'import type { ToolRenderProps, ToolRenderFn } from \"@zaikit/react\";',\n 'import { useToolRenderer as _useToolRenderer } from \"@zaikit/react\";',\n \"\",\n \"// ─── Types ───\",\n ];\n\n for (const tool of tools) {\n const pascal = toPascalCase(tool.name);\n lines.push(\"\");\n lines.push(`export type ${pascal}Input = ${tool.input};`);\n lines.push(`export type ${pascal}Output = ${tool.output};`);\n if (tool.suspend) {\n lines.push(`export type ${pascal}Suspend = ${tool.suspend};`);\n }\n if (tool.resume) {\n lines.push(`export type ${pascal}Resume = ${tool.resume};`);\n }\n }\n\n lines.push(\"\");\n lines.push(\"// ─── Typed Props ───\");\n\n for (const tool of tools) {\n const pascal = toPascalCase(tool.name);\n lines.push(\"\");\n if (tool.suspend && tool.resume) {\n lines.push(\n `export type ${pascal}ToolProps = ToolRenderProps<${pascal}Input, ${pascal}Suspend, ${pascal}Resume>;`,\n );\n } else {\n lines.push(\n `export type ${pascal}ToolProps = ToolRenderProps<${pascal}Input>;`,\n );\n }\n }\n\n // ─── Tool Props Map + Typed useToolRenderer ───\n lines.push(\"\");\n lines.push(\"// ─── Typed useToolRenderer ───\");\n lines.push(\"\");\n\n if (tools.length > 0) {\n lines.push(\"type ToolPropsMap = {\");\n for (const tool of tools) {\n const pascal = toPascalCase(tool.name);\n lines.push(` ${tool.name}: ${pascal}ToolProps;`);\n }\n lines.push(\"};\");\n lines.push(\"\");\n lines.push(\n \"export function useToolRenderer<T extends keyof ToolPropsMap>(name: T, render: (props: ToolPropsMap[T]) => ReactNode): void;\",\n );\n lines.push(\n \"export function useToolRenderer(name: string, render: ToolRenderFn): void;\",\n );\n lines.push(\n \"export function useToolRenderer(name: string, render: ToolRenderFn): void {\",\n );\n lines.push(\" _useToolRenderer(name, render);\");\n lines.push(\"}\");\n } else {\n lines.push(\"export { _useToolRenderer as useToolRenderer };\");\n }\n\n // ─── Tool Name Union ───\n lines.push(\"\");\n lines.push(\"// ─── Tool Name Union ───\");\n lines.push(\"\");\n if (tools.length > 0) {\n const union = tools.map((t) => ` | \"${t.name}\"`).join(\"\\n\");\n lines.push(`export type ToolName =\\n${union};`);\n } else {\n lines.push(\"export type ToolName = never;\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 #!/usr/bin/env node
package/dist/cli.js ADDED
@@ -0,0 +1,88 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ extractToolTypes,
4
+ generateOutput
5
+ } from "./chunk-XWVGW5I6.js";
6
+
7
+ // src/cli.ts
8
+ import fs from "fs";
9
+ import path from "path";
10
+ function findTsConfig(startDir) {
11
+ let dir = startDir;
12
+ while (true) {
13
+ const candidate = path.join(dir, "tsconfig.json");
14
+ if (fs.existsSync(candidate)) return candidate;
15
+ const parent = path.dirname(dir);
16
+ if (parent === dir) return null;
17
+ dir = parent;
18
+ }
19
+ }
20
+ function parseArgs(argv) {
21
+ const args = {};
22
+ for (let i = 2; i < argv.length; i++) {
23
+ const arg = argv[i];
24
+ if (arg.startsWith("--")) {
25
+ const eqIdx = arg.indexOf("=");
26
+ if (eqIdx !== -1) {
27
+ args[arg.slice(2, eqIdx)] = arg.slice(eqIdx + 1);
28
+ } else if (i + 1 < argv.length && !argv[i + 1].startsWith("--")) {
29
+ args[arg.slice(2)] = argv[++i];
30
+ }
31
+ }
32
+ }
33
+ if (!args.agent) {
34
+ console.error(
35
+ "Usage: zaikit-codegen-react --agent <path> --output <path> [--export <name>] [--tsconfig <path>]"
36
+ );
37
+ process.exit(1);
38
+ }
39
+ if (!args.output) {
40
+ console.error("Missing required --output flag");
41
+ process.exit(1);
42
+ }
43
+ return {
44
+ agent: args.agent,
45
+ export: args.export ?? "agent",
46
+ output: args.output,
47
+ tsconfig: args.tsconfig
48
+ };
49
+ }
50
+ function main() {
51
+ const args = parseArgs(process.argv);
52
+ const cwd = process.cwd();
53
+ const agentPath = path.resolve(cwd, args.agent);
54
+ const outputPath = path.resolve(cwd, args.output);
55
+ if (!fs.existsSync(agentPath)) {
56
+ console.error(`Agent file not found: ${agentPath}`);
57
+ process.exit(1);
58
+ }
59
+ const tsConfigPath = args.tsconfig ? path.resolve(cwd, args.tsconfig) : findTsConfig(path.dirname(agentPath));
60
+ if (!tsConfigPath) {
61
+ console.error(
62
+ `Could not find tsconfig.json from ${path.dirname(agentPath)}. Use --tsconfig to specify one.`
63
+ );
64
+ process.exit(1);
65
+ }
66
+ console.log(`Agent: ${agentPath}`);
67
+ console.log(`Export: ${args.export}`);
68
+ console.log(`TSConfig: ${tsConfigPath}`);
69
+ console.log(`Output: ${outputPath}`);
70
+ console.log("");
71
+ const tools = extractToolTypes({
72
+ tsConfigPath,
73
+ agentPath,
74
+ exportName: args.export
75
+ });
76
+ if (tools.length === 0) {
77
+ console.warn("No tools found on the agent. Generated file will be empty.");
78
+ }
79
+ const output = generateOutput(tools);
80
+ const outputDir = path.dirname(outputPath);
81
+ if (!fs.existsSync(outputDir)) {
82
+ fs.mkdirSync(outputDir, { recursive: true });
83
+ }
84
+ fs.writeFileSync(outputPath, output);
85
+ console.log(`Generated ${tools.length} tool type(s) \u2192 ${outputPath}`);
86
+ }
87
+ main();
88
+ //# sourceMappingURL=cli.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/cli.ts"],"sourcesContent":["#!/usr/bin/env node\nimport fs from \"node:fs\";\nimport path from \"node:path\";\nimport { extractToolTypes } from \"./extract\";\nimport { generateOutput } from \"./generate\";\n\nfunction findTsConfig(startDir: string): string | null {\n let dir = startDir;\n while (true) {\n const candidate = path.join(dir, \"tsconfig.json\");\n if (fs.existsSync(candidate)) return candidate;\n const parent = path.dirname(dir);\n if (parent === dir) return null;\n dir = parent;\n }\n}\n\nfunction parseArgs(argv: string[]): {\n agent: string;\n export: string;\n output: string;\n tsconfig: string | undefined;\n} {\n const args: Record<string, string> = {};\n for (let i = 2; i < argv.length; i++) {\n const arg = argv[i];\n if (arg.startsWith(\"--\")) {\n const eqIdx = arg.indexOf(\"=\");\n if (eqIdx !== -1) {\n args[arg.slice(2, eqIdx)] = arg.slice(eqIdx + 1);\n } else if (i + 1 < argv.length && !argv[i + 1].startsWith(\"--\")) {\n args[arg.slice(2)] = argv[++i];\n }\n }\n }\n\n if (!args.agent) {\n console.error(\n \"Usage: zaikit-codegen-react --agent <path> --output <path> [--export <name>] [--tsconfig <path>]\",\n );\n process.exit(1);\n }\n if (!args.output) {\n console.error(\"Missing required --output flag\");\n process.exit(1);\n }\n\n return {\n agent: args.agent,\n export: args.export ?? \"agent\",\n output: args.output,\n tsconfig: args.tsconfig,\n };\n}\n\nfunction main() {\n const args = parseArgs(process.argv);\n const cwd = process.cwd();\n\n const agentPath = path.resolve(cwd, args.agent);\n const outputPath = path.resolve(cwd, args.output);\n\n if (!fs.existsSync(agentPath)) {\n console.error(`Agent file not found: ${agentPath}`);\n process.exit(1);\n }\n\n const tsConfigPath = args.tsconfig\n ? path.resolve(cwd, args.tsconfig)\n : findTsConfig(path.dirname(agentPath));\n\n if (!tsConfigPath) {\n console.error(\n `Could not find tsconfig.json from ${path.dirname(agentPath)}. Use --tsconfig to specify one.`,\n );\n process.exit(1);\n }\n\n console.log(`Agent: ${agentPath}`);\n console.log(`Export: ${args.export}`);\n console.log(`TSConfig: ${tsConfigPath}`);\n console.log(`Output: ${outputPath}`);\n console.log(\"\");\n\n const tools = extractToolTypes({\n tsConfigPath,\n agentPath,\n exportName: args.export,\n });\n\n if (tools.length === 0) {\n console.warn(\"No tools found on the agent. Generated file will be empty.\");\n }\n\n const output = generateOutput(tools);\n\n const outputDir = path.dirname(outputPath);\n if (!fs.existsSync(outputDir)) {\n fs.mkdirSync(outputDir, { recursive: true });\n }\n\n fs.writeFileSync(outputPath, output);\n console.log(`Generated ${tools.length} tool type(s) → ${outputPath}`);\n}\n\nmain();\n"],"mappings":";;;;;;;AACA,OAAO,QAAQ;AACf,OAAO,UAAU;AAIjB,SAAS,aAAa,UAAiC;AACrD,MAAI,MAAM;AACV,SAAO,MAAM;AACX,UAAM,YAAY,KAAK,KAAK,KAAK,eAAe;AAChD,QAAI,GAAG,WAAW,SAAS,EAAG,QAAO;AACrC,UAAM,SAAS,KAAK,QAAQ,GAAG;AAC/B,QAAI,WAAW,IAAK,QAAO;AAC3B,UAAM;AAAA,EACR;AACF;AAEA,SA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,cAAc;AACjB,YAAQ;AAAA,MACN,qCAAqC,KAAK,QAAQ,SAAS,CAAC;AAAA,IAC9D;AACA,YAAQ,KAAK,CAAC;AAAA,EAChB;AAEA,UAAQ,IAAI,aAAa,SAAS,EAAE;AACpC,UAAQ,IAAI,aAAa,KAAK,MAAM,EAAE;AACtC,UAAQ,IAAI,aAAa,YAAY,EAAE;AACvC,UAAQ,IAAI,aAAa,UAAU,EAAE;AACrC,UAAQ,IAAI,EAAE;AAEd,QAAM,QAAQ,iBAAiB;AAAA,IAC7B;AAAA,IACA;AAAA,IACA,YAAY,KAAK;AAAA,EACnB,CAAC;AAED,MAAI,MAAM,WAAW,GAAG;AACtB,YAAQ,KAAK,4DAA4D;AAAA,EAC3E;AAEA,QAAM,SAAS,eAAe,KAAK;AAEnC,QAAM,YAAY,KAAK,QAAQ,UAAU;AACzC,MAAI,CAAC,GAAG,WAAW,SAAS,GAAG;AAC7B,OAAG,UAAU,WAAW,EAAE,WAAW,KAAK,CAAC;AAAA,EAC7C;AAEA,KAAG,cAAc,YAAY,MAAM;AACnC,UAAQ,IAAI,aAAa,MAAM,MAAM,wBAAmB,UAAU,EAAE;AACtE;AAEA,KAAK;","names":[]}
@@ -0,0 +1,16 @@
1
+ type ToolTypeInfo = {
2
+ name: string;
3
+ input: string;
4
+ output: string;
5
+ suspend: string | null;
6
+ resume: string | null;
7
+ };
8
+ declare function extractToolTypes(options: {
9
+ tsConfigPath: string;
10
+ agentPath: string;
11
+ exportName: string;
12
+ }): ToolTypeInfo[];
13
+
14
+ declare function generateOutput(tools: ToolTypeInfo[]): string;
15
+
16
+ export { type ToolTypeInfo, extractToolTypes, generateOutput };
package/dist/index.js ADDED
@@ -0,0 +1,9 @@
1
+ import {
2
+ extractToolTypes,
3
+ generateOutput
4
+ } from "./chunk-XWVGW5I6.js";
5
+ export {
6
+ extractToolTypes,
7
+ generateOutput
8
+ };
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@zaikit/codegen-react",
3
+ "version": "0.1.0",
4
+ "license": "MIT",
5
+ "type": "module",
6
+ "files": [
7
+ "dist"
8
+ ],
9
+ "bin": {
10
+ "zaikit-codegen-react": "./dist/cli.js"
11
+ },
12
+ "exports": {
13
+ ".": {
14
+ "import": "./dist/index.js",
15
+ "types": "./dist/index.d.ts"
16
+ }
17
+ },
18
+ "dependencies": {
19
+ "ts-morph": "^25"
20
+ },
21
+ "devDependencies": {
22
+ "@types/node": "^22",
23
+ "tsup": "^8",
24
+ "typescript": "^5",
25
+ "vitest": "^3",
26
+ "zod": "^4",
27
+ "@zaikit/core": "0.1.0",
28
+ "@zaikit/typescript-config": "0.0.0"
29
+ },
30
+ "scripts": {
31
+ "build": "tsup",
32
+ "dev": "tsup --watch",
33
+ "check-types": "tsc --noEmit",
34
+ "test": "vitest run"
35
+ }
36
+ }