1
1
const { join } = require ( "path" ) ;
2
2
const { copySync, ensureDirSync } = require ( "fs-extra" ) ;
3
- const { readdirSync, lstatSync, readFileSync, existsSync } = require ( "fs" ) ;
3
+ const {
4
+ readdirSync,
5
+ lstatSync,
6
+ readFileSync,
7
+ existsSync,
8
+ writeFileSync
9
+ } = require ( "fs" ) ;
4
10
const { CODE_GEN_OUTPUT_DIR } = require ( "./code-gen-dir" ) ;
5
11
6
12
const getOverwritablePredicate = packageName => pathName => {
@@ -12,6 +18,7 @@ const getOverwritablePredicate = packageName => pathName => {
12
18
"runtimeConfig.ts" ,
13
19
"runtimeConfig.browser.ts" ,
14
20
"runtimeConfig.shared.ts" ,
21
+ "runtimeConfig.rn.ts" ,
15
22
"index.ts" ,
16
23
"endpoints.ts"
17
24
] ;
@@ -25,6 +32,31 @@ const getOverwritablePredicate = packageName => pathName => {
25
32
) ;
26
33
} ;
27
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 us removing dependencies/scripts
42
+ * from codegen, but maintain the newer dependency versions
43
+ * in existing package.json
44
+ */
45
+ const mergeManifest = ( fromContent , toContent ) => {
46
+ const merged = { } ;
47
+ const fromNames = Object . keys ( fromConfig ) ;
48
+ for ( const name of fromNames ) {
49
+ if ( typeof toContent [ name ] === "object" ) {
50
+ merged [ name ] = mergeManifest ( fromContent [ name ] , toContent [ name ] ) ;
51
+ } else {
52
+ // If key (say dependency) is present in both codegen and
53
+ // package.json, we prefer latter
54
+ merged [ name ] = toContent [ name ] || fromContent [ name ] ;
55
+ }
56
+ }
57
+ return merged ;
58
+ } ;
59
+
28
60
async function copyToClients ( clientsDir ) {
29
61
for ( const modelName of readdirSync ( CODE_GEN_OUTPUT_DIR ) ) {
30
62
if ( modelName === "source" ) continue ;
@@ -49,7 +81,17 @@ async function copyToClients(clientsDir) {
49
81
for ( const packageSub of readdirSync ( artifactPath ) ) {
50
82
const packageSubPath = join ( artifactPath , packageSub ) ;
51
83
const destSubPath = join ( destPath , packageSub ) ;
52
- if ( overwritablePredicate ( packageSub ) || ! existsSync ( destSubPath ) ) {
84
+ if ( packageSub === "package.json" ) {
85
+ //copy manifest file
86
+ const destManifest = existsSync ( destSubPath )
87
+ ? JSON . parse ( readFileSync ( destSubPath ) . toString ( ) )
88
+ : { } ;
89
+ const mergedManifest = mergeManifest ( packageManifest , destManifest ) ;
90
+ writeFileSync ( destSubPath , JSON . stringify ( mergedManifest , null , 2 ) ) ;
91
+ } else if (
92
+ overwritablePredicate ( packageSub ) ||
93
+ ! existsSync ( destSubPath )
94
+ ) {
53
95
//Overwrite the directories and files that are overwritable, or not yet exists
54
96
if ( lstatSync ( packageSubPath ) . isDirectory ( ) ) ensureDirSync ( destSubPath ) ;
55
97
copySync ( packageSubPath , destSubPath , {
0 commit comments