1+ const path = require ( 'path' ) ;
2+ const fs = require ( 'fs' ) ;
3+
4+ versionReplace = function ( pathToUnifiedDeps , pathToNewUnifiedDeps , outputPath ) {
5+ var currentDeps = fs . readFileSync ( pathToUnifiedDeps , "utf8" ) ;
6+ var newDeps = fs . readFileSync ( pathToNewUnifiedDeps , "utf8" ) ;
7+
8+ var currentDepsArr = currentDeps . split ( '\n' ) ;
9+ var newDepsArr = newDeps . split ( '\n' ) ;
10+ var newDepsDict = { } ;
11+
12+ newDepsArr . forEach ( function ( newDep ) {
13+ // add to dictionary
14+ var depDetails = newDep . split ( "\"" ) ;
15+ console . log ( JSON . stringify ( depDetails ) ) ;
16+ var name = depDetails [ 1 ] ;
17+ var version = depDetails [ 3 ] ;
18+ console . log ( name + ' ' + version ) ;
19+ newDepsDict [ name ] = version ;
20+ } ) ;
21+
22+ var updatedDeps = [ ] ;
23+
24+ currentDepsArr . forEach ( function ( currentDep ) {
25+ var depDetails = currentDep . split ( "\"" ) ;
26+ var name = depDetails [ 1 ] ;
27+ var version = depDetails [ 3 ] ;
28+
29+ // find if there is a match in new
30+ if ( newDepsDict [ name ] ) {
31+ // update the version
32+ depDetails [ 3 ] = newDepsDict [ name ] ;
33+ updatedDeps . push ( depDetails . join ( '\"' ) ) ;
34+ } else {
35+ if ( currentDep . indexOf ( '</packages>' ) <= - 1 ) {
36+ updatedDeps . push ( currentDep ) ;
37+ }
38+ console . log ( `"${ currentDep } "` ) ;
39+ }
40+ } ) ;
41+
42+ // list new ones that arent in current
43+ newDepsArr . forEach ( function ( newDep ) {
44+ // add to dictionary
45+ var depDetails = newDep . split ( "\"" ) ;
46+ console . log ( JSON . stringify ( depDetails ) ) ;
47+ var name = depDetails [ 1 ] ;
48+ var version = depDetails [ 3 ] ;
49+
50+ var currentContainsNew = false ;
51+ currentDepsArr . forEach ( function ( currentDep ) {
52+ var depDetails = currentDep . split ( "\"" ) ;
53+ var currName = depDetails [ 1 ] ;
54+
55+ if ( currName === name ) {
56+ currentContainsNew = true ;
57+ }
58+ } ) ;
59+
60+ if ( ! currentContainsNew ) {
61+ console . log ( name ) ;
62+ updatedDeps . push ( newDep ) ;
63+ }
64+ } ) ;
65+
66+ updatedDeps . push ( '</packages>' ) ;
67+
68+ // write it as a new file where currentDeps is
69+ fs . writeFileSync ( outputPath , updatedDeps . join ( "\n" ) ) ;
70+ console . log ( 'Done.' ) ;
71+ }
72+
73+ const unifiedDeps = process . argv [ 2 ] ;
74+ const newDeps = process . argv [ 3 ] ;
75+
76+ versionReplace ( unifiedDeps , newDeps , unifiedDeps ) ;
0 commit comments