From 7161dedd5a3acc993681d5da251eb694b07d8eff Mon Sep 17 00:00:00 2001 From: Dylan Conway <35280289+dylan-conway@users.noreply.github.com> Date: Tue, 7 Jan 2025 20:23:25 -0800 Subject: [PATCH] Update `--filter` docs (#16225) --- docs/cli/filter.md | 67 +++++++++++++++++++++++++++++++++----------- docs/cli/install.md | 14 +++++++++ docs/cli/outdated.md | 2 ++ docs/cli/run.md | 4 +-- 4 files changed, 68 insertions(+), 19 deletions(-) diff --git a/docs/cli/filter.md b/docs/cli/filter.md index b4ee0dcf3f..38a497548b 100644 --- a/docs/cli/filter.md +++ b/docs/cli/filter.md @@ -1,4 +1,51 @@ -Use the `--filter` flag to execute lifecycle scripts in multiple packages at once: +The `--filter` (or `-F`) flag is used for selecting packages by pattern in a monorepo. Patterns can be used to match package names or package paths, with full glob syntax support. + +Currently `--filter` is supported by `bun install` and `bun outdated`, and can also be used to run scripts for multiple packages at once. + +## Matching + +### Package Name `--filter ` + +Name patterns select packages based on the package name, as specified in `package.json`. For example, if you have packages `pkg-a`, `pkg-b` and `other`, you can match all packages with `*`, only `pkg-a` and `pkg-b` with `pkg*`, and a specific package by providing the full name of the package. + +### Package Path `--filter ./` + +Path patterns are specified by starting the pattern with `./`, and will select all packages in directories that match the pattern. For example, to match all packages in subdirectories of `packages`, you can use `--filter './packages/**'`. To match a package located in `packages/foo`, use `--filter ./packages/foo`. + +## `bun install` and `bun outdated` + +Both `bun install` and `bun outdated` support the `--filter` flag. + +`bun install` by default will install dependencies for all packages in the monorepo. To install dependencies for specific packages, use `--filter`. + +Given a monorepo with workspaces `pkg-a`, `pkg-b`, and `pkg-c` under `./packages`: + +```bash +# Install dependencies for all workspaces except `pkg-c` +$ bun install --filter '!pkg-c' + +# Install dependencies for packages in `./packages` (`pkg-a`, `pkg-b`, `pkg-c`) +$ bun install --filter './packages/*' + +# Save as above, but exclude the root package.json +$ bun install --filter --filter '!./' --filter './packages/*' +``` + +Similarly, `bun outdated` will display outdated dependencies for all packages in the monorepo, and `--filter` can be used to restrict the command to a subset of the packages: + +```bash +# Display outdated dependencies for workspaces starting with `pkg-` +$ bun outdated --filter 'pkg-*' + +# Display outdated dependencies for only the root package.json +$ bun outdated --filter './' +``` + +For more infomation on both these commands, see [`bun install`](https://bun.sh/docs/cli/install) and [`bun outdated`](https://bun.sh/docs/cli/outdated). + +## Running scripts with `--filter` + +Use the `--filter` flag to execute scripts in multiple packages at once: ```bash bun --filter