重要提示: 此中文文档针对的是 Yarn 2 版本。
有关 1.x 版本的中文文档,请点击进入 yarn.bootcss.com。
Yarn
yarn addyarn binyarn cache cleanyarn config getyarn config setyarn configyarn constraints queryyarn constraints sourceyarn constraintsyarn dedupeyarn dlxyarn execyarn infoyarn inityarn installyarn linkyarn nodeyarn npm infoyarn npm loginyarn npm logoutyarn npm publishyarn npm tag addyarn npm tag listyarn npm tag removeyarn npm whoamiyarn packyarn patch-commityarn patchyarn plugin import from sourcesyarn plugin importyarn plugin listyarn plugin removeyarn plugin runtimeyarn rebuildyarn removeyarn runyarn searchyarn set resolutionyarn set version from sourcesyarn set versionyarn stageyarn unplugyarn upyarn upgrade-interactiveyarn version applyyarn version checkyarn versionyarn whyyarn workspaceyarn workspaces focusyarn workspaces foreachyarn workspaces list

yarn workspaces foreach

Plugin

To use this command, first install the workspace-tools plugin: yarn plugin import workspace-tools

Run a command on all workspaces.

Usage

$> yarn workspaces foreach [-A,--all] [-v,--verbose] [-p,--parallel] [-i,--interlaced] [-j,--jobs #0] [-t,--topological] [--topological-dev] [--include #0] [--exclude #0] [--private] <commandName> ...

Examples

Publish current and all descendant packages :

yarn workspaces foreach npm publish --tolerate-republish

Run build script on current and all descendant packages :

yarn workspaces foreach run build

Run build script on current and all descendant packages in parallel, building dependent packages first :

yarn workspaces foreach -pt run build

Details

This command will run a given sub-command on current and all its descendant workspaces. Various flags can alter the exact behavior of the command:

  • If -p,--parallel is set, the commands will be ran in parallel; they'll by default be limited to a number of parallel tasks roughly equal to half your core number, but that can be overridden via -j,--jobs.

  • If -p,--parallel and -i,--interlaced are both set, Yarn will print the lines from the output as it receives them. If -i,--interlaced wasn't set, it would instead buffer the output from each process and print the resulting buffers only after their source processes have exited.

  • If -t,--topological is set, Yarn will only run the command after all workspaces that depend on it through the dependencies field have successfully finished executing. If --topological-dev is set, both the dependencies and devDependencies fields will be considered when figuring out the wait points.

  • If -A,--all is set, Yarn will run the command on all the workspaces of a project. By default yarn runs the command only on current and all its descendant workspaces.

  • The command may apply to only some workspaces through the use of --include which acts as a whitelist. The --exclude flag will do the opposite and will be a list of packages that mustn't execute the script. Both flags accept glob patterns (if valid Idents and supported by micromatch). Make sure to escape the patterns, to prevent your own shell from trying to expand them.

Adding the -v,--verbose flag will cause Yarn to print more information; in particular the name of the workspace that generated the output will be printed at the front of each line.

If the command is run and the script being run does not exist the child workspace will be skipped without error.