跳到正文
DEV Community· VANSH ARORA·· 3 小時前AI 評分28

TokenCap 如何在不破壞相容性的前提下,將 2,400 行 CLI 單體重構為 7 個核心動詞

How We Refactored a 2,400-Line CLI Monolith into 7 Core Verbs Without Breaking Changes

AI 導讀

TokenCap 將 CLI 從單一 2,455 行的 `bin/tokencap.js` 重構為以 7 個核心動詞組織的模組化架構,涵蓋快照生成、查詢、分析、服務、更新及系統資訊。透過別名策略保留原有腳本和 CI 工作流程的相容性後,dispatcher 縮減至 234 行。

正文

VANSH ARORA

As TokenCap expanded from a simple repository snapshot generator to a full intelligence engine with graphs, memory loops, and security scanners, our CLI sprawled out of control. We had 21 distinct commands in a single 2,455-line bin/tokencap.js file.

To fix this, we completed a major CLI refactoring around 7 core verbs.

The 7 Core Verbs Hierarchy

  1. make (src/cli/make.js): Snapshot generation, watch mode, custom budgeting profiles.
  2. ask (src/cli/ask.js): Spatial queries (ask impact, ask dead-code, ask adr).
  3. analyze (src/cli/analyze.js): Repo audits (analyze health, analyze debt, analyze security).
  4. serve (src/cli/serve.js): Background daemons (serve --daemon, serve --mcp).
  5. update (src/cli/update.js): In-place upgrades.
  6. help & version: System introspection.

The Silent Alias Strategy

To guarantee that existing scripts and CI workflows did not break:

// bin/tokencap.js excerpt
const ALIASES = {
  graph: ["ask", "impact"],
  capsule: ["make"],
  security: ["analyze", "security"],
  health: ["analyze", "health"],
  mcp: ["serve", "--mcp"]
};

const normalizedArgs = resolveAliases(process.argv.slice(2));

The dispatcher shrank from 2,455 lines down to 234 lines of clean routing logic, with handlers decoupled into modular, testable files in src/cli/.

Inspect the CLI architecture at tokencap.vansharora.app

來源:DEV Community · dev.to