11// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22// See LICENSE in the project root for license information.
33
4+ import * as colors from 'colors' ;
5+
6+ import { RushConfiguration } from './RushConfiguration' ;
47import { RushConfigurationProject } from './RushConfigurationProject' ;
8+ import { RushConstants } from '../logic/RushConstants' ;
59
610/**
711 * @public
@@ -18,6 +22,63 @@ export class VersionMismatchFinder {
1822 private _mismatches : Map < string , Map < string , string [ ] > > ;
1923 private _projects : RushConfigurationProject [ ] ;
2024
25+ public static rushCheck ( rushConfiguration : RushConfiguration ) : void {
26+ VersionMismatchFinder . _checkForInconsistentVersions ( rushConfiguration , true ) ;
27+ }
28+
29+ public static enforceConsistentVersions ( rushConfiguration : RushConfiguration ) : void {
30+ VersionMismatchFinder . _checkForInconsistentVersions ( rushConfiguration , false ) ;
31+ }
32+
33+ private static _checkForInconsistentVersions (
34+ rushConfiguration : RushConfiguration ,
35+ isRushCheckCommand : boolean ) : void {
36+
37+ if ( rushConfiguration . enforceConsistentVersions || isRushCheckCommand ) {
38+ // Collect all the preferred versions into a single table
39+ const allPreferredVersions : { [ dependency : string ] : string } = { } ;
40+
41+ rushConfiguration . commonVersions . getAllPreferredVersions ( ) . forEach ( ( version : string , dependency : string ) => {
42+ allPreferredVersions [ dependency ] = version ;
43+ } ) ;
44+
45+ // Create a fake project for the purposes of reporting conflicts with preferredVersions
46+ // or xstitchPreferredVersions from common-versions.json
47+ const projects : RushConfigurationProject [ ] = [ ...rushConfiguration . projects ] ;
48+
49+ projects . push ( {
50+ packageName : 'preferred versions from ' + RushConstants . commonVersionsFilename ,
51+ packageJson : { dependencies : allPreferredVersions }
52+ } as RushConfigurationProject ) ;
53+
54+ const mismatchFinder : VersionMismatchFinder = new VersionMismatchFinder (
55+ projects ,
56+ rushConfiguration . commonVersions . allowedAlternativeVersions
57+ ) ;
58+
59+ // Iterate over the list. For any dependency with mismatching versions, print the projects
60+ mismatchFinder . getMismatches ( ) . forEach ( ( dependency : string ) => {
61+ console . log ( colors . yellow ( dependency ) ) ;
62+ mismatchFinder . getVersionsOfMismatch ( dependency ) ! . forEach ( ( version : string ) => {
63+ console . log ( ` ${ version } ` ) ;
64+ mismatchFinder . getConsumersOfMismatch ( dependency , version ) ! . forEach ( ( project : string ) => {
65+ console . log ( ` - ${ project } ` ) ;
66+ } ) ;
67+ } ) ;
68+ console . log ( ) ;
69+ } ) ;
70+
71+ if ( mismatchFinder . numberOfMismatches ) {
72+ console . log ( colors . red ( `Found ${ mismatchFinder . numberOfMismatches } mis-matching dependencies!` ) ) ;
73+ process . exit ( 1 ) ;
74+ } else {
75+ if ( isRushCheckCommand ) {
76+ console . log ( colors . green ( `Found no mis-matching dependencies!` ) ) ;
77+ }
78+ }
79+ }
80+ }
81+
2182 constructor ( projects : RushConfigurationProject [ ] , allowedAlternativeVersions ?: Map < string , ReadonlyArray < string > > ) {
2283 this . _projects = projects ;
2384 this . _mismatches = new Map < string , Map < string , string [ ] > > ( ) ;
0 commit comments