12
12
/**
13
13
* This script crawls through a React Native application's dependencies and invokes the codegen
14
14
* for any libraries that require it.
15
- * To enable codegen support, the library should include a config in the codegenConfigKey key
16
- * in a codegenConfigFilename file.
15
+ * To enable codegen support, the library should include a config in the codegenConfig key
16
+ * in a package.json file.
17
17
*/
18
18
19
19
const { execFileSync, execSync} = require ( 'child_process' ) ;
@@ -37,7 +37,7 @@ const CORE_LIBRARIES_WITH_OUTPUT_FOLDER = {
37
37
rncore : path . join ( REACT_NATIVE_PACKAGE_ROOT_FOLDER , 'ReactCommon' ) ,
38
38
FBReactNativeSpec : null ,
39
39
} ;
40
- const REACT_NATIVE_DEPENDENCY_NAME = 'react-native' ;
40
+ const REACT_NATIVE = 'react-native' ;
41
41
42
42
// HELPERS
43
43
@@ -58,12 +58,16 @@ function isAppRootValid(appRootDir) {
58
58
return true ;
59
59
}
60
60
61
- function readPackageJSON ( appRootDir ) {
62
- return JSON . parse ( fs . readFileSync ( path . join ( appRootDir , 'package.json' ) ) ) ;
61
+ function readPkgJsonInDirectory ( dir ) {
62
+ const pkgJsonPath = path . join ( dir , 'package.json' ) ;
63
+ if ( ! fs . existsSync ( pkgJsonPath ) ) {
64
+ throw `[Codegen] Error: ${ pkgJsonPath } does not exist.` ;
65
+ }
66
+ return JSON . parse ( fs . readFileSync ( pkgJsonPath ) ) ;
63
67
}
64
68
65
69
function printDeprecationWarningIfNeeded ( dependency ) {
66
- if ( dependency === REACT_NATIVE_DEPENDENCY_NAME ) {
70
+ if ( dependency === REACT_NATIVE ) {
67
71
return ;
68
72
}
69
73
console . log ( `[Codegen] CodegenConfig Deprecated Setup for ${ dependency } .
@@ -101,44 +105,35 @@ function printDeprecationWarningIfNeeded(dependency) {
101
105
}
102
106
103
107
// Reading Libraries
104
- function extractLibrariesFromConfigurationArray (
105
- configFile ,
106
- codegenConfigKey ,
107
- dependencyPath ,
108
- ) {
109
- return configFile [ codegenConfigKey ] . libraries . map ( config => {
108
+ function extractLibrariesFromConfigurationArray ( configFile , dependencyPath ) {
109
+ return configFile . codegenConfig . libraries . map ( config => {
110
110
return {
111
111
config,
112
112
libraryPath : dependencyPath ,
113
113
} ;
114
114
} ) ;
115
115
}
116
116
117
- function extractLibrariesFromJSON (
118
- configFile ,
119
- codegenConfigKey ,
120
- dependency ,
121
- dependencyPath ,
122
- ) {
117
+ function extractLibrariesFromJSON ( configFile , dependency , dependencyPath ) {
123
118
var isBlocking = false ;
124
119
if ( dependency == null ) {
125
- dependency = REACT_NATIVE_DEPENDENCY_NAME ;
120
+ dependency = REACT_NATIVE ;
126
121
dependencyPath = REACT_NATIVE_PACKAGE_ROOT_FOLDER ;
127
122
// If we are exploring the ReactNative libraries, we want to raise an error
128
123
// if the codegen is not properly configured.
129
124
isBlocking = true ;
130
125
}
131
126
132
- if ( configFile [ codegenConfigKey ] == null ) {
127
+ if ( configFile . codegenConfig == null ) {
133
128
if ( isBlocking ) {
134
129
throw `[Codegen] Error: Could not find codegen config for ${ dependency } .` ;
135
130
}
136
131
return [ ] ;
137
132
}
138
133
139
134
console . log ( `[Codegen] Found ${ dependency } ` ) ;
140
- if ( configFile [ codegenConfigKey ] . libraries == null ) {
141
- var config = configFile [ codegenConfigKey ] ;
135
+ if ( configFile . codegenConfig . libraries == null ) {
136
+ const config = configFile . codegenConfig ;
142
137
return [
143
138
{
144
139
config,
@@ -147,38 +142,20 @@ function extractLibrariesFromJSON(
147
142
] ;
148
143
} else {
149
144
printDeprecationWarningIfNeeded ( dependency ) ;
150
- return extractLibrariesFromConfigurationArray (
151
- configFile ,
152
- codegenConfigKey ,
153
- dependencyPath ,
154
- ) ;
145
+ return extractLibrariesFromConfigurationArray ( configFile , dependencyPath ) ;
155
146
}
156
147
}
157
148
158
- function handleReactNativeCoreLibraries (
159
- codegenConfigFilename ,
160
- codegenConfigKey ,
161
- ) {
149
+ function handleReactNativeCoreLibraries ( ) {
162
150
// Handle react-native core libraries.
163
151
// This is required when react-native is outside of node_modules.
164
152
console . log ( '[Codegen] Processing react-native core libraries' ) ;
165
- const reactNativePkgJson = path . join (
166
- REACT_NATIVE_PACKAGE_ROOT_FOLDER ,
167
- codegenConfigFilename ,
153
+ return extractLibrariesFromJSON (
154
+ readPkgJsonInDirectory ( REACT_NATIVE_PACKAGE_ROOT_FOLDER ) ,
168
155
) ;
169
- if ( ! fs . existsSync ( reactNativePkgJson ) ) {
170
- throw '[Codegen] Error: Could not find config file for react-native.' ;
171
- }
172
- const reactNativeConfigFile = JSON . parse ( fs . readFileSync ( reactNativePkgJson ) ) ;
173
- return extractLibrariesFromJSON ( reactNativeConfigFile , codegenConfigKey ) ;
174
156
}
175
157
176
- function handleThirdPartyLibraries (
177
- baseCodegenConfigFileDir ,
178
- dependencies ,
179
- codegenConfigFilename ,
180
- codegenConfigKey ,
181
- ) {
158
+ function handleThirdPartyLibraries ( baseCodegenConfigFileDir , dependencies ) {
182
159
// Determine which of these are codegen-enabled libraries
183
160
const configDir =
184
161
baseCodegenConfigFileDir ||
@@ -189,33 +166,25 @@ function handleThirdPartyLibraries(
189
166
190
167
// Handle third-party libraries
191
168
return Object . keys ( dependencies ) . flatMap ( dependency => {
192
- if ( dependency === REACT_NATIVE_DEPENDENCY_NAME ) {
169
+ if ( dependency === REACT_NATIVE ) {
193
170
// react-native should already be added.
194
171
return [ ] ;
195
172
}
196
- const codegenConfigFileDir = path . join ( configDir , dependency ) ;
197
- const configFilePath = path . join (
198
- codegenConfigFileDir ,
199
- codegenConfigFilename ,
200
- ) ;
201
- if ( ! fs . existsSync ( configFilePath ) ) {
173
+ let configFile ;
174
+ try {
175
+ configFile = readPkgJsonInDirectory ( codegenConfigFileDir ) ;
176
+ } catch {
202
177
return [ ] ;
203
178
}
204
- const configFile = JSON . parse ( fs . readFileSync ( configFilePath ) ) ;
205
179
return extractLibrariesFromJSON (
206
180
configFile ,
207
- codegenConfigKey ,
208
181
dependency ,
209
182
codegenConfigFileDir ,
210
183
) ;
211
184
} ) ;
212
185
}
213
186
214
- function handleLibrariesFromReactNativeConfig (
215
- codegenConfigKey ,
216
- codegenConfigFilename ,
217
- appRootDir ,
218
- ) {
187
+ function handleLibrariesFromReactNativeConfig ( appRootDir ) {
219
188
const rnConfigFileName = 'react-native.config.js' ;
220
189
221
190
console . log (
@@ -242,37 +211,27 @@ function handleLibrariesFromReactNativeConfig(
242
211
appRootDir ,
243
212
dependencyConfig . root ,
244
213
) ;
245
- const configFilePath = path . join (
246
- codegenConfigFileDir ,
247
- codegenConfigFilename ,
248
- ) ;
249
- if ( ! fs . existsSync ( configFilePath ) ) {
214
+ let configFile ;
215
+ try {
216
+ configFile = readPkgJsonInDirectory ( codegenConfigFileDir ) ;
217
+ } catch {
250
218
return [ ] ;
251
219
}
252
- const pkgJsonPath = path . join ( codegenConfigFileDir , 'package.json' ) ;
253
- const pkgJson = JSON . parse ( fs . readFileSync ( pkgJsonPath ) ) ;
254
- const configFile = JSON . parse ( fs . readFileSync ( configFilePath ) ) ;
255
220
256
221
return extractLibrariesFromJSON (
257
222
configFile ,
258
- codegenConfigKey ,
259
- pkgJson . name ,
223
+ configFile . name ,
260
224
codegenConfigFileDir ,
261
225
) ;
262
226
} ) ;
263
227
}
264
228
265
- function handleInAppLibraries ( pkgJson , codegenConfigKey , appRootDir ) {
229
+ function handleInAppLibraries ( pkgJson , appRootDir ) {
266
230
console . log (
267
231
'\n\n[Codegen] >>>>> Searching for codegen-enabled libraries in the app' ,
268
232
) ;
269
233
270
- return extractLibrariesFromJSON (
271
- pkgJson ,
272
- codegenConfigKey ,
273
- pkgJson . name ,
274
- appRootDir ,
275
- ) ;
234
+ return extractLibrariesFromJSON ( pkgJson , pkgJson . name , appRootDir ) ;
276
235
}
277
236
278
237
// CodeGen
@@ -417,28 +376,14 @@ function createComponentProvider(schemaPaths, node) {
417
376
console . log ( `Generated provider in: ${ outputDir } ` ) ;
418
377
}
419
378
420
- function findCodegenEnabledLibraries (
421
- appRootDir ,
422
- baseCodegenConfigFileDir ,
423
- codegenConfigFilename ,
424
- codegenConfigKey ,
425
- ) {
426
- const pkgJson = readPackageJSON ( appRootDir ) ;
379
+ function findCodegenEnabledLibraries ( appRootDir , baseCodegenConfigFileDir ) {
380
+ const pkgJson = readPkgJsonInDirectory ( appRootDir ) ;
427
381
const dependencies = { ...pkgJson . dependencies , ...pkgJson . devDependencies } ;
428
382
return [
429
- ...handleReactNativeCoreLibraries ( codegenConfigFilename , codegenConfigKey ) ,
430
- ...handleThirdPartyLibraries (
431
- baseCodegenConfigFileDir ,
432
- dependencies ,
433
- codegenConfigFilename ,
434
- codegenConfigKey ,
435
- ) ,
436
- ...handleLibrariesFromReactNativeConfig (
437
- codegenConfigKey ,
438
- codegenConfigFilename ,
439
- appRootDir ,
440
- ) ,
441
- ...handleInAppLibraries ( pkgJson , codegenConfigKey , appRootDir ) ,
383
+ ...handleReactNativeCoreLibraries ( ) ,
384
+ ...handleThirdPartyLibraries ( baseCodegenConfigFileDir , dependencies ) ,
385
+ ...handleLibrariesFromReactNativeConfig ( appRootDir ) ,
386
+ ...handleInAppLibraries ( pkgJson , appRootDir ) ,
442
387
] ;
443
388
}
444
389
@@ -484,24 +429,15 @@ function cleanupEmptyFilesAndFolders(filepath) {
484
429
* - setups the CLI to generate the code
485
430
* - generate the code
486
431
*
487
- * @parameter appRootDir: the directory with the app source code, where the `codegenConfigFilename` lives.
432
+ * @parameter appRootDir: the directory with the app source code, where the package.json lives.
488
433
* @parameter outputPath: the base output path for the CodeGen.
489
434
* @parameter node: the path to the node executable, used to run the codegen scripts.
490
- * @parameter codegenConfigFilename: the file that contains the codeGen configuration. The default is `package.json`.
491
- * @parameter codegenConfigKey: the key in the codegenConfigFile that controls the codegen.
492
435
* @parameter baseCodegenConfigFileDir: the directory of the codeGenConfigFile.
493
436
* @throws If it can't find a config file for react-native.
494
437
* @throws If it can't find a CodeGen configuration in the file.
495
438
* @throws If it can't find a cli for the CodeGen.
496
439
*/
497
- function execute (
498
- appRootDir ,
499
- outputPath ,
500
- node ,
501
- codegenConfigFilename ,
502
- codegenConfigKey ,
503
- baseCodegenConfigFileDir ,
504
- ) {
440
+ function execute ( appRootDir , outputPath , node , baseCodegenConfigFileDir ) {
505
441
if ( ! isAppRootValid ( appRootDir ) ) {
506
442
return ;
507
443
}
@@ -510,8 +446,6 @@ function execute(
510
446
const libraries = findCodegenEnabledLibraries (
511
447
appRootDir ,
512
448
baseCodegenConfigFileDir ,
513
- codegenConfigFilename ,
514
- codegenConfigKey ,
515
449
) ;
516
450
517
451
if ( libraries . length === 0 ) {
0 commit comments