Skip to content

How to add a command to the CLI

dia-scripts is extended by adding a command module and registering it. This guide adds one. See the architecture for how the CLI is wired.

Write the command module

A command module exports the yargs quartet — command, desc, builder, handler:

// scripts/dia-scripts/commands/greet.mjs
import { printHeader } from "../utils/log.mjs";

export const command = "greet";
export const desc = "Print a friendly greeting";

export const builder = function builder(yargs) {
    // The root disables help globally, so a top-level command re-enables its own with .help().
    return yargs
        .option("name", {
            type: "string",
            default: "world",
            describe: "Who to greet",
        })
        .help();
};

export const handler = async function handler(argv) {
    printHeader(`Hello, ${argv.name}!`);
};

Use the shared library rather than raw console/process: utils/log.mjs for output, sf-cli.mjs's runSalesforceCommand for sf calls, utils/env.mjs for anything environmental.

Register it

Add an explicit static import and .command() call in cli/run-script.mjs:

import * as greet from "../commands/greet.mjs";
// …
    .command(greet)

Registration is always an explicit .command() call — see commands are registered explicitly.

Add it to a group (optional)

To make it mygroup mycommand instead of top-level, register it in the group module's builder (e.g. commands/ci.mjs) rather than in run-script.mjs. Give the group handler = unknownCommandHandler and .fail(groupFailHandler) from cli/command-group.mjs so an unknown subcommand exits cleanly.

Add an npm alias

Keep the pipeline surface stable by exposing the command through package.json:

"greet": "node ./scripts/dia-scripts/index.mjs greet"

Verify

node scripts/dia-scripts/index.mjs greet --help
node scripts/dia-scripts/index.mjs greet --name DIA
npm run lint
npm run format:check