Rush Stack商店部落格活動
跳到主要內容

自訂命令

如果您的工具鏈具有特殊模式或功能,您可以將這些公開為 Rush 工具的自訂命令或參數。

定義自訂命令和參數

這些定義在組態檔 common/config/rush/command-line.json 中。您的組態檔應符合 command-line.schema.json 結構描述。請參考此範例

{
"$schema": "https://developer.microsoft.com/json-schemas/rush/v5/command-line.schema.json",

"commands": [
{
/**
* (Required) Determines the type of custom command.
* Rush's "bulk" commands are invoked separately for each project. Rush will look in
* each project's package.json file for a "scripts" entry whose name matches the
* command name. By default, the command will run for every project in the repo,
* according to the dependency graph (similar to how "rush build" works).
* The set of projects can be restricted e.g. using the "--to" or "--from" parameters.
*/
"commandKind": "bulk",
"name": "import-strings",
"summary": "Imports translated strings into each project.",
"description": "Requests translated strings from the translation service and imports them into each project.",
"enableParallelism": true
},
{
/**
* (Required) Determines the type of custom command.
* Rush's "global" commands are invoked once for the entire repo.
*/
"commandKind": "global",

"name": "deploy-app",
"summary": "Deploys the application",
"description": "Run this command to deploy the application",

"shellCommand": "node common/scripts/deploy-app.js"
}
],

"parameters": [
{
/**
* (Required) Determines the type of custom parameter.
* A "flag" is a custom command-line parameter whose presence acts as an on/off switch.
*/
"parameterKind": "flag",
"longName": "--ship",
"shortName": "-s",
"description": "Perform a production build, including minification and localization steps",
"associatedCommands": [ "build", "rebuild", "import-strings" ],
},

{
"parameterKind": "flag",
"longName": "--minimal",
"shortName": "-m",
"description": "Perform a fast build, which disables certain tasks such as unit tests and linting",
"associatedCommands": [ "build", "rebuild" ]
},
{
/**
* (Required) Determines the type of custom parameter.
* "A "choice" is a custom command-line parameter whose argument must be chosen from a list
* of allowable alternatives.
*/
"parameterKind": "choice",
"longName": "--locale",
"description": "Selects a single instead of the default locale (en-us) for non-ship builds or all locales for ship builds.",
"associatedCommands": [ "build", "rebuild", "import-strings" ],
"alternatives": [
{
"name": "en-us",
"description": "US English"
},
{
"name": "fr-fr",
"description": "French (France)"
},
{
"name": "es-es",
"description": "Spanish (Spain)"
},
{
"name": "zh-cn",
"description": "Chinese (China)"
}
]
}
]
}

自訂命令:您可以定義自己的命令,這些命令與 Rush 的內建命令動詞(例如 rush buildrush check 等)類似。有兩種

  • 大量命令:這些命令會針對儲存庫中的每個專案個別執行,類似於 rush build 的運作方式。如果您設定 "enableParallelism": true,則可以平行處理專案。大量命令可以執行每個專案的 package.json 檔案中定義的指令碼,或由(選用)shellCommand 欄位指定的單一指令碼檔案。

  • 全域命令:這些命令會針對整個儲存庫執行一次,方法是執行由(必要)shellCommand 欄位指定的單一指令碼檔案。

您也可以定義自己的命令列「參數」。參數可以透過其 associatedCommands 清單與一個或多個命令關聯。您甚至可以將您的自訂參數與 Rush 自己的內建 buildrebuild 命令關聯。在上面的範例中,我們將 --ship 參數與 rush buildrush rebuild 和我們的自訂 rush import-strings 關聯。

目前支援三種類型的 parameterKind

  • 旗標參數: 「旗標」是一個簡單的開關,例如 --production
  • 字串參數: 包含文字字串引數的參數,例如 --title "Hello, world!"
  • 字串清單參數: 可以多次指定的字串參數,例如 --category docs --category dashboard
  • 選擇參數: 類似於字串,但引數必須來自支援替代方案的清單,例如 --locale fr-fr
  • 整數參數: 包含整數引數的參數,例如 --pull-request 1234
  • 整數清單參數: 可以多次指定的整數參數,例如 --pr 1234 --pr 1235 --pr 1236

未來可能會支援更多參數類型。(它們是使用 ts-command-line 程式庫剖析的,該程式庫支援可以公開的其他參數類型。)

使用自訂命令和選項

您的自訂定義及其描述將會納入 Rush 的命令列說明中(當在您的儲存庫工作資料夾下叫用時)。繼續上述範例,如果我們執行 rush import-strings --help,我們現在會看到類似如下的內容

Rush Multi-Project Build Tool 5.1.0 - https://rush.dev.org.tw

usage: rush import-strings [-h] [-p COUNT] [-t PROJECT1]
[--to-version-policy VERSION_POLICY_NAME]
[-f PROJECT2] [-v] [-s]
[--locale {en-us,fr-fr,es-es,zh-cn}]

Requests translated strings from the translation service and imports them
into each project.

Optional arguments:
-h, --help Show this help message and exit.
-p COUNT, --parallelism COUNT
Specify the number of concurrent build processes The
value "max" can be specified to indicate the number
of CPU cores. If this parameter omitted, the default
value depends on the operating system and number of
CPU cores.
-t PROJECT1, --to PROJECT1
Run command in the specified project and all of its
dependencies
--to-version-policy VERSION_POLICY_NAME
Run command in all projects with the specified
version policy and all of their dependencies
-f PROJECT2, --from PROJECT2
Run command in all projects that directly or
indirectly depend on the specified project
-v, --verbose Display the logs during the build, rather than just
displaying the build status summary
-s, --ship Perform a production build, including minification
and localization steps
--locale {en-us,fr-fr,es-es,zh-cn}
Selects a single instead of the default locale
(en-us) for non-ship builds or all locales for ship
builds.

如何實作自訂命令/參數?對於全域命令,Rush 只會叫用其 shellCommand 並將參數傳遞過去。對於大量命令,Rush 可以選擇在您的 package.json 檔案中尋找對應的指令碼名稱。假設我們有類似以下的內容

example/package.json

{
"name": "example",
"version": "1.0.0",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"scripts": {
"import-strings": "./node_modules/.bin/loc-importer",
"build": "./node_modules/.bin/heft build"
}
}

如果我們執行 rush import-strings --locale fr-fr,則 Rush 會讀取「import-strings」指令碼主體並像這樣執行它

./node_modules/.bin/loc-importer --locale fr-fr

(Rush 直接使用您的 Shell 執行它;它不依賴 npm run。)由於這個選擇參數有預設值,如果我們執行 rush import-strings,則會像這樣執行 loc-importer

./node_modules/.bin/loc-importer --locale en-us

換句話說,Rush 的自訂參數只是附加到 package.json 指令碼主體。這表示如果您的指令碼主體使用不支援這些參數或需要將它們插入字串中間的 Shell 運算式,例如「rimraf ./lib && rimraf ./temp」,您可能會遇到問題。這是經過設計的:我們不建議在 JSON 字串內撰寫非平凡的建置指令碼。相反地,最好將此操作移到可以註解和檢閱的適當指令碼檔案中。隨著您的單一儲存庫成長,您可能也會想要將該指令碼移到可在專案之間共用的可重複使用程式庫中。

另請參閱