You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -11,77 +11,229 @@ This package provides a CLI for the Codama library that can be used to run scrip
11
11
12
12
Note that, whilst the CLI code is located in the `@codama/cli` package, the CLI binary is also included in the main `codama` library.
13
13
14
-
## Getting started
14
+
## Installation
15
15
16
-
To get started with Codama, simply install `codama`to your project and run the `init` command like so:
16
+
Once you have the `codama`package installed, you can use the CLI using the `codama` binary.
17
17
18
18
```sh
19
19
pnpm install codama
20
-
pnpm codama init
20
+
codama --help
21
21
```
22
22
23
-
You will be prompted for the path of your IDL and asked to select any script presets you would like to use.
23
+
Note that you may install it from `@codama/cli` instead if you only want the CLI without the rest of the Codama library.
24
+
25
+
## Commands
26
+
27
+
### `codama init`
24
28
25
-
To initialize a [gill based Codama](https://gill.site/docs/guides/codama)configurationfile, run the `init` command with the `--gill` flag like so:
29
+
To get started with the Codama CLI, you will first need to create a [Codama configuration file](#configuration-file). You can do this using the `init` command as follows:
26
30
27
31
```sh
28
-
pnpm codama init --gill
32
+
codama init
29
33
```
30
34
31
-
## `codama run`
35
+
You will be prompted for the path of your IDL and asked to select any script presets you would like to use.
36
+
37
+
Here are the available options for the `init` command:
32
38
33
-
Once you have your codama configuration file, you can run your Codama scripts using the `codama run` command as follows:
|`[scripts...]`| As optional arguments, you may provide the names of the scripts you wish to run. |
62
+
|`--all`, `-a`| Runs all the scripts defined in your configuration file. |
63
+
|`--config <path>`, `-c`| Specifies the path to your configuration file. Defaults to `codama.json` or `codama.js`. |
64
+
|`--idl`, `-i`| Overrides the `idl` field in your configuration file. |
65
+
66
+
## Configuration file
67
+
68
+
> [!NOTE]
69
+
> If you don't have a Codama configuration file yet, you can generate a new one using the `codama init` command as [described above](#codama-init).
42
70
43
-
The codama configuration file defines an object containing the following fields:
71
+
The Codama configuration file defines an object containing the following fields:
44
72
45
73
-`idl` (string): The path to the IDL file. This can be a Codama IDL or an Anchor IDL which will be automatically converted to a Codama IDL.
46
-
-`before` (array): An array of visitors that will run before every script.
74
+
```json
75
+
{
76
+
"idl": "path/to/idl"
77
+
}
78
+
```
79
+
- `before` (array): An array of visitors that will run before every script. See [the next section](#using-visitors) for more details on how to use visitors in your configuration file.
- `scripts` (object): An object defining the available Codama scripts. The keys identify the scripts and the values are arrays of visitors that make up the script.
86
+
```json
87
+
{
88
+
"scripts": {
89
+
"apple": ["./my-apple-visitor.js"],
90
+
"banana": ["./my-banana-visitor.js"]
91
+
}
92
+
}
93
+
```
94
+
95
+
Whether it is in the `before` array or in the `scripts` values, if you have a single visitor, you may provide it directly instead of wrapping it in an array. For instance, the following configuration file is valid:
96
+
97
+
```json
98
+
{
99
+
"idl": "path/to/idl",
100
+
"before": "./my-visitor.js",
101
+
"scripts": { "apple": "./my-apple-visitor.js" }
102
+
}
103
+
```
48
104
49
-
Whether it is in the `before` array or in the `scripts` values, when defining a visitor you may either provide:
105
+
Note that the configuration file can also be a JavaScript file exporting the configuration object as the `default` export.
50
106
51
-
- an object with the following fields:
52
-
-`from` (string): The import path to the visitor.
53
-
-`args` (array): An array of arguments to pass to the visitor.
54
-
- a string: The import path to the visitor. This is equivalent to providing an object with a `from` field and an empty `args` array.
107
+
```js
108
+
exportdefault {
109
+
idl:'path/to/idl',
110
+
before:'./my-visitor.js',
111
+
scripts: { apple:'./my-apple-visitor.js' },
112
+
};
113
+
```
114
+
115
+
## Using visitors
116
+
117
+
When using a visitor in your configuration file, **you must provide its import path**. This can either be a local path (relative or absolute) or an external NPM package. For instance the following are all valid visitor import paths:
118
+
119
+
```js
120
+
'./my-visitor.js'; // Relative local path.
121
+
'/Users/me/my-visitor.js'; // Absolute local path.
122
+
'some-library'; // External package.
123
+
'@acme/some-library'; // Scoped external package.
124
+
```
55
125
56
-
Visitor import paths can either be local paths (pointing to JavaScript files exporting visitors) or npm package names. By default, the `default` export will be used but you may specify a named export by appending a `#` followed by the export name. When resolved, the imported element inside the module should either be a `Visitor<any, 'rootNode'>` or a function that returns a `Visitor<any, 'rootNode'>` given the arguments provided. Here are some examples of valid visitor import paths:
126
+
In all these cases, the visitor will be imported from the `default` export of the module by default. If you want to **import a named export** instead, you may append a `#` followed by the export name to the import path. For instance:
57
127
58
128
```js
59
-
'./my-visitor.js'; // Relative local path to a visitor module.
60
-
'/Users/me/my-visitor.js'; // Absolute local path to a visitor module.
61
-
'some-library'; // npm package name.
62
-
'@acme/some-library'; // Scoped npm package name.
63
129
'./my-visitor.js#myExport'; // Named export from a local path.
64
-
'@acme/some-library#myExport'; // Named export from an npm package.
130
+
'@acme/some-library#myExport'; // Named export from an external package.
65
131
```
66
132
67
-
Here is an example of what a Codama configuration file might look like:
133
+
Some visitors may instead be **exported as functions** that return visitor objects. This allows the visitor to receive arguments from the end-user to customize its behavior. When that is the case, you may instead define your visitor as an object with the following fields:
134
+
135
+
-`from` (string): The import path (and potentially the export name) of the visitor.
136
+
-`args` (array): An array of arguments to pass to the visitor. If you don't need to pass any arguments to the visitor function, you may simply provide the import path as a string instead of an object.
Note that you can use the `--js` flag to generate a `.js` configuration file when running the `init` command.
145
+
Finally note that, if the invoked visitor returns a new `RootNode`, this new `RootNode` will be used for the subsequent visitors instead of the original one. This allows us to chain visitors that modify the Codama IDL. Consider the following example such that the `./delete-all-accounts.js` visitor returns a new IDL with all accounts removed it.
146
+
147
+
```js
148
+
exportdefault {
149
+
scripts: {
150
+
documentation: [
151
+
'./delete-all-accounts.js', // After this visitor, the IDL has no accounts.
152
+
'./generate-documentation.js', // Generates documentation for an IDL with no accounts.
153
+
],
154
+
},
155
+
};
156
+
```
157
+
158
+
## Recipes
159
+
160
+
There are plenty of existing visitors that you can use in your Codama scripts. The "[Available visitors](https://github.com/codama-idl/codama?tab=readme-ov-file#available-visitors)" section of the Codama README aims to list as many of them as possible. This includes visitors maintained by various teams across the ecosystem.
161
+
162
+
The examples below show how to use some of these visitors in your configuration file. They make heavy use of the [`@codama/visitors`](https://github.com/codama-idl/codama/tree/main/packages/visitors/README.md) package which provides a large number of utility visitors.
163
+
164
+
### Deleting nodes
165
+
166
+
Returns a new IDL with the specified nodes removed.
167
+
168
+
See [`deleteNodesVisitor`](https://github.com/codama-idl/codama/tree/main/packages/visitors-core#deletenodesvisitor) and [node selectors](https://github.com/codama-idl/codama/tree/main/packages/visitors-core#selecting-nodes).
169
+
170
+
In the example below, we remove the `mint` account, the `initializeMint` instruction and all errors from the IDL.
0 commit comments