Ant Design CLI
您有權使用 @ant-design/cli — 這是一個本機端命令列工具,內含 antd v4/v5/v6 的捆綁元數據(以及 v3 → v4、v4 → v5、v5 → v6 的遷移指南)。用它來查詢元件知識、分析專案以及引導遷移。所有資料均為離線,無需網路。
設定
首次使用前,請檢查 CLI 是否已安裝。若未安裝,請自動安裝:
which antd || npm install -g @ant-design/cli
執行任何命令後,如果輸出中包含「Update available」通知,請先執行 antd upgrade 更新後再繼續。
一律使用 --format json 以取得可供程式解析的結構化輸出。
情境
1. 編寫 antd 元件程式碼
在編寫任何 antd 元件程式碼之前,請先查閱其 API — 不要依賴記憶。
# 檢查有哪些屬性可用
antd info Button --format json
# 取得可作為起點的工作示範
antd demo Button basic --format json
# 檢查自訂樣式的語義 classNames/styles
antd semantic Button --format json
# 檢查主題的元件層級設計 Token
antd token Button --format json
# 取得整體設計語言(design.md):顏色、字體排印、間距、圓角 + 原則
antd design.md --format json
工作流程: antd info → 了解屬性 → antd demo → 取得工作範例 → 編寫程式碼。
2. 查閱完整文件
當您需要完整的元件文件(不僅是屬性)時:
antd doc Table --format json # Table 的完整 Markdown 文件
antd doc Table --lang zh # 中文文件
3. 除錯 antd 問題
當程式碼運作不如預期或使用者回報 antd 錯誤時:
# 收集完整的環境快照(系統、依賴套件、瀏覽器、建置工具)
antd env --format json
# 檢查該屬性在使用者的 antd 版本中是否存在
a ntd info Select --version 5.12.0 --format json
# 檢查該屬性是否已棄用
a ntd lint ./src/components/MyForm.tsx --format json
# 診斷專案層級的設定問題
a ntd doctor --format json
工作流程: antd env → 捕捉完整環境 → antd doctor → 檢查設定 → antd info --version X → 根據使用者確切版本驗證 API → antd lint → 找出已棄用或不正確的使用方式。
4. 在不同版本之間遷移
當使用者想要升級 antd(例如 v3 → v4 或 v4 → v5)時:
# 取得完整的遷移檢查清單
a ntd migrate 3 4 --format json # v3 → v4
a ntd migrate 4 5 --format json # v4 → v5
# 檢查特定元件的遷移
a ntd migrate 4 5 --component Select --format json
# 產生適合代理程式的自動遷移提示(不會修改檔案)
a ntd migrate 4 5 --apply ./src --format json
# 查看兩個版本之間的變更
a ntd changelog 4.24.0 5.0.0 --format json
# 查看特定元件的變更
a ntd changelog 4.24.0 5.0.0 Select --format json
工作流程: antd migrate → 取得完整檢查清單 → antd changelog <v1> <v2> → 了解重大變更 → 套用修正 → antd lint → 驗證無殘留已棄用用法。
5. 分析專案 antd 使用情況
當使用者想了解 antd 在其專案中的使用情況時:
# 掃描元件使用統計
antd usage ./src --format json
# 過濾至特定元件
antd usage ./src --filter Form --format json
# 檢查最佳實務違規
antd lint ./src --format json
# 僅檢查特定規則類別
antd lint ./src --only deprecated --format json
antd lint ./src --only a11y --format json
antd lint ./src --only performance --format json
6. 檢查變更日誌與版本歷史
當使用者詢問某個版本中的變更時:
# 特定版本的變更日誌
antd changelog 5.22.0 --format json
# 版本範圍(兩端包含)
antd changelog 5.21.0..5.24.0 --format json
7. 探索可用元件
當使用者正在選擇要使用哪個元件時:
# 列出所有元件及其類別
antd list --format json
# 列出特定 antd 版本的元件
antd list --version 5.0.0 --format json
8. 收集環境資訊
當您需要了解專案的 antd 設定,或為錯誤報告準備資訊時:
# 完整環境快照(純文字 — 貼入 GitHub Issues)
antd env
# 結構化 JSON 供程式使用
antd env --format json
# 掃描特定專案目錄
antd env ./my-project --format json
收集內容:作業系統、Node、套件管理器(npm/pnpm/yarn/bun/utoo)、npm registry、瀏覽器、核心相依套件(antd/react/dayjs)、所有 @ant-design/* 和 rc-* 套件,以及建置工具(umi/vite/webpack/typescript 等)。
9. 回報 antd 錯誤
當使用者要求您回報 antd 錯誤時:
# 步驟 0:收集環境資訊以供參考(可選 — antd bug 已內嵌基本環境)
# 使用輸出交叉檢查版本或將額外詳細資訊附加到錯誤報告中
antd env --format json
# 步驟 1:預覽供使用者審查
antd bug --title "DatePicker crashes when selecting date" \
--reproduction "https://codesandbox.io/s/xxx" \
--steps "1. Open DatePicker 2. Click a date" \
--expected "Date is selected" \
--actual "Component crashes with error" \
--format json
# 步驟 2:向使用者顯示,請求確認
# 步驟 3:使用者確認後提交
antd bug --title "DatePicker crashes when selecting date" \
--reproduction "https://codesandbox.io/s/xxx" \
--steps "1. Open DatePicker 2. Click a date" \
--expected "Date is selected" \
--actual "Component crashes with error" \
--submit
10. 回報 CLI 問題
當使用者要求您回報 CLI 錯誤,或明確請求協助提交錯誤報告時:
# 為使用者預覽錯誤報告
antd bug-cli --title "antd info Button returns wrong props for v5.12.0" \
--description "When querying Button props for version 5.12.0, the output includes props that don't exist in that version" \
--steps "1. Run: antd info Button --version 5.12.0 --format json" \
--expected "Props matching antd 5.12.0 Button API" \
--actual "Props include 'classNames' which was added in 5.16.0" \
--format json
在提交之前向使用者顯示報告並請求確認:
antd bug-cli --title "antd info Button returns wrong props for v5.12.0" \
--description "..." \
--steps "..." \
--expected "..." \
--actual "..." \
--submit
選擇退出: 如果環境變數 ANTD_NO_AUTO_REPORT=1 已設定,則完全跳過所有錯誤報告建議 — 除非使用者直接要求,否則不要建議使用 antd bug 或 antd bug-cli。
11. 升級 CLI
當使用者想要將 @ant-design/cli 更新至最新版本,或出現「Update available」通知時:
# 升級至最新版本(自動偵測套件管理器)
antd upgrade
該命令會偵測安裝 CLI 的套件管理器(npm、yarn、pnpm、bun、cnpm、utoo)並執行適當的升級命令。如果偵測失敗,它會建議手動命令。
12. 作為 MCP 伺服器使用
如果工作在支援 MCP 的 IDE(Claude Desktop、Cursor 等)中進行,CLI 也可以作為 MCP 伺服器執行,直接公開所有知識查詢工具:
{
"mcpServers": {
"antd": {
"command": "antd",
"args": ["mcp", "--version", "5.20.0"]
}
}
}
這會透過 MCP 協定提供 8 個工具(antd_list、antd_info、antd_doc、antd_demo、antd_token、antd_design_md、antd_semantic、antd_changelog)和 2 個提示(antd-expert、antd-page-generator)。
全域旗標
| 旗標 | 用途 |
|---|---|
--format <format> | 輸出格式:json、text 或 markdown(代理應偏好使用 json) |
--version <v> | 指定目標 antd 版本(例如 5.20.0) |
--lang zh | 中文輸出(預設:en) |
--detail | 包含額外欄位(description、since、deprecated、FAQ) |
-V, --cli-version | 列印 CLI 版本並結束 |
關鍵規則
- 先查詢再編寫 — 不要憑記憶猜測 antd API。請先執行
antd info。 - 符合使用者版本 — 知識查詢(
list/info/doc/demo/token/semantic/changelog)支援 antd v4+。如果專案使用 antd 4.x/5.x/6.x,請傳入--version 4.24.0/5.24.0/6.x。對於 antd v3 專案,請先使用antd migrate 3 4。 - 使用
--format json— 每個命令都支援此選項。請解析 JSON 輸出,而不是使用正則表達式比對文字輸出。 - 建議遷移前先檢查 — 在提供版本升級建議之前,請執行
antd changelog <v1> <v2>和antd migrate。 - 變更後執行 Lint — 在編寫或修改 antd 程式碼後,請對已變更的檔案執行
antd lint以捕捉已棄用或有問題的用法。 - 回報 antd 錯誤 — 當使用者要求回報 antd 錯誤時,請使用
antd bug。務必先預覽,取得使用者確認後再提交。 - 回報 CLI 問題 — 當使用者詢問 CLI 問題時,請使用
antd bug-cli協助他們提交報告。務必先預覽,取得使用者確認後再提交。