Skip to content

Commit d4dcf0a

Browse files
fix(rush): default rush-pnpm query commands to recursive (#5822)
* fix(rush): default rush-pnpm query commands to recursive * Rush change. Co-authored-by: Ian Clanton-Thuon <iclanton@users.noreply.github.com> --------- Co-authored-by: kiranmagic7 <262980978+kiranmagic7@users.noreply.github.com> Co-authored-by: Ian Clanton-Thuon <iclanton@users.noreply.github.com>
1 parent 7148e57 commit d4dcf0a

3 files changed

Lines changed: 67 additions & 0 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "Default `rush-pnpm outdated` and `rush-pnpm why` to recursive workspace queries.",
5+
"type": "minor",
6+
"packageName": "@microsoft/rush"
7+
}
8+
],
9+
"packageName": "@microsoft/rush",
10+
"email": "262980978+kiranmagic7@users.noreply.github.com"
11+
}

libraries/rush-lib/src/cli/RushPnpmCommandLineParser.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,25 @@ import { EnvironmentVariableNames } from '../api/EnvironmentConfiguration';
3636
import { initializeDotEnv } from '../logic/dotenv';
3737

3838
const RUSH_SKIP_CHECKS_PARAMETER: string = '--rush-skip-checks';
39+
const RUSH_PNPM_RECURSIVE_DEFAULT_COMMANDS: Set<string> = new Set(['outdated', 'why']);
40+
41+
function _hasRecursiveFlag(pnpmArgs: string[]): boolean {
42+
return pnpmArgs.some((arg) => arg === '-r' || arg === '--recursive' || arg.startsWith('--recursive='));
43+
}
44+
45+
function _hasGlobalFlag(pnpmArgs: string[]): boolean {
46+
return pnpmArgs.some((arg) => arg === '-g' || arg === '--global' || arg.startsWith('--global='));
47+
}
48+
49+
function _addDefaultRecursiveFlagIfNeeded(commandName: string, pnpmArgs: string[]): void {
50+
if (
51+
RUSH_PNPM_RECURSIVE_DEFAULT_COMMANDS.has(commandName) &&
52+
!_hasRecursiveFlag(pnpmArgs) &&
53+
!_hasGlobalFlag(pnpmArgs)
54+
) {
55+
pnpmArgs.splice(1, 0, '--recursive');
56+
}
57+
}
3958

4059
/**
4160
* Options for RushPnpmCommandLineParser
@@ -253,6 +272,7 @@ export class RushPnpmCommandLineParser {
253272
}
254273

255274
this._commandName = commandName;
275+
_addDefaultRecursiveFlagIfNeeded(commandName, pnpmArgs);
256276

257277
// Warn about commands known not to work
258278
/* eslint-disable no-fallthrough */
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
import { RushPnpmCommandLineParser } from '../RushPnpmCommandLineParser';
5+
6+
interface IRushPnpmCommandLineParserInternals {
7+
_validatePnpmUsageAsync(pnpmArgs: string[]): Promise<void>;
8+
}
9+
10+
async function validatePnpmArgsAsync(pnpmArgs: string[]): Promise<string[]> {
11+
const parser: IRushPnpmCommandLineParserInternals = Object.create(RushPnpmCommandLineParser.prototype);
12+
await parser._validatePnpmUsageAsync(pnpmArgs);
13+
return pnpmArgs;
14+
}
15+
16+
describe(RushPnpmCommandLineParser.name, () => {
17+
it('adds recursive mode to workspace query commands by default', async () => {
18+
await expect(validatePnpmArgsAsync(['outdated'])).resolves.toEqual(['outdated', '--recursive']);
19+
await expect(validatePnpmArgsAsync(['why', '@rushstack/node-core-library'])).resolves.toEqual([
20+
'why',
21+
'--recursive',
22+
'@rushstack/node-core-library'
23+
]);
24+
});
25+
26+
it('does not duplicate explicit recursive flags', async () => {
27+
await expect(validatePnpmArgsAsync(['outdated', '-r'])).resolves.toEqual(['outdated', '-r']);
28+
await expect(
29+
validatePnpmArgsAsync(['why', '--recursive', '@rushstack/node-core-library'])
30+
).resolves.toEqual(['why', '--recursive', '@rushstack/node-core-library']);
31+
});
32+
33+
it('does not force recursive mode for global outdated checks', async () => {
34+
await expect(validatePnpmArgsAsync(['outdated', '--global'])).resolves.toEqual(['outdated', '--global']);
35+
});
36+
});

0 commit comments

Comments
 (0)