CerebroToolboxRuntime

Runtime

Last updated:

Toolbox: Runtime

The runtime property provides access to the CLI instance, allowing you to call other commands programmatically.

Accessing Runtime

execute: ({ runtime }) => {
    // runtime is the CLI instance
    const cliName = runtime.getCliName();
    const commands = runtime.getCommands();
};

Calling Other Commands

Use runtime.runCommand() to execute other commands:

cli.addCommand({
    name: "deploy",
    execute: async ({ runtime, logger }) => {
        logger.info("Building...");
        await runtime.runCommand("build", {
            argv: ["--production"],
        });

        logger.info("Deploying...");
    },
});

Passing Arguments

await runtime.runCommand("copy", {
    argv: ["file1.txt", "file2.txt"],
});

Passing Options

await runtime.runCommand("build", {
    argv: ["--production", "--output", "dist"],
});

Merging Options

await runtime.runCommand("build", {
    argv: ["--production"],
    customOption: "value",
});

Getting Return Values

const result = await runtime.runCommand("calculate");
console.log(result); // Command return value

Runtime Methods

getCliName()

Get the CLI application name:

const name = runtime.getCliName(); // "my-app"

getCommands()

Get all registered commands:

const commands = runtime.getCommands(); // Map<string, Command>
commands.forEach((command, name) => {
    console.log(`Command: ${name}`);
});

getCwd()

Get the current working directory:

const cwd = runtime.getCwd(); // "/path/to/project"

getPackageName()

Get the package name if configured:

const packageName = runtime.getPackageName(); // "my-app" | undefined

getPackageVersion()

Get the package version if configured:

const version = runtime.getPackageVersion(); // "1.0.0" | undefined

getPluginManager()

Get the plugin manager instance:

const pluginManager = runtime.getPluginManager();
// Advanced plugin management

Command Composition Examples

Build Pipeline

cli.addCommand({
    name: "ci",
    execute: async ({ runtime, logger }) => {
        logger.info("Running CI pipeline...");

        await runtime.runCommand("lint");
        await runtime.runCommand("test", { argv: ["--coverage"] });
        await runtime.runCommand("build", { argv: ["--production"] });

        logger.info("CI pipeline complete!");
    },
});

Conditional Execution

cli.addCommand({
    name: "release",
    options: [{ name: "skip-tests", type: Boolean }],
    execute: async ({ runtime, options, logger }) => {
        if (!options.skipTests) {
            logger.info("Running tests...");
            await runtime.runCommand("test");
        }

        logger.info("Building release...");
        await runtime.runCommand("build", { argv: ["--production"] });
    },
});

Nested Command Calls

cli.addCommand({
    name: "deploy",
    execute: async ({ runtime }) => {
        await runtime.runCommand("build");
    },
});

cli.addCommand({
    name: "build",
    execute: async ({ runtime }) => {
        await runtime.runCommand("compile");
        await runtime.runCommand("bundle");
    },
});

Error Handling

Errors from called commands propagate:

cli.addCommand({
    name: "deploy",
    execute: async ({ runtime, logger }) => {
        try {
            await runtime.runCommand("build");
        } catch (error) {
            logger.error("Build failed:", error);
            throw error; // Re-throw to stop deployment
        }

        await runtime.runCommand("deploy-step");
    },
});

Best Practices

  1. Use for composition - Break complex operations into smaller commands
  2. Handle errors - Wrap runCommand calls in try-catch when needed
  3. Pass options explicitly - Use argv array for clarity
  4. Avoid circular calls - Don't create command call loops
  5. Document dependencies - Make it clear which commands depend on others
Support

Contribute to our work and keep us going

Community is the heart of open source. The success of our packages wouldn't be possible without the incredible contributions of users, testers, and developers who collaborate with us every day.Want to get involved? Here are some tips on how you can make a meaningful impact on our open source projects.

Ready to help us out?

Be sure to check out the package's contribution guidelines first. They'll walk you through the process on how to properly submit an issue or pull request to our repositories.

Submit a pull request

Found something to improve? Fork the repo, make your changes, and open a PR. We review every contribution and provide feedback to help you get merged.

Good first issues

Simple issues suited for people new to open source development, and often a good place to start working on a package.
View good first issues