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.node.org.cn

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** 脚本主体。这意味着如果您的脚本主体使用诸如 “rimraf ./lib && rimraf ./temp” 之类的 shell 表达式(不支持这些参数),或者需要将它们插入字符串中间,您可能会遇到问题。这是有意为之:我们不建议在 JSON 字符串中编写非平凡的构建脚本。相反,最好将此操作移到可以注释和审查的适当脚本文件中。随着您的单一仓库的增长,您可能还需要将该脚本移到可以在项目之间共享的可重用库中。

另请参阅