Skip to content

Commit 64ebf51

Browse files
authored
Merge pull request microsoft#1757 from microsoft/zhas/add-json-flag-to-check
add --json flag to rush check
2 parents 9f285ae + 1f793b9 commit 64ebf51

File tree

4 files changed

+83
-11
lines changed

4 files changed

+83
-11
lines changed

apps/rush-lib/src/cli/actions/CheckAction.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22
// See LICENSE in the project root for license information.
33

44
import * as colors from 'colors';
5+
import {
6+
CommandLineStringParameter,
7+
CommandLineFlagParameter
8+
} from '@microsoft/ts-command-line';
9+
510
import { RushCommandLineParser } from '../RushCommandLineParser';
611
import { BaseRushAction } from './BaseRushAction';
712
import { VersionMismatchFinder } from '../../logic/versionMismatch/VersionMismatchFinder';
813
import { Variants } from '../../api/Variants';
9-
import { CommandLineStringParameter } from '@microsoft/ts-command-line';
1014

1115
export class CheckAction extends BaseRushAction {
1216
private _variant: CommandLineStringParameter;
17+
private _jsonFlag: CommandLineFlagParameter;
1318

1419
public constructor(parser: RushCommandLineParser) {
1520
super({
@@ -25,6 +30,10 @@ export class CheckAction extends BaseRushAction {
2530

2631
protected onDefineParameters(): void {
2732
this._variant = this.defineStringParameter(Variants.VARIANT_PARAMETER);
33+
this._jsonFlag = this.defineFlagParameter({
34+
parameterLongName: '--json',
35+
description: 'If this flag is specified, output will be in JSON format.'
36+
});
2837
}
2938

3039
protected run(): Promise<void> {
@@ -38,7 +47,8 @@ export class CheckAction extends BaseRushAction {
3847
}
3948

4049
VersionMismatchFinder.rushCheck(this.rushConfiguration, {
41-
variant: this._variant.value
50+
variant: this._variant.value,
51+
printAsJson: this._jsonFlag.value
4252
});
4353
return Promise.resolve();
4454
}

apps/rush-lib/src/cli/test/__snapshots__/CommandLineHelp.test.ts.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ Optional arguments:
205205
`;
206206
207207
exports[`CommandLineHelp prints the help for each action: check 1`] = `
208-
"usage: rush check [-h] [--variant VARIANT]
208+
"usage: rush check [-h] [--variant VARIANT] [--json]
209209
210210
Checks each project's package.json files and ensures that all dependencies
211211
are of the same version throughout the repository.
@@ -215,6 +215,7 @@ Optional arguments:
215215
--variant VARIANT Run command using a variant installation configuration.
216216
This parameter may alternatively specified via the
217217
RUSH_VARIANT environment variable.
218+
--json If this flag is specified, output will be in JSON format.
218219
"
219220
`;
220221

apps/rush-lib/src/logic/versionMismatch/VersionMismatchFinder.ts

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ import { CommonVersionsConfiguration } from '../../api/CommonVersionsConfigurati
1212
import { VersionMismatchFinderEntity } from './VersionMismatchFinderEntity';
1313
import { VersionMismatchFinderProject } from './VersionMismatchFinderProject';
1414
import { VersionMismatchFinderCommonVersions } from './VersionMismatchFinderCommonVersions';
15+
import { AlreadyReportedError } from '../../utilities/AlreadyReportedError';
1516

1617
export interface IVersionMismatchFinderRushCheckOptions {
1718
variant?: string | undefined;
19+
printAsJson?: boolean | undefined;
1820
}
1921

2022
export interface IVersionMismatchFinderEnsureConsistentVersionsOptions {
@@ -25,6 +27,20 @@ export interface IVersionMismatchFinderGetMismatchesOptions {
2527
variant?: string | undefined;
2628
}
2729

30+
export interface IMismatchDependency {
31+
dependencyName: string;
32+
versions: IMismatchDependencyVersion[];
33+
}
34+
35+
export interface IMismatchDependencyVersion {
36+
version: string,
37+
projects: string[];
38+
}
39+
40+
export interface IMismatchDependencies {
41+
mismatchedVersions: IMismatchDependency[];
42+
}
43+
2844
export class VersionMismatchFinder {
2945
/* store it like this:
3046
* {
@@ -96,20 +112,24 @@ export class VersionMismatchFinder {
96112
options: {
97113
isRushCheckCommand: boolean;
98114
variant?: string | undefined;
115+
printAsJson?: boolean | undefined;
99116
}
100117
): void {
101-
102118
if (rushConfiguration.ensureConsistentVersions || options.isRushCheckCommand) {
103119
const mismatchFinder: VersionMismatchFinder = VersionMismatchFinder.getMismatches(rushConfiguration, options);
104120

105-
mismatchFinder.print();
106-
107-
if (mismatchFinder.numberOfMismatches) {
108-
console.log(colors.red(`Found ${mismatchFinder.numberOfMismatches} mis-matching dependencies!`));
109-
process.exit(1);
121+
if (options.printAsJson) {
122+
mismatchFinder.printAsJson();
110123
} else {
111-
if (options.isRushCheckCommand) {
112-
console.log(colors.green(`Found no mis-matching dependencies!`));
124+
mismatchFinder.print();
125+
126+
if (mismatchFinder.numberOfMismatches > 0) {
127+
console.log(colors.red(`Found ${mismatchFinder.numberOfMismatches} mis-matching dependencies!`));
128+
throw new AlreadyReportedError();
129+
} else {
130+
if (options.isRushCheckCommand) {
131+
console.log(colors.green(`Found no mis-matching dependencies!`));
132+
}
113133
}
114134
}
115135
}
@@ -139,6 +159,36 @@ export class VersionMismatchFinder {
139159
return mismatchedVersion;
140160
}
141161

162+
public printAsJson(): void {
163+
const mismatchDependencies: IMismatchDependency[] = [];
164+
165+
this.getMismatches().forEach((dependency: string) => {
166+
const mismatchDependencyVersionArray: IMismatchDependencyVersion[] = [];
167+
this.getVersionsOfMismatch(dependency)!.forEach((version: string) => {
168+
const projects: string[] = []
169+
this.getConsumersOfMismatch(dependency, version)!.forEach((project: VersionMismatchFinderEntity) => {
170+
projects.push(project.friendlyName);
171+
});
172+
const mismatchDependencyVersion: IMismatchDependencyVersion = {
173+
version: version,
174+
projects: projects
175+
};
176+
mismatchDependencyVersionArray.push(mismatchDependencyVersion);
177+
});
178+
const mismatchDepency: IMismatchDependency = {
179+
dependencyName: dependency,
180+
versions: mismatchDependencyVersionArray
181+
};
182+
mismatchDependencies.push(mismatchDepency);
183+
});
184+
185+
const output: IMismatchDependencies = {
186+
mismatchedVersions: mismatchDependencies
187+
};
188+
189+
console.log(JSON.stringify(output, undefined, 2));
190+
}
191+
142192
public print(): void {
143193
// Iterate over the list. For any dependency with mismatching versions, print the projects
144194
this.getMismatches().forEach((dependency: string) => {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/rush",
5+
"comment": "Add a --json flag for \"rush check\" to facilitate automation",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@microsoft/rush",
10+
"email": "zhas@microsoft.com"
11+
}

0 commit comments

Comments
 (0)