Skip to main content

脚本

环境文件

¥Environment files

Yarn 将自动解释 .env.yarn 文件的内容,并将它们注入 yarn run 运行的所有命令中。可以通过使用 injectEnvironmentFiles 设置来配置此行为。

¥Yarn will automatically interpret the content of .env.yarn files, and inject them within all commands run by yarn run. This behavior can be configured through the use of the injectEnvironmentFiles setting.

请注意,你还可以配置将加载的文件,但前提是这些文件存在;使得可以编写条件导入,例如:

¥Note that you can also configure files that will be loaded but only if present; making it possible to write conditional imports such as:

injectEnvironmentFiles:
- .env.yarn
- .env.local?

其中 .env.local 仅在存在时优先,否则将被忽略。

¥Where .env.local will take precedence only if present, and ignored otherwise.

并行任务

¥Parallel tasks

Yarn 在 scripts 字段中定义的脚本中具有针对后台作业语法 (foo&) 的原生集成。它将并行运行它们,并且输出的每一行都将以标识符为前缀,显示它们来自何处。例如,以下命令将并行运行 linting 和测试,并在两者完成后报告:

¥Yarn has a native integration for the background job syntax (foo&) within scripts defined in the scripts field. It will run them in parallel, and each line of the output will be prefixed by an identifier showing where they come from. For example, the following command will run linting and tests in parallel, and report once both are finished:

$ yarn lint & yarn test

可移植 shell

¥Portable shell

Windows 可移植性在使用其他包管理器时可能会很麻烦。脚本不能指望 Posix shell 可用,因此你必须依靠奇怪的黑客来使用半​​可移植脚本 - 或者将它们全部删除,改用 Node.js 脚本,首先违背小型、非侵入式脚本的意义。也就是说,除非你使用 Yarn!

¥Windows portability can be troublesome with other package managers. Scripts can't expect a Posix shell to be available, so you have to rely on strange hacks to use semi-portable scripts - or drop them altogether and use Node.js scripts instead, defeating the point of small, non-intrusive scripts in the first place. That is, unless you use Yarn!

Yarn 实现并维护一个类似 Posix 的 shell 解释器,它支持你通常在脚本中找到的所有语法,以及一些简单的内置函数,如 cd/echo。例如,尽管分配了环境变量,以下命令在 Windows 和 Linux 上都能正常工作:

¥Yarn implements and maintains a Posix-like shell interpreter that supports all of the syntax you typically find within scripts, along with a couple of simple builtins like cd / echo. For instance, the following command will work just fine on both Windows and Linux despite assigning environment variables:

$ NODE_ENV=production webpack
信息

我们说它是一个类似 Posix 的解释器,而不是与 Posix 兼容的解释器,因为它没有实现一些在 scripts 字段上下文中没用的最复杂的功能。例如,不支持多行流控制结构(如 while)。

¥We say it's a Posix-like interpreter rather than a Posix-compatible interpreter because it doesn't implement some of the most complex features that aren't useful in the context of the scripts field. For instance, multi-line flow control structures such as while aren't supported.

脚本参数

¥Script arguments

默认情况下,yarn run 中脚本名称后面的任何参数都将添加到评估的命令中。它在简单情况下工作得很好,例如在这种情况下,运行 yarn test packages/lib 将导致 jest packages/lib 被评估:

¥By default, any parameter following the name of the script in yarn run will be added to the evaluated command. It works well enough in simple cases, like this one where running yarn test packages/lib will lead to jest packages/lib being evaluated:

{
"scripts": {
"test": "jest"
}
}

但是,在更复杂的情况下,你可能需要明确引用这些参数。Yarn 通过使用 $0$1 等变量来支持这一点,这些变量将被插入并禁用参数的自动附加:

¥In more complex situations however, you may want to explicitly refer to those arguments. Yarn supports this by using the $0, $1, etc variables, which will be interpolated and will disable the automatic appending of the arguments:

{
"scripts": {
"get-latest": "curl https://registry.yarnpkg.com/$0 | jq .['dist-tags'].latest"
}
}

当然,你也可以使用 $@ 数组来检索所有参数的列表,这将允许你在多个命令中重复使用它们:

¥Of course, you can also use the $@ array to retrieve the list of all arguments, which will let you reuse them in multiple commands:

{
"scripts": {
"build-and-test": "yarn build \"$@\" && yarn test \"$@\""
}
}