Skip to content

Commit a2299f5

Browse files
authored
Merge pull request #19 from macalinao/igm/cli-visitor-update
Pass in Anchor IDL to visitors config
2 parents dbbd147 + 0d09839 commit a2299f5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+558
-666
lines changed

.changeset/few-bats-rescue.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@macalinao/token-metadata-client": patch
3+
"@macalinao/grill-cli": patch
4+
"example-dapp": patch
5+
---
6+
7+
Regenerate token metadata client

.changeset/wet-carpets-boil.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@macalinao/grill-cli": patch
3+
---
4+
5+
Pass in Anchor IDL to visitors array

apps/example-dapp/src/routeTree.gen.ts

Lines changed: 92 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -8,160 +8,160 @@
88
// You should NOT make any changes in this file as it will be overwritten.
99
// Additionally, you should also exclude this file from your linter and/or formatter to prevent it from being checked or modified.
1010

11-
import { Route as rootRouteImport } from './routes/__root'
12-
import { Route as ExamplesRouteImport } from './routes/examples'
13-
import { Route as IndexRouteImport } from './routes/index'
14-
import { Route as ExamplesIndexRouteImport } from './routes/examples/index'
15-
import { Route as ExamplesWrappedSolRouteImport } from './routes/examples/wrapped-sol'
16-
import { Route as ExamplesTransferSolRouteImport } from './routes/examples/transfer-sol'
17-
import { Route as ExamplesTokensRouteImport } from './routes/examples/tokens'
18-
import { Route as ExamplesDashboardRouteImport } from './routes/examples/dashboard'
11+
import { Route as rootRouteImport } from "./routes/__root.tsx"
12+
import { Route as ExamplesRouteImport } from "./routes/examples.tsx"
13+
import { Route as IndexRouteImport } from "./routes/index.tsx"
14+
import { Route as ExamplesIndexRouteImport } from "./routes/examples/index.tsx"
15+
import { Route as ExamplesWrappedSolRouteImport } from "./routes/examples/wrapped-sol.tsx"
16+
import { Route as ExamplesTransferSolRouteImport } from "./routes/examples/transfer-sol.tsx"
17+
import { Route as ExamplesTokensRouteImport } from "./routes/examples/tokens.tsx"
18+
import { Route as ExamplesDashboardRouteImport } from "./routes/examples/dashboard.tsx"
1919

2020
const ExamplesRoute = ExamplesRouteImport.update({
21-
id: '/examples',
22-
path: '/examples',
21+
id: "/examples",
22+
path: "/examples",
2323
getParentRoute: () => rootRouteImport,
2424
} as any)
2525
const IndexRoute = IndexRouteImport.update({
26-
id: '/',
27-
path: '/',
26+
id: "/",
27+
path: "/",
2828
getParentRoute: () => rootRouteImport,
2929
} as any)
3030
const ExamplesIndexRoute = ExamplesIndexRouteImport.update({
31-
id: '/',
32-
path: '/',
31+
id: "/",
32+
path: "/",
3333
getParentRoute: () => ExamplesRoute,
3434
} as any)
3535
const ExamplesWrappedSolRoute = ExamplesWrappedSolRouteImport.update({
36-
id: '/wrapped-sol',
37-
path: '/wrapped-sol',
36+
id: "/wrapped-sol",
37+
path: "/wrapped-sol",
3838
getParentRoute: () => ExamplesRoute,
3939
} as any)
4040
const ExamplesTransferSolRoute = ExamplesTransferSolRouteImport.update({
41-
id: '/transfer-sol',
42-
path: '/transfer-sol',
41+
id: "/transfer-sol",
42+
path: "/transfer-sol",
4343
getParentRoute: () => ExamplesRoute,
4444
} as any)
4545
const ExamplesTokensRoute = ExamplesTokensRouteImport.update({
46-
id: '/tokens',
47-
path: '/tokens',
46+
id: "/tokens",
47+
path: "/tokens",
4848
getParentRoute: () => ExamplesRoute,
4949
} as any)
5050
const ExamplesDashboardRoute = ExamplesDashboardRouteImport.update({
51-
id: '/dashboard',
52-
path: '/dashboard',
51+
id: "/dashboard",
52+
path: "/dashboard",
5353
getParentRoute: () => ExamplesRoute,
5454
} as any)
5555

5656
export interface FileRoutesByFullPath {
57-
'/': typeof IndexRoute
58-
'/examples': typeof ExamplesRouteWithChildren
59-
'/examples/dashboard': typeof ExamplesDashboardRoute
60-
'/examples/tokens': typeof ExamplesTokensRoute
61-
'/examples/transfer-sol': typeof ExamplesTransferSolRoute
62-
'/examples/wrapped-sol': typeof ExamplesWrappedSolRoute
63-
'/examples/': typeof ExamplesIndexRoute
57+
"/": typeof IndexRoute
58+
"/examples": typeof ExamplesRouteWithChildren
59+
"/examples/dashboard": typeof ExamplesDashboardRoute
60+
"/examples/tokens": typeof ExamplesTokensRoute
61+
"/examples/transfer-sol": typeof ExamplesTransferSolRoute
62+
"/examples/wrapped-sol": typeof ExamplesWrappedSolRoute
63+
"/examples/": typeof ExamplesIndexRoute
6464
}
6565
export interface FileRoutesByTo {
66-
'/': typeof IndexRoute
67-
'/examples/dashboard': typeof ExamplesDashboardRoute
68-
'/examples/tokens': typeof ExamplesTokensRoute
69-
'/examples/transfer-sol': typeof ExamplesTransferSolRoute
70-
'/examples/wrapped-sol': typeof ExamplesWrappedSolRoute
71-
'/examples': typeof ExamplesIndexRoute
66+
"/": typeof IndexRoute
67+
"/examples/dashboard": typeof ExamplesDashboardRoute
68+
"/examples/tokens": typeof ExamplesTokensRoute
69+
"/examples/transfer-sol": typeof ExamplesTransferSolRoute
70+
"/examples/wrapped-sol": typeof ExamplesWrappedSolRoute
71+
"/examples": typeof ExamplesIndexRoute
7272
}
7373
export interface FileRoutesById {
7474
__root__: typeof rootRouteImport
75-
'/': typeof IndexRoute
76-
'/examples': typeof ExamplesRouteWithChildren
77-
'/examples/dashboard': typeof ExamplesDashboardRoute
78-
'/examples/tokens': typeof ExamplesTokensRoute
79-
'/examples/transfer-sol': typeof ExamplesTransferSolRoute
80-
'/examples/wrapped-sol': typeof ExamplesWrappedSolRoute
81-
'/examples/': typeof ExamplesIndexRoute
75+
"/": typeof IndexRoute
76+
"/examples": typeof ExamplesRouteWithChildren
77+
"/examples/dashboard": typeof ExamplesDashboardRoute
78+
"/examples/tokens": typeof ExamplesTokensRoute
79+
"/examples/transfer-sol": typeof ExamplesTransferSolRoute
80+
"/examples/wrapped-sol": typeof ExamplesWrappedSolRoute
81+
"/examples/": typeof ExamplesIndexRoute
8282
}
8383
export interface FileRouteTypes {
8484
fileRoutesByFullPath: FileRoutesByFullPath
8585
fullPaths:
86-
| '/'
87-
| '/examples'
88-
| '/examples/dashboard'
89-
| '/examples/tokens'
90-
| '/examples/transfer-sol'
91-
| '/examples/wrapped-sol'
92-
| '/examples/'
86+
| "/"
87+
| "/examples"
88+
| "/examples/dashboard"
89+
| "/examples/tokens"
90+
| "/examples/transfer-sol"
91+
| "/examples/wrapped-sol"
92+
| "/examples/"
9393
fileRoutesByTo: FileRoutesByTo
9494
to:
95-
| '/'
96-
| '/examples/dashboard'
97-
| '/examples/tokens'
98-
| '/examples/transfer-sol'
99-
| '/examples/wrapped-sol'
100-
| '/examples'
95+
| "/"
96+
| "/examples/dashboard"
97+
| "/examples/tokens"
98+
| "/examples/transfer-sol"
99+
| "/examples/wrapped-sol"
100+
| "/examples"
101101
id:
102-
| '__root__'
103-
| '/'
104-
| '/examples'
105-
| '/examples/dashboard'
106-
| '/examples/tokens'
107-
| '/examples/transfer-sol'
108-
| '/examples/wrapped-sol'
109-
| '/examples/'
102+
| "__root__"
103+
| "/"
104+
| "/examples"
105+
| "/examples/dashboard"
106+
| "/examples/tokens"
107+
| "/examples/transfer-sol"
108+
| "/examples/wrapped-sol"
109+
| "/examples/"
110110
fileRoutesById: FileRoutesById
111111
}
112112
export interface RootRouteChildren {
113113
IndexRoute: typeof IndexRoute
114114
ExamplesRoute: typeof ExamplesRouteWithChildren
115115
}
116116

117-
declare module '@tanstack/react-router' {
117+
declare module "@tanstack/react-router" {
118118
interface FileRoutesByPath {
119-
'/examples': {
120-
id: '/examples'
121-
path: '/examples'
122-
fullPath: '/examples'
119+
"/examples": {
120+
id: "/examples"
121+
path: "/examples"
122+
fullPath: "/examples"
123123
preLoaderRoute: typeof ExamplesRouteImport
124124
parentRoute: typeof rootRouteImport
125125
}
126-
'/': {
127-
id: '/'
128-
path: '/'
129-
fullPath: '/'
126+
"/": {
127+
id: "/"
128+
path: "/"
129+
fullPath: "/"
130130
preLoaderRoute: typeof IndexRouteImport
131131
parentRoute: typeof rootRouteImport
132132
}
133-
'/examples/': {
134-
id: '/examples/'
135-
path: '/'
136-
fullPath: '/examples/'
133+
"/examples/": {
134+
id: "/examples/"
135+
path: "/"
136+
fullPath: "/examples/"
137137
preLoaderRoute: typeof ExamplesIndexRouteImport
138138
parentRoute: typeof ExamplesRoute
139139
}
140-
'/examples/wrapped-sol': {
141-
id: '/examples/wrapped-sol'
142-
path: '/wrapped-sol'
143-
fullPath: '/examples/wrapped-sol'
140+
"/examples/wrapped-sol": {
141+
id: "/examples/wrapped-sol"
142+
path: "/wrapped-sol"
143+
fullPath: "/examples/wrapped-sol"
144144
preLoaderRoute: typeof ExamplesWrappedSolRouteImport
145145
parentRoute: typeof ExamplesRoute
146146
}
147-
'/examples/transfer-sol': {
148-
id: '/examples/transfer-sol'
149-
path: '/transfer-sol'
150-
fullPath: '/examples/transfer-sol'
147+
"/examples/transfer-sol": {
148+
id: "/examples/transfer-sol"
149+
path: "/transfer-sol"
150+
fullPath: "/examples/transfer-sol"
151151
preLoaderRoute: typeof ExamplesTransferSolRouteImport
152152
parentRoute: typeof ExamplesRoute
153153
}
154-
'/examples/tokens': {
155-
id: '/examples/tokens'
156-
path: '/tokens'
157-
fullPath: '/examples/tokens'
154+
"/examples/tokens": {
155+
id: "/examples/tokens"
156+
path: "/tokens"
157+
fullPath: "/examples/tokens"
158158
preLoaderRoute: typeof ExamplesTokensRouteImport
159159
parentRoute: typeof ExamplesRoute
160160
}
161-
'/examples/dashboard': {
162-
id: '/examples/dashboard'
163-
path: '/dashboard'
164-
fullPath: '/examples/dashboard'
161+
"/examples/dashboard": {
162+
id: "/examples/dashboard"
163+
path: "/dashboard"
164+
fullPath: "/examples/dashboard"
165165
preLoaderRoute: typeof ExamplesDashboardRouteImport
166166
parentRoute: typeof ExamplesRoute
167167
}

packages/grill-cli/src/bin/cli.ts

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,25 @@ program
4545
)
4646
.action(async (options: { idl: string; output: string; config: string }) => {
4747
try {
48-
const idlPath = resolve(options.idl);
49-
const outputPath = resolve(options.output);
5048
const configPath = resolve(options.config);
5149

50+
// Load config first if it exists
51+
let config: GrillConfig = {};
52+
if (await fileExists(configPath)) {
53+
console.log(`Loading config from ${configPath}...`);
54+
55+
// Dynamic import of the config file
56+
const configUrl = new URL(`file://${configPath}`);
57+
const configModule = (await import(configUrl.href)) as {
58+
default: GrillConfig;
59+
};
60+
config = configModule.default;
61+
}
62+
63+
// Use config values if provided, otherwise fall back to command line options
64+
const idlPath = resolve(config.idlPath ?? options.idl);
65+
const outputPath = resolve(config.outputDir ?? options.output);
66+
5267
// Check if IDL file exists
5368
if (!(await fileExists(idlPath))) {
5469
console.error(`Error: IDL file not found at ${idlPath}`);
@@ -66,28 +81,22 @@ program
6681
const root = rootNodeFromAnchor(idl);
6782
const codama = createFromRoot(root);
6883

69-
// Load and apply config if it exists
70-
if (await fileExists(configPath)) {
71-
console.log(`Loading config from ${configPath}...`);
84+
// Apply additional visitors from config
85+
if (config.visitors) {
86+
// Resolve visitors - either array or function
87+
const visitors =
88+
typeof config.visitors === "function"
89+
? config.visitors({ idl })
90+
: config.visitors;
7291

73-
// Dynamic import of the config file
74-
const configUrl = new URL(`file://${configPath}`);
75-
const configModule = (await import(configUrl.href)) as {
76-
default: GrillConfig;
77-
};
78-
const config = configModule.default;
79-
80-
// Apply additional visitors from config
81-
if (config.visitors && config.visitors.length > 0) {
92+
if (visitors.length > 0) {
8293
console.log(
83-
`Applying ${config.visitors.length.toLocaleString()} custom visitor(s)...`,
94+
`Applying ${visitors.length.toLocaleString()} custom visitor(s)...`,
8495
);
85-
for (const visitor of config.visitors) {
96+
for (const visitor of visitors) {
8697
codama.update(visitor);
8798
}
8899
}
89-
} else {
90-
console.log("No config file found, using defaults...");
91100
}
92101

93102
// Apply the ESM TypeScript visitor
@@ -124,19 +133,23 @@ program
124133
* @type {import('@macalinao/grill-cli').GrillConfig}
125134
*/
126135
export default {
136+
// Optional: Path to the Anchor IDL file (overrides --idl option)
137+
// idlPath: "./target/idl/program.json",
138+
139+
// Optional: Output directory for generated client (overrides --output option)
140+
// outputDir: "./src/generated",
141+
127142
// Optional: Add custom visitors to transform the Codama tree
128-
visitors: [
129-
// Example: Add a custom visitor
130-
// (node) => {
131-
// // Transform the node
132-
// return node;
133-
// }
134-
],
143+
// Can be an array of visitors or a function that returns visitors
144+
// visitors: [
145+
// // Example: Add a custom visitor
146+
// someVisitor(),
147+
// ],
135148
136-
// Optional: Configure the output
137-
output: {
138-
// Add any output configuration here
139-
}
149+
// Example using a function to access the IDL:
150+
// visitors: ({ idl }) => [
151+
// customVisitor(idl),
152+
// ],
140153
};
141154
`;
142155

0 commit comments

Comments
 (0)