diff --git a/docs/pm/cli/outdated.mdx b/docs/pm/cli/outdated.mdx index 361f0bd65e8f..fdc670ac30cb 100644 --- a/docs/pm/cli/outdated.mdx +++ b/docs/pm/cli/outdated.mdx @@ -94,6 +94,20 @@ bun outdated '!@types/*' | typescript (dev) | 5.7.2 | 5.7.3 | 5.7.3 | ``` +To check several dependencies, pass more than one name or pattern. `bun outdated` matches each pattern against the dependency name as written in `package.json`. The table lists a dependency when at least one pattern matches it. A pattern that starts with `!` removes the dependencies it matches: + +```sh terminal icon="terminal" +bun outdated typescript 'eslint*' '!eslint-plugin-sonarjs' +``` + +```txt +| Package | Current | Update | Latest | +| ------------------------------ | ------- | ------ | ---------- | +| eslint (dev) | 8.57.1 | 8.57.1 | 9.20.0 | +| eslint-plugin-security (dev) | 2.1.1 | 2.1.1 | 3.0.1 | +| typescript (dev) | 5.7.2 | 5.7.3 | 5.7.3 | +``` + ### Workspace Filters Use the `--filter` flag to check for outdated dependencies in a different workspace package: diff --git a/src/runtime/cli/outdated_command.rs b/src/runtime/cli/outdated_command.rs index 003000547e14..24653ec7a5ee 100644 --- a/src/runtime/cli/outdated_command.rs +++ b/src/runtime/cli/outdated_command.rs @@ -64,6 +64,36 @@ impl<'a> FilterType<'a> { // *NOTE*: name and path are not allocated → no Drop impl needed. } +/// Combines the patterns like `--filter` values (`workspace_selection::select`). +fn package_patterns_select(patterns: &[FilterType<'_>], dep_name: &[u8]) -> bool { + let mut has_positive = false; + let mut selected = false; + for pattern in patterns { + match pattern { + FilterType::Path => unreachable!(), + FilterType::All => { + has_positive = true; + selected = true; + } + FilterType::Name(name_pattern) => { + if name_pattern.is_empty() { + continue; + } + let result = glob::r#match(name_pattern, dep_name); + if result.is_negated() { + if !result.matches() { + return false; + } + } else { + has_positive = true; + selected |= result.matches(); + } + } + } + } + selected || !has_positive +} + impl OutdatedCommand { pub(crate) fn exec(ctx: Command::Context) -> crate::Result<()> { bun_core::prettyln!( @@ -391,29 +421,10 @@ impl OutdatedCommand { } // package patterns match against dependency name (name in package.json) - if let Some(patterns) = &package_patterns { - let matched = 'match_: { - for pattern in patterns { - match pattern { - FilterType::Path => unreachable!(), - FilterType::Name(name_pattern) => { - if name_pattern.is_empty() { - continue; - } - if !glob::r#match(name_pattern, dep.name.slice(string_buf)) - .matches() - { - break 'match_ false; - } - } - FilterType::All => {} - } - } - true - }; - if !matched { - continue; - } + if let Some(patterns) = &package_patterns + && !package_patterns_select(patterns, dep.name.slice(string_buf)) + { + continue; } let package_name = diff --git a/test/cli/install/bun-install-registry.test.ts b/test/cli/install/bun-install-registry.test.ts index 01cc01240eaf..37141f03fda1 100644 --- a/test/cli/install/bun-install-registry.test.ts +++ b/test/cli/install/bun-install-registry.test.ts @@ -9569,6 +9569,52 @@ describe("outdated", () => { expect(out).toContain("prereleases-1"); }); + test("several dependency pattern args", async () => { + await write( + packageJson, + JSON.stringify({ + name: "foo", + dependencies: { + "a-dep": "1.0.1", + "no-deps": "1.0.0", + "prereleases-1": "1.0.0-future.1", + }, + }), + ); + await runBunInstall(env, packageDir); + + // The "Package" column of every row in the table. + async function outdatedPackages(patterns: string[]): Promise { + const out = await runBunOutdated(env, packageDir, ...patterns); + return out + .split(/\r?\n/) + .filter(line => line.startsWith("| ") && !line.startsWith("| Package ")) + .map(line => line.split("|")[1].trim()); + } + + // `listed` is what `bun outdated ` prints. + const cases: { patterns: string[]; listed: string[] }[] = [ + { patterns: [], listed: ["a-dep", "no-deps", "prereleases-1"] }, + // A dependency is listed when at least one pattern matches it. + { patterns: ["no-deps", "a-dep"], listed: ["a-dep", "no-deps"] }, + { patterns: ["not-a-dependency", "a-dep"], listed: ["a-dep"] }, + { patterns: ["no-deps", "pre*"], listed: ["no-deps", "prereleases-1"] }, + { patterns: ["*", "a-dep"], listed: ["a-dep", "no-deps", "prereleases-1"] }, + // A negated pattern removes its matches, wherever it is in the list. + { patterns: ["prereleases-1", "*-dep*", "!no-deps"], listed: ["a-dep", "prereleases-1"] }, + { patterns: ["!no-deps", "*", "a-dep"], listed: ["a-dep", "prereleases-1"] }, + { patterns: ["no-deps", "a-dep", "!a-*"], listed: ["no-deps"] }, + // With only negated patterns, every other dependency is listed. + { patterns: ["!a-dep"], listed: ["no-deps", "prereleases-1"] }, + { patterns: ["!a-dep", "!no-deps"], listed: ["prereleases-1"] }, + // `!!` is a double negation, so the pattern is not negated. + { patterns: ["!!a-dep", "no-deps"], listed: ["a-dep", "no-deps"] }, + ]; + // `bun outdated` reads the lockfile and the manifest cache and writes nothing. + const listed = await Promise.all(cases.map(({ patterns }) => outdatedPackages(patterns))); + expect(cases.map(({ patterns }, i) => ({ patterns, listed: listed[i] }))).toEqual(cases); + }); + test("scoped workspace names", async () => { await Promise.all([ write(