1+ import RushConfigurationProject from './RushConfigurationProject' ;
2+
3+ export class VersionMismatchFinder {
4+ private _projects : RushConfigurationProject [ ] ;
5+ /* store it like this:
6+ * {
7+ * "@types /node": {
8+ * "1.0.0": [ '@ms /rush' ]
9+ * }
10+ * }
11+ */
12+ private _mismatches : Map < string , Map < string , string [ ] > > ;
13+
14+ constructor ( projects : RushConfigurationProject [ ] ) {
15+ this . _projects = projects ;
16+ this . _mismatches = new Map < string , Map < string , string [ ] > > ( ) ;
17+ this . _analyze ( ) ;
18+ }
19+
20+ public get numberOfMismatches ( ) : number {
21+ return this . _mismatches . size ;
22+ }
23+
24+ public getMismatches ( ) : Array < string > {
25+ return this . _iterableToArray < string > ( this . _mismatches . keys ( ) ) ;
26+ }
27+
28+ public getVersionsOfMismatch ( mismatch : string ) : Array < string > {
29+ return this . _mismatches . has ( mismatch )
30+ ? this . _iterableToArray < string > ( this . _mismatches . get ( mismatch ) . keys ( ) )
31+ : undefined ;
32+ }
33+
34+ public getConsumersOfMismatch ( mismatch : string , version : string ) : Array < string > {
35+ const mismatchedPackage : Map < string , string [ ] > = this . _mismatches . get ( mismatch ) ;
36+ if ( ! mismatchedPackage ) {
37+ return undefined ;
38+ }
39+
40+ const mismatchedVersion : string [ ] = mismatchedPackage . get ( version ) ;
41+ return mismatchedVersion ;
42+ }
43+
44+ private _analyze ( ) : void {
45+ this . _projects . forEach ( ( project : RushConfigurationProject ) => {
46+ this . _addDependenciesToList ( project . packageName , project . packageJson . dependencies ) ;
47+ this . _addDependenciesToList ( project . packageName , project . packageJson . devDependencies ) ;
48+ // tslint:disable:no-any
49+ this . _addDependenciesToList ( project . packageName , project . packageJson . peerDependencies ) ;
50+ this . _addDependenciesToList ( project . packageName , project . packageJson . optionalDependencies ) ;
51+ // tslint:enable:no-any
52+ } ) ;
53+
54+ this . _mismatches . forEach ( ( mismatches : Map < string , string [ ] > , project : string ) => {
55+ if ( mismatches . size <= 1 ) {
56+ this . _mismatches . delete ( project ) ;
57+ }
58+ } ) ;
59+ }
60+
61+ private _addDependenciesToList ( project : string , depMap : { [ dependency : string ] : string } ) : void {
62+ Object . keys ( depMap || { } ) . forEach ( ( dependency : string ) => {
63+ const version : string = depMap [ dependency ] ;
64+ if ( ! this . _mismatches . has ( dependency ) ) {
65+ this . _mismatches . set ( dependency , new Map < string , string [ ] > ( ) ) ;
66+ }
67+ if ( ! this . _mismatches . get ( dependency ) . has ( version ) ) {
68+ this . _mismatches . get ( dependency ) . set ( version , [ ] ) ;
69+ }
70+ this . _mismatches . get ( dependency ) . get ( version ) . push ( project ) ;
71+ } ) ;
72+ }
73+
74+ private _iterableToArray < T > ( iterable : Iterator < T > ) : T [ ] {
75+ const b : T [ ] = [ ] ;
76+ let a : IteratorResult < T > ;
77+ while ( ( a = iterable . next ( ) ) && ! a . done ) {
78+ b . push ( a . value ) ;
79+ }
80+ return b ;
81+ }
82+ }
0 commit comments