src/bin/codegraph.ts · uninstall.ts · node-version-check.tsCLI 入口用 commander 定義全部子指令。每個指令只是「把參數整理好 → 呼叫 CodeGraph class / installer → 把結果排版印出」的薄殼。serve --mcp 啟動 MCP server。bin/codegraph.js(dist)被 chmod 0o755 當 binary。node-version-check.ts 在 Node <20 或 ≥25 時硬性退出。
每個子指令與 CLI 參考一一對應。查詢類(query/callers/callees/impact/files/status)都支援 --json,給腳本用;explore 與 node 直接輸出與 MCP 工具相同的內容——非 MCP harness 的對等介面。
讓 agent 能以 command: "codegraph", args: ["serve", "--mcp"] 啟動。server 內部再處理 daemon 共享、connect-time catch-up、staleness banner 等(見 mcp)。
codegraph uninstall 走 installer 的反向:剝掉每個 agent 的 MCP config、instructions 段落與 permissions(--keep-cli 只移除設定);列出找到的每份安裝(standalone bundle / npm global / launcher link)再刪。codegraph uninit 是另一回事——只移除單一專案的 .codegraph/。
package.json engines 是 >=20.0.0 <25.0.0,這裡在啟動時強制檢查。CLI/MCP 跑在 bundle runtime(不受影響);從 source 跑需要 Node ≥22.5(node:sqlite)。
CLI 的真正威力不在互動查詢,而在可程式化。這裡是實務:把查詢類指令的 --json 輸出接進 shell 管線,產生一份「每個 controller 的受影響測試數」報表。
# ① 抓全部 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)。
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 found | npm 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 沒跑 uninstall | codegraph uninstall(含 --keep-cli 選項) |
--json?列出至少三個能串接 --json 的消費者(腳本、CI、別的 CLI…)。