@@ -32,25 +32,29 @@ const getOverwritablePredicate = packageName => pathName => {
32
32
) ;
33
33
} ;
34
34
35
+ /**
36
+ * Copy the keys from newly-generated package.json to
37
+ * existing package.json. For each keys in new package.json
38
+ * we prefer the new key. Whereas for the values, we prefer
39
+ * the values in the existing package.json.
40
+ *
41
+ * This behavior enables as remove dependencies/scripts
42
+ * from codegen, but maintain the newer dependency versions
43
+ * in existing package.json
44
+ */
35
45
const mergeManifest = ( fromContent , toContent ) => {
36
46
const merged = { } ;
37
- const toNames = Object . keys ( toContent ) ;
38
- for ( const name of toNames ) {
39
- if ( ! fromContent [ name ] ) {
40
- merged [ name ] = toContent [ name ] ;
41
- continue ;
42
- }
47
+ const fromNames = Object . keys ( fromConfig ) ;
48
+ for ( const name of fromNames ) {
49
+ merged [ name ] = fromConfig [ name ] ;
43
50
if ( typeof toContent [ name ] === "object" ) {
44
51
merged [ name ] = mergeManifest ( fromContent [ name ] , toContent [ name ] ) ;
45
52
} else {
53
+ // If key (say dependency) is present in both codegen and
54
+ // package.json, we prefer latter
46
55
merged [ name ] = toContent [ name ] || fromContent [ name ] ;
47
56
}
48
57
}
49
- for ( const name of Object . keys ( fromContent ) ) {
50
- if ( toNames . indexOf ( name ) < 0 ) {
51
- merged [ name ] = fromContent [ name ] ;
52
- }
53
- }
54
58
return merged ;
55
59
} ;
56
60
@@ -78,19 +82,23 @@ async function copyToClients(clientsDir) {
78
82
for ( const packageSub of readdirSync ( artifactPath ) ) {
79
83
const packageSubPath = join ( artifactPath , packageSub ) ;
80
84
const destSubPath = join ( destPath , packageSub ) ;
81
- if ( overwritablePredicate ( packageSub ) || ! existsSync ( destSubPath ) ) {
85
+ if ( packageSub === "package.json" ) {
86
+ //copy manifest file
87
+ const destManifest = existsSync ( destSubPath )
88
+ ? JSON . parse ( readFileSync ( destSubPath ) . toString ( ) )
89
+ : { } ;
90
+ const mergedManifest = mergeManifest ( packageManifest , destManifest ) ;
91
+ writeFileSync ( destSubPath , JSON . stringify ( mergedManifest , null , 2 ) ) ;
92
+ } else if (
93
+ overwritablePredicate ( packageSub ) ||
94
+ ! existsSync ( destSubPath )
95
+ ) {
82
96
//Overwrite the directories and files that are overwritable, or not yet exists
83
97
if ( lstatSync ( packageSubPath ) . isDirectory ( ) ) ensureDirSync ( destSubPath ) ;
84
98
copySync ( packageSubPath , destSubPath , {
85
99
overwrite : true
86
100
} ) ;
87
101
}
88
- if ( packageSub === "package.json" ) {
89
- //copy manifest file
90
- const destManifest = JSON . parse ( readFileSync ( destSubPath ) . toString ( ) ) ;
91
- const mergedManifest = mergeManifest ( packageManifest , destManifest ) ;
92
- writeFileSync ( destSubPath , JSON . stringify ( mergedManifest , null , 2 ) ) ;
93
- }
94
102
}
95
103
}
96
104
}
0 commit comments