Skip to content
clercjsPublic

Latest commit

Β 

History

1,223 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Clerc Card

Clerc is a strongly-typed CLI framework for Node.js 24+, with built-in help, Cargo-style diagnostics, structured logging, shell completions, and MCP support.

Version CI Downloads/week License


Note

ESM-only, targeting Node.js >= 24.

Why Clerc

  • Signature-driven TypeScript types - Write "deploy <env> [region] -- [extra...]" and get ctx.args inferred as { env: string; region: string | undefined; extra: string[] } without as const. Typos in args or flags option keys fail at compile time.
  • Standard Schema v1 everywhere - Pass Zod, Valibot, ArkType, or built-in Types.Enum / Types.Integer / Types.Number / Types.String / Types.Regex directly to type. One schema drives runtime validation, TypeScript output types, --help placeholders, shell completion candidates, and MCP JSON Schemas.
  • Editorial --help, typo hints, and ctx.log built in - Every CLI gets aligned --help pages (command/flag groups, examples, notes), Cargo-style error diagnostics with Levenshtein suggestions (buid -> 'build', --prt -> '--port'), and a structured logger (step, box, spinner, task, streaming .line(), and JSON mode).
  • One API for apps and plugins - clerc() and plugin() share .command(), .flags(), .groups(), .middleware(), .onError(), and .use(). Onion middlewares inject typed ctx.state via next({ db }), clean up in try / finally, and support { enforce: "pre" | "post" }.
  • Shell completions and MCP server from one definition - Add @clerc/plugin-completions for Bash, Zsh, Fish, and PowerShell tab-completion. Add @clerc/plugin-mcp and <cli> mcp exposes your commands as Model Context Protocol tools and help pages as resources for AI agents.
  • In-memory testing without process.exit() - await cli.run(["deploy", "prod"], { terminal: createMemoryTerminal() }) captures stdout, stderr, and exitCode in memory without mutating globals or killing your test runner.

Quick Start

pnpm add clerc
import { Types, clerc } from "clerc";

const cli = clerc({
  name: "acme",
  version: "2.0.0",
  description: "Build and deploy TypeScript services",
});

cli.command("deploy <env> [region]", {
  aliases: "d",
  description: "Deploy service to target environment",
  args: {
    env: {
      type: Types.Enum("staging", "prod"),
      description: "Target environment",
    },
    region: {
      default: "us-east-1",
      description: "Cloud region",
    },
  },
  flags: {
    port: {
      type: Types.Integer({ min: 1, max: 65_535 }),
      short: "p",
      default: 3000,
      env: "PORT",
      description: "Server port",
    },
    dryRun: {
      type: Boolean,
      short: "n",
      description: "Preview without deploying",
    },
  },
  run(ctx) {
    // ctx.args.env    -> "staging" | "prod"
    // ctx.args.region -> string
    // ctx.flags.port  -> number
    ctx.log.step(
      `Deploying to ${ctx.args.env} (${ctx.args.region})`,
      `port=${ctx.flags.port}`,
    );

    if (ctx.flags.dryRun) {
      ctx.log.box.info("Dry run", "No remote resources were modified.");
    } else {
      ctx.log.success("Deployment complete");
    }
  },
});

await cli.runMain();

Running acme deploy --help renders:

acme deploy v2.0.0  alias: d
Deploy service to target environment
────────────────────────────────────────────────────────────────────────
USAGE   acme deploy [options] <env> [region]

ARGUMENTS
  <env>                    Target environment  [staging|prod]
  [region]                 Cloud region  [default: us-east-1]

OPTIONS
  -p, --port <1..65535>    Server port  [default: 3000] [env: PORT]
  -n, --dry-run            Preview without deploying

GLOBAL OPTIONS
  -q, --quiet              Suppress non-error output
  -v, --verbose            Enable verbose debug logging
  -h, --help               Show help

Mistyping a command or option prints actionable diagnostics:

$ acme deply
error unknown command 'deply'

  tip a similar command exists: 'deploy'

      For more information, try 'acme --help'.

Plugins, Typed State & MCP

Plugins encapsulate flags, subcommands, and middlewares, then compose onto any CLI with full type inference:

import { completionsPlugin } from "@clerc/plugin-completions";
import { mcpPlugin } from "@clerc/plugin-mcp";
import { clerc, plugin } from "clerc";

const dbPlugin = plugin("acme:db")
  .flags({
    databaseUrl: {
      type: String,
      env: "DATABASE_URL",
      default: "sqlite://local.db",
    },
  })
  .middleware(async (ctx, next) => {
    const db = await connectDb(ctx.flags.databaseUrl);
    try {
      return await next({ db });
    } finally {
      await db.close();
    }
  });

const cli = clerc({ name: "acme", version: "2.0.0" })
  .use(dbPlugin)
  .use(completionsPlugin()) // adds 'acme completions <shell>'
  .use(mcpPlugin()); // adds 'acme mcp' stdio server

cli.command("users list", {
  meta: { mcp: { readOnly: true } },
  async run(ctx) {
    // ctx.state.db is typed from dbPlugin
    const users = await ctx.state.db.query("SELECT id, email FROM users");
    ctx.log.info(`Found ${users.length} users`);
  },
});

Testing in Memory

import { createMemoryTerminal } from "clerc";
import { expect, test } from "vitest";

test("previews staging deploy", async () => {
  const terminal = createMemoryTerminal({ env: { PORT: "8080" } });
  const result = await cli.run(["deploy", "staging", "-n"], { terminal });

  expect(result.ok).toBe(true);
  expect(terminal.stdoutText()).toContain("port=8080");
});

Packages

Package Description
clerc Core framework: POSIX argv parser, clerc, plugin, help renderer, diagnostics, built-in -v/-q verbosity, Standard Schema v1, and Terminal.
@clerc/logger CLI logger with aligned word labels, node:util object/error formatting, streaming .line(), box, spinner, task, and JSON output.
@clerc/theme Zero-dependency semantic ANSI theme and terminal color/Unicode detection.
@clerc/plugin-completions Zero-dependency shell completions for Bash, Zsh, Fish, and PowerShell with async context-aware complete().
@clerc/plugin-mcp Zero-dependency MCP stdio server exposing CLI commands as tools and help pages as resources.
@clerc/plugin-update-notifier Zero-dependency background npm update checker and banner renderer.

Documentation & Examples

License

MIT. Made by Ray.

Releases

Packages

Used by

Contributors

Languages