Cli
Last updated:
API: CLI
The main CLI class for creating and managing command-line applications.
Creating a CLI Instance
Constructor
new Cerebro(name: string, options?: CliOptions)Parameters:
name- The CLI application nameoptions- Optional configuration
Example:
import { Cerebro } from "@visulima/cerebro";
const cli = new Cerebro("my-app", {
packageName: "my-app",
packageVersion: "1.0.0",
cwd: process.cwd(),
logger: console,
argv: process.argv.slice(2),
});Factory Function
import { createCerebro } from "@visulima/cerebro";
const cli = createCerebro("my-app", {
packageName: "my-app",
packageVersion: "1.0.0",
});CliOptions
interface CliOptions<T extends Console = Console> {
argv?: ReadonlyArray<string>;
cwd?: string;
logger?: T;
packageName?: string;
packageVersion?: string;
}Methods
addCommand()
Add a command to the CLI:
cli.addCommand(command: Command): thisExample:
cli.addCommand({
name: "build",
description: "Build the project",
execute: ({ logger }) => {
logger.info("Building...");
},
});addPlugin()
Add a plugin to extend functionality:
cli.addPlugin(plugin: Plugin): thisExample:
cli.addPlugin({
name: "my-plugin",
execute: (toolbox) => {
toolbox.myFeature = () => {};
},
});run()
Run the CLI application:
cli.run(options?: CliRunOptions): Promise<void>Options:
interface CliRunOptions {
shouldExitProcess?: boolean; // Default: true
autoDispose?: boolean; // Default: true
[key: string]: any; // Additional options
}Example:
await cli.run({
shouldExitProcess: false, // Don't exit process
autoDispose: false, // Don't cleanup
});runCommand()
Execute a command programmatically:
cli.runCommand(commandName: string, options?: RunCommandOptions): Promise<unknown>Options:
interface RunCommandOptions {
argv?: string[];
[key: string]: any;
}Example:
await runtime.runCommand("build", {
argv: ["--production"],
});setDefaultCommand()
Set the default command when none is provided:
cli.setDefaultCommand(commandName: string): thisExample:
cli.setDefaultCommand("help");setCommandSection()
Configure help output:
cli.setCommandSection(section: CommandSection): thisExample:
cli.setCommandSection({
header: "My CLI v1.0.0",
footer: "Visit https://example.com for more info",
});getCliName()
Get the CLI application name:
cli.getCliName(): stringgetCommands()
Get all registered commands:
cli.getCommands(): Map<string, Command>getCwd()
Get the current working directory:
cli.getCwd(): stringgetPackageName()
Get the package name:
cli.getPackageName(): string | undefinedgetPackageVersion()
Get the package version:
cli.getPackageVersion(): string | undefinedgetPluginManager()
Get the plugin manager instance:
cli.getPluginManager(): PluginManagerdispose()
Clean up resources:
cli.dispose(): voidComplete Example
import { Cerebro } from "@visulima/cerebro";
const cli = new Cerebro("my-app", {
packageName: "my-app",
packageVersion: "1.0.0",
});
cli.setCommandSection({
header: "My Awesome CLI v1.0.0",
});
cli.setDefaultCommand("help");
cli.addCommand({
name: "hello",
execute: ({ logger }) => {
logger.info("Hello!");
},
});
await cli.run();