From 01ea1090e79d5034784ef0b7a80cd7494bee9410 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Mon, 18 May 2026 16:05:51 +0700 Subject: [PATCH] replace Commander.js with CAC in CLI code Resolves #1088 Co-Authored-By: Claude Sonnet 4.6 Signed-off-by: Alfi Maulana --- CLAUDE.md | 2 +- README.md | 2 +- package.json | 2 +- pnpm-lock.yaml | 18 +++++++++--------- src/bin.ts | 25 +++++++++++++++---------- 5 files changed, 27 insertions(+), 22 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 34665933..81c4934d 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -30,7 +30,7 @@ pnpm start # Run CLI directly via jiti (no compile step needed) This is a minimal Node.js library + CLI starter template written in TypeScript targeting Node 24 (ESM). - **`src/lib.ts`** — The library's public API (currently a Fibonacci sequence generator as a placeholder example). -- **`src/bin.ts`** — CLI entry point built with Commander.js that wraps the library. +- **`src/bin.ts`** — CLI entry point built with CAC that wraps the library. - **`src/*.test.ts`** — Vitest test files co-located with source. ### Build outputs diff --git a/README.md b/README.md index 5174e3e0..87d0e175 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ A minimal [Node.js](https://nodejs.org/en) project template written in [TypeScri ## What's Included -- **TypeScript** library with [ESM](https://nodejs.org/api/esm.html) support and a CLI entry point using [Commander.js](https://github.com/tj/commander.js) +- **TypeScript** library with [ESM](https://nodejs.org/api/esm.html) support and a CLI entry point using [CAC](https://github.com/cacjs/cac) - **[pnpm](https://pnpm.io/)** as the package manager - **Formatting** with [Prettier](https://prettier.io/) and **linting** with [ESLint](https://eslint.org/) - **Testing** with [Vitest](https://vitest.dev/) — 100% code coverage required diff --git a/package.json b/package.json index 215eb332..88f10395 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "test": "vitest run" }, "dependencies": { - "commander": "^14.0.3" + "cac": "^7.0.0" }, "devDependencies": { "@eslint/js": "^10.0.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1cebc753..11169910 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,9 +8,9 @@ importers: .: dependencies: - commander: - specifier: ^14.0.3 - version: 14.0.3 + cac: + specifier: ^7.0.0 + version: 7.0.0 devDependencies: '@eslint/js': specifier: ^10.0.1 @@ -574,14 +574,14 @@ packages: resolution: {integrity: sha512-VZznLgtwhn+Mact9tfiwx64fA9erHH/MCXEUfB/0bX/6Fz6ny5EGTXYltMocqg4xFAQZtnO3DHWWXi8RiuN7cQ==} engines: {node: 18 || 20 || >=22} + cac@7.0.0: + resolution: {integrity: sha512-tixWYgm5ZoOD+3g6UTea91eow5z6AAHaho3g0V9CNSNb45gM8SmflpAc+GRd1InC4AqN/07Unrgp56Y94N9hJQ==} + engines: {node: '>=20.19.0'} + chai@6.2.1: resolution: {integrity: sha512-p4Z49OGG5W/WBCPSS/dH3jQ73kD6tiMmUM+bckNK6Jr5JHMG3k9bg/BvKR8lKmtVBKmOiuVaV2ws8s9oSbwysg==} engines: {node: '>=18'} - commander@14.0.3: - resolution: {integrity: sha512-H+y0Jo/T1RZ9qPP4Eh1pkcQcLRglraJaSLoyOtHxu6AapkjWVCy2Sit1QQ4x3Dng8qDlSsZEet7g5Pq06MvTgw==} - engines: {node: '>=20'} - cross-spawn@7.0.6: resolution: {integrity: sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==} engines: {node: '>= 8'} @@ -1465,9 +1465,9 @@ snapshots: dependencies: balanced-match: 4.0.4 - chai@6.2.1: {} + cac@7.0.0: {} - commander@14.0.3: {} + chai@6.2.1: {} cross-spawn@7.0.6: dependencies: diff --git a/src/bin.ts b/src/bin.ts index 0fdff87e..f2b7871c 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -1,15 +1,20 @@ #!/usr/bin/env node -import { program } from "commander"; +import cac from "cac"; import { fibonacciSequence } from "./lib.js"; -program - .name("my_fibonacci") - .version("0.0.0") - .description("Generate a Fibonacci sequence up to the given number of terms.") - .argument("", "The number of terms", parseInt) - .action((n: number) => { - const sequence = fibonacciSequence(n); +const cli = cac("my_fibonacci"); + +cli + .command( + "", + "Generate a Fibonacci sequence up to the given number of terms.", + ) + .action((n: string) => { + const sequence = fibonacciSequence(parseInt(n)); process.stdout.write(`${sequence.join(" ")}\n`); - }) - .parse(); + }); + +cli.version("0.0.0"); +cli.help(); +cli.parse();