Expose CLI commands as a programmatic library API - #512
Draft
tuler wants to merge 3 commits into
Draft
Conversation
Every command is now also a function, so applications can be built, run
and inspected from a script:
import { build, hash, run } from "@cartesi/cli";
The command actions were moved to `src/api`, and the Commander commands
became thin wrappers around them. The API functions take plain option
objects with defaults, return values instead of printing, throw instead
of exiting, and are silent unless `progress` is set to "default" or
"verbose", which reproduces the CLI output.
`src/lib.ts` is the new package entrypoint, bundled to `dist/lib.js` and
typed by `dist/types`, generated with `tsc --emitDeclarationOnly`. The
`bin` entrypoint is unchanged.
While rewiring the run command, "build and redeploy" now builds with the
configuration files given to `run`, instead of always `cartesi.toml`.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015SA64hXXCsNWisTYLVpEBn
🦋 Changeset detectedLatest commit: 33b100c The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
Contributor
Coverage Report
📁 File Coverage (20 files)
|
The doctor API now runs every check in parallel and returns all results, failed and successful, instead of stopping at the first failure and reporting progress through callbacks. The command renders the results after they are gathered. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015SA64hXXCsNWisTYLVpEBn
Adds `depositEther`, `depositErc20`, `depositErc721`, `depositErc1155` and `depositErc1155Batch` to the library API, moving the deposit logic out of the commands. The commands keep the prompts, and pass everything resolved to the API. Amounts are given in the base unit of the asset as a bigint, or in its display unit as a string. Deposits that cannot be made now throw `DepositError` subclasses instead of printing and returning, which the commands render the same way as before. The connection setup shared by `send` and the deposits (client, sender and application address) moved to `resolveConnection`. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015SA64hXXCsNWisTYLVpEBn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR refactors the Cartesi CLI to expose all commands as a programmatic library API, allowing developers to use CLI functionality directly from TypeScript/JavaScript code without invoking the command-line interface.
Key Changes
New API modules (
apps/cli/src/api/): Created modular API functions for all commands:build.ts- Build application drives and machine snapshotrun.ts- Start local Cartesi rollups environmentsend.ts- Send inputs to deployed applicationsdeposit/- Deposit assets:depositEther,depositErc20,depositErc721,depositErc1155,depositErc1155Batchlogs.ts- Stream environment logshash.ts- Get machine snapshot hashstatus.ts- Query environment statusshell.ts- Boot machine in interactive modedoctor.ts- Check system requirementscreate.ts- Create new applications from templatesclean.ts- Clean build artifactsaddress-book.ts- Get deployed contract addressesconnection.ts- Shared resolution of client, sender and application addresstypes.ts- Shared types and configuration resolutionCommand refactoring: Updated all command files to delegate to the new API modules, eliminating code duplication. Commands keep the interactive prompts and pass resolved values to the API.
Library exports:
src/lib.tsas the main library entrypointpackage.jsonto export both CLI (dist/index.js) and library (dist/lib.js) entrypointstsconfig.build.jsonEnhanced wallet support: Added
interactiveparameter togetRpcUrl()for programmatic use casesTest coverage: Added unit tests for
encodeInput(),resolveConfig(),parseAmount()and the deposit error hierarchyDocumentation: Updated README with library usage examples
Notable Implementation Details
OptionsandResulttypessilent,default,verbose) for library useConfigobjects or file pathsrun()function returns aRunResultobject with methods to deploy/undeploy applications and stop the environmentprogressoption is specifieddoctor()runs every check concurrently and returns all results, failed and successful, instead of stopping at the first failurebigint, or in its display unit as a string ("1.5"); the token defaults to the corresponding devnet test tokenDepositErrorsubclasses (InsufficientBalanceError,InvalidAmountError,TokenNotFoundError) instead of printing and returning; the commands render them as beforeBehavior Changes
run: "Build and redeploy" now builds with the configuration files given torun -c, instead of alwayscartesi.tomldeposit erc721: a failed ownership check that is notERC721NonexistentTokennow stops instead of reporting the failure and continuingdeposit erc1155-batch: the token-ids/amounts length mismatch is reported after the application address prompt, rather than before itTesting
Type checking, linting and the 153 unit tests pass. A sample consumer was verified to type-check against the generated declarations under both
bundlerandnodenextmodule resolution, and importingdist/lib.jswas verified to have no CLI side effects. Docker was not available in the development environment, so the docker-dependent paths (build,run,logs, deposits) were not exercised end to end and rely on CI.https://claude.ai/code/session_015SA64hXXCsNWisTYLVpEBn