src/bin — CLI 層

commander 子指令:install、init、index、sync、status、query、serve --mcp…
檔案:src/bin/codegraph.ts · uninstall.ts · node-version-check.ts

大方向

CLI 入口用 commander 定義全部子指令。每個指令只是「把參數整理好 → 呼叫 CodeGraph class / installer → 把結果排版印出」的薄殼。serve --mcp 啟動 MCP server。bin/codegraph.js(dist)被 chmod 0o755 當 binary。node-version-check.ts 在 Node <20 或 ≥25 時硬性退出。


commander 指令表 src/bin/codegraph.tsinstall / init / uninit / index / sync / status / unlock / query / explore / node / files / callers / callees / impact / affected / daemon / telemetry / upgrade / version。

每個子指令與 CLI 參考一一對應。查詢類(query/callers/callees/impact/files/status)都支援 --json,給腳本用;explore 與 node 直接輸出與 MCP 工具相同的內容——非 MCP harness 的對等介面。

serve --mcp src/bin/codegraph.ts啟動 MCP server(agent 透過 stdio 啟動它)。

讓 agent 能以 command: "codegraph", args: ["serve", "--mcp"] 啟動。server 內部再處理 daemon 共享、connect-time catch-up、staleness banner 等(見 mcp)。

uninstall.ts src/bin/反安裝器:從所有 agent 移除設定,可保留 CLI。

codegraph uninstall 走 installer 的反向:剝掉每個 agent 的 MCP config、instructions 段落與 permissions(--keep-cli 只移除設定);列出找到的每份安裝(standalone bundle / npm global / launcher link)再刪。codegraph uninit 是另一回事——只移除單一專案的 .codegraph/。

node-version-check src/bin/Node 範圍強制:≥20 <25,否則硬退出。

package.json engines 是 >=20.0.0 <25.0.0,這裡在啟動時強制檢查。CLI/MCP 跑在 bundle runtime(不受影響);從 source 跑需要 Node ≥22.5(node:sqlite)。

看完這頁你應該能說出:CLI 是 CodeGraph class / installer 的薄殼、explore/node 與 MCP 工具輸出一致、uninstall 與 uninit 的差別、以及 Node 版本硬檢查的位置。

🔍 進階真實情境 Worked Example:用 --json 把 CLI 串成自訂管線

CLI 的真正威力不在互動查詢,而在可程式化。這裡是實務:把查詢類指令的 --json 輸出接進 shell 管線,產生一份「每個 controller 的受影響測試數」報表。

CLI 管線工作流
# ① 抓全部 controller 符號(名稱/檔案/行號)
codegraph query controller --kind class --json \
  | jq -r '.results[] | [.name, .file_path, .start_line] | @tsv'

# ② 對每個 controller 算影響半徑(JSON 進、JSON 出)
for sym in $(codegraph query controller --kind class --json | jq -r '.results[].name'); do
  n=$(codegraph impact "$sym" --depth 2 --json | jq '.affected | length')
  printf '%s\t%s\n' "$sym" "$n"
done | sort -k2 -rn | head -10

# ③ 接進 CI:只有「受影響測試」存在時才跑
git diff --name-only | codegraph affected --stdin --quiet | xargs -r npx vitest run

為什麼走這條路徑:每個查詢類指令(query/callers/callees/impact/files/status)都支援 --json——這是與腳本世界接軌的正式契約。CLI 薄殼保證「JSON 結構 = CodeGraph class 的資料結構」,所以管線不會被排版文字污染。能用腳本自動化的分析,就不該由人眼反覆執行。

🔬 深入原理擴充

內部架構:薄殼的分工。commander 定義全部子指令,每個指令只是「整理參數 → 呼叫 CodeGraph class / installer → 排版或 JSON 輸出」的薄殼。serve --mcp 啟動 MCP server;bin/codegraph.js 被 chmod 0o755 當 binary。uninstall.ts 走 installer 的反向——剝掉每個 agent 的 MCP config、instructions 段落與 permissions(--keep-cli 只移除設定);uninit 是另一回事,只移除單一專案的 .codegraph/。node-version-check.ts 在 Node <20 或 ≥25 時硬性退出(package.json engines 是 >=20.0.0 <25.0.0)——CLI/MCP 跑在 bundle runtime,從 source 跑才需要 Node ≥22.5(node:sqlite)。

大家以為分析正確但其實有誤:以為 CLI 有自己一套查詢邏輯。實際它是 CodeGraph class 的薄殼——你在 CLI 學到的行為,在 library 與 MCP 完全一致。另一個誤解:以為 codegraph uninstall 會刪掉 CLI binary。實際 --keep-cli 只移除 agent 設定,binary 還在;要連 binary 一起移除才需要更多步驟。

🩺 診斷式疑難排解表

症狀可能原因解決方案
node-version-check 硬退出Node <20 或 ≥25用 bundle runtime,或切換到支援的 Node 版本
剛裝完卻 command not foundnpm global bin 不在 PATH檢查 npm prefix -g 的 bin 目錄並加入 PATH
多份 codegraph 版本混亂global + local 安裝並存which -a codegraph 找出全部,統一保留一份
--json 輸出被 jq 解析失敗stderr 混入 stdout,或 exit code 非零加 2>/dev/null;先檢查 echo $?
uninstall 後設定還在只跑了 uninit 沒跑 uninstallcodegraph uninstall(含 --keep-cli 選項)

🧩 進階挑戰題

  1. 為什麼所有查詢類指令都支援 --json?列出至少三個能串接 --json 的消費者(腳本、CI、別的 CLI…)。
  2. explore 與 MCP 工具「輸出一致」的保證,對 harness 開發者有何意義?如果兩者輸出漂移,會發生什麼?
  3. node-version-check 在 Node ≥25 也硬退出——為什麼連 upper bound 都要限制?bundle runtime 如何繞過這個限制?