11#!/usr/bin/env node
22import { writeFile } from "node:fs/promises" ;
33import { resolve } from "node:path" ;
4+ import { renderVisitor as renderRustVisitor } from "@codama/renderers-rust" ;
45import { renderESMTypeScriptVisitor } from "@macalinao/codama-renderers-js-esm" ;
56import { renderMarkdownVisitor } from "@macalinao/codama-renderers-markdown" ;
67import { Command } from "commander" ;
8+ import { CONFIG_TEMPLATE } from "../config-template.js" ;
79import { fileExists , processIdls } from "../utils/index.js" ;
810
911const program = new Command ( ) ;
@@ -17,25 +19,15 @@ program
1719 . command ( "generate" )
1820 . alias ( "gen" )
1921 . description ( "Generate a client from an Anchor IDL" )
20- . option (
21- "-i, --idl <path>" ,
22- "Path to the Anchor IDL file(s) or glob pattern" ,
23- "./idls/*.json" ,
24- )
25- . option (
26- "-o, --output <path>" ,
27- "Output directory for generated client" ,
28- "./src/generated" ,
29- )
3022 . option (
3123 "-c, --config <path>" ,
3224 "Path to coda.config.mjs file" ,
3325 "./coda.config.mjs" ,
3426 )
35- . action ( async ( options : { idl : string ; output : string ; config : string } ) => {
27+ . action ( async ( options : { config : string } ) => {
3628 try {
3729 const { codama, config } = await processIdls ( options ) ;
38- const outputPath = resolve ( config . outputDir ?? options . output ) ;
30+ const outputPath = resolve ( config . outputDir ?? "./src/generated/" ) ;
3931
4032 // Apply the ESM TypeScript visitor
4133 console . log ( `Generating client to ${ outputPath } ...` ) ;
@@ -66,46 +58,8 @@ program
6658 process . exit ( 1 ) ;
6759 }
6860
69- // Create config template
70- const configTemplate = `/**
71- * @type {import('@macalinao/coda').CodaConfig}
72- */
73- export default {
74- // Optional: Path to the Anchor IDL file(s) (overrides --idl option)
75- // Can be a single path, glob pattern, or an array of paths/patterns
76- // Default: Looks for "./idls/*.json" if available, otherwise "./target/idl/program.json"
77- // idlPath: "./target/idl/program.json", // Single file
78- // idlPath: "./idls/*.json", // Glob pattern (all JSON files in idls/)
79- // idlPath: "./idls/program_*.json", // Glob pattern (matching files)
80- // idlPath: ["./idls/program1.json", "./idls/program2.json"], // Array of files
81- // idlPath: ["./idls/*.json", "./extra/*.json"], // Array with glob patterns
82-
83- // Optional: Output directory for generated client (overrides --output option)
84- // outputDir: "./src/generated",
85-
86- // Optional: Documentation generation options
87- // docs: {
88- // // NPM package name for the TypeScript client
89- // // If provided, will add an NPM badge and link to the package
90- // npmPackageName: "@my-org/my-solana-client",
91- // },
92-
93- // Optional: Add custom visitors to transform the Codama tree
94- // Can be an array of visitors or a function that returns visitors
95- // visitors: [
96- // // Example: Add a custom visitor
97- // someVisitor(),
98- // ],
99-
100- // Example using a function to access the IDL:
101- // visitors: ({ idl }) => [
102- // customVisitor(idl),
103- // ],
104- };
105- ` ;
106-
10761 // Write config file
108- await writeFile ( configPath , configTemplate , "utf-8" ) ;
62+ await writeFile ( configPath , CONFIG_TEMPLATE , "utf-8" ) ;
10963
11064 console . log ( `✅ Created coda.config.mjs at ${ configPath } ` ) ;
11165 console . log ( "\nNext steps:" ) ;
@@ -119,6 +73,31 @@ export default {
11973 }
12074 } ) ;
12175
76+ program
77+ . command ( "generate-rust" )
78+ . alias ( "gen-rust" )
79+ . description ( "Generate a Rust client from an Anchor IDL" )
80+ . option (
81+ "-c, --config <path>" ,
82+ "Path to coda.config.mjs file" ,
83+ "./coda.config.mjs" ,
84+ )
85+ . action ( async ( options : { config : string } ) => {
86+ try {
87+ const { codama, config } = await processIdls ( options ) ;
88+ const outputPath = resolve ( config . rustOutputDir ?? "./rust" ) ;
89+
90+ // Apply the Rust visitor
91+ console . log ( `Generating Rust client to ${ outputPath } ...` ) ;
92+ codama . accept ( renderRustVisitor ( outputPath ) ) ;
93+
94+ console . log ( "✅ Rust client generated successfully!" ) ;
95+ } catch ( error ) {
96+ console . error ( "Error generating Rust client:" , error ) ;
97+ process . exit ( 1 ) ;
98+ }
99+ } ) ;
100+
122101program
123102 . command ( "docs" )
124103 . description ( "Generate markdown documentation from an Anchor IDL" )
0 commit comments