Skip to content

Conversation

@lorisleiva
Copy link
Member

@lorisleiva lorisleiva commented Jan 30, 2025

This PR adds a brand new @codama/cli package which is then imported (not re-exported) by the main codama library in order to use the codama keyword as CLI binary.

The CLI currently supports the following two commands:

  • codama init: which initialises a Codama configuration file by using prompts and selected script presets.
  • codama run: which runs any script defined in your configuration file.

You can learn more about the CLI commands and the Codama config file from the @codama/cli README copy/pasted below:


This package provides a CLI for the Codama library that can be used to run scripts on Codama IDLs.

Note that, whilst the CLI code is located in the @codama/cli package, the CLI binary is directly provided by the main codama library.

Getting started

To get started with Codama, simply install codama to your project and run the init command like so:

pnpm install codama
codama init

You will be prompted for the path of your IDL and asked to select any script presets you would like to use.

codama run

Once you have your codama config file, you can run your Codama scripts using the codama run command as follows:

codama run         # Only runs your before visitors.
codama run js rust # Runs your before visitors followed by the `js` and `rust` scripts.
codama run --all   # Runs your before visitors followed by all your scripts.

The configuration file

The codama config file defines an object containing the following fields:

  • 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.
  • before (array): An array of visitors that will run before every script.
  • 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.

Whether it is in the before array or in the scripts values, when defining a visitor you may either provide:

  • an object with the following fields:
    • from (string): The import path to the visitor.
    • args (array): An array of arguments to pass to the visitor.
  • a string: The import path to the visitor. This is equivalent to providing an object with a from field and an empty args array.

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 nammed 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:

'./my-visitor.js'; // Relative local path to a visitor module.
'/Users/me/my-visitor.js'; // Absolute local path to a visitor module.
'some-library'; // npm package name.
'@acme/some-library'; // Scoped npm package name.
'./my-visitor.js#myExport'; // Named export from a local path.
'@acme/some-library#myExport'; // Named export from an npm package.

Here is an example of what a Codama configuration file might look like:

{
    "idl": "path/to/idl",
    "before": [
        "./my-before-visitor.js",
        { "from": "some-library#removeTypes", "args": [["internalFoo", "internalBar"]] }
    ],
    "scripts": {
        "js": [
            {
                "from": "@codama/renderers-js",
                "args": ["clients/js/src/generated"]
            }
        ]
    }
}

Note that you can use the --js flag to generate a .js configuration file when running the init command.

@changeset-bot
Copy link

changeset-bot bot commented Jan 30, 2025

🦋 Changeset detected

Latest commit: dcf0aa2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 17 packages
Name Type
@codama/renderers-vixen-parser Patch
@codama/renderers-js-umi Patch
@codama/renderers-rust Patch
@codama/renderers-js Patch
codama Patch
@codama/cli Patch
@codama/renderers Patch
@codama/errors Patch
@codama/node-types Patch
@codama/nodes Patch
@codama/validators Patch
@codama/visitors-core Patch
@codama/visitors Patch
@codama/dynamic-codecs Patch
@codama/dynamic-parsers Patch
@codama/nodes-from-anchor Patch
@codama/renderers-core Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Member Author

lorisleiva commented Jan 30, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

@lorisleiva lorisleiva force-pushed the 01-29-add_codama_cli branch 2 times, most recently from 2ec7616 to 1159ce7 Compare January 30, 2025 19:48
@lorisleiva lorisleiva marked this pull request as ready for review January 30, 2025 19:51
@lorisleiva lorisleiva force-pushed the 01-29-add_codama_cli branch from 1159ce7 to bce00d1 Compare January 30, 2025 19:58
@lorisleiva lorisleiva requested a review from febo January 30, 2025 23:26
@lorisleiva lorisleiva force-pushed the 01-29-add_codama_cli branch 2 times, most recently from 3b77810 to 14a98bb Compare January 31, 2025 13:10
@lorisleiva lorisleiva changed the base branch from 01-28-update_web3.js_v2_urls to graphite-base/425 January 31, 2025 13:18
Copy link
Contributor

@febo febo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great – just found a tiny typo.

Copy link
Member Author

lorisleiva commented Jan 31, 2025

Merge activity

  • Jan 31, 12:48 PM EST: A user started a stack merge that includes this pull request via Graphite.
  • Jan 31, 12:50 PM EST: Graphite rebased this pull request as part of a merge.
  • Jan 31, 12:51 PM EST: A user merged this pull request with Graphite.

@lorisleiva lorisleiva changed the base branch from graphite-base/425 to main January 31, 2025 17:49
@lorisleiva lorisleiva force-pushed the 01-29-add_codama_cli branch from 3e80075 to dcf0aa2 Compare January 31, 2025 17:50
@lorisleiva lorisleiva merged commit 7bb6920 into main Jan 31, 2025
3 checks passed
@lorisleiva lorisleiva deleted the 01-29-add_codama_cli branch January 31, 2025 17:51
@github-actions github-actions bot mentioned this pull request Jan 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants