@@ -12,9 +12,11 @@ import { CommonVersionsConfiguration } from '../../api/CommonVersionsConfigurati
1212import { VersionMismatchFinderEntity } from './VersionMismatchFinderEntity' ;
1313import { VersionMismatchFinderProject } from './VersionMismatchFinderProject' ;
1414import { VersionMismatchFinderCommonVersions } from './VersionMismatchFinderCommonVersions' ;
15+ import { AlreadyReportedError } from '../../utilities/AlreadyReportedError' ;
1516
1617export interface IVersionMismatchFinderRushCheckOptions {
1718 variant ?: string | undefined ;
19+ printAsJson ?: boolean | undefined ;
1820}
1921
2022export 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+
2844export 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 ) => {
0 commit comments