Skip to content

Commit 33a44e6

Browse files
dmytrorykunfacebook-github-bot
authored andcommitted
Refactor generate-artifacts-executor.js: delete configFilename and configKey arguments (#41533)
Summary: Pull Request resolved: #41533 This diff removes `configFilename` and `configKey` arguments from iOS codegen CLI. Now we always expect them to be `package.json` and `codegenConfig` respectively. ## Motivation The existing implementation expects every library to have its codegen config in a file with `configFilename` name. `configFilename` is passed as a single CLI argument and applied to every app dependency. I.e. if `configFilename = codegen.config.json` then we expect to find this file in *every* third-party library. That is weird expectation. This customisation option is unsound. Same with `configKey`. It is much simpler to just stick with convention that `configFilename = "package.json"` and `configKey = "codegenConfig"`. Changelog: [General][Breaking] - Delete `configFilename` and `configKey` arguments from iOS codegen CLI. Now we always expect them to be `package.json` and `codegenConfig` respectively. Reviewed By: cipolleschi Differential Revision: D51256486 fbshipit-source-id: fe190b514be7c4e489c7be01294958cf3254602a
1 parent d1e03f5 commit 33a44e6

File tree

3 files changed

+48
-141
lines changed

3 files changed

+48
-141
lines changed

packages/react-native/scripts/codegen/__tests__/generate-artifacts-executor-test.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ const child_process = require('child_process');
1616
const fs = require('fs');
1717
const path = require('path');
1818

19-
const codegenConfigKey = 'codegenConfig';
2019
const reactNativeDependencyName = 'react-native';
2120
const rootPath = path.join(__dirname, '../../..');
2221

@@ -85,15 +84,14 @@ describe('extractLibrariesFromJSON', () => {
8584
it('throws if in react-native and no dependencies found', () => {
8685
let configFile = {};
8786
expect(() => {
88-
underTest._extractLibrariesFromJSON(configFile, codegenConfigKey);
87+
underTest._extractLibrariesFromJSON(configFile);
8988
}).toThrow();
9089
});
9190

9291
it('it skips if not into react-native and no dependencies found', () => {
9392
let configFile = {};
9493
let libraries = underTest._extractLibrariesFromJSON(
9594
configFile,
96-
codegenConfigKey,
9795
'some-node-module',
9896
'node_modules/some',
9997
);
@@ -104,7 +102,6 @@ describe('extractLibrariesFromJSON', () => {
104102
let configFile = fixtures.noLibrariesConfigFile;
105103
let libraries = underTest._extractLibrariesFromJSON(
106104
configFile,
107-
codegenConfigKey,
108105
'my-app',
109106
'.',
110107
);
@@ -123,7 +120,6 @@ describe('extractLibrariesFromJSON', () => {
123120
const configFile = {codegenConfig: {libraries: []}};
124121
let libraries = underTest._extractLibrariesFromJSON(
125122
configFile,
126-
codegenConfigKey,
127123
reactNativeDependencyName,
128124
rootPath,
129125
);
@@ -134,7 +130,6 @@ describe('extractLibrariesFromJSON', () => {
134130
const configFile = fixtures.singleLibraryCodegenConfig;
135131
let libraries = underTest._extractLibrariesFromJSON(
136132
configFile,
137-
codegenConfigKey,
138133
reactNativeDependencyName,
139134
rootPath,
140135
);
@@ -155,7 +150,6 @@ describe('extractLibrariesFromJSON', () => {
155150
const myDependencyPath = path.join(__dirname, myDependency);
156151
let libraries = underTest._extractLibrariesFromJSON(
157152
configFile,
158-
codegenConfigKey,
159153
myDependency,
160154
myDependencyPath,
161155
);

packages/react-native/scripts/codegen/generate-artifacts-executor.js

Lines changed: 43 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
/**
1313
* This script crawls through a React Native application's dependencies and invokes the codegen
1414
* 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.
1717
*/
1818

1919
const {execFileSync, execSync} = require('child_process');
@@ -37,7 +37,7 @@ const CORE_LIBRARIES_WITH_OUTPUT_FOLDER = {
3737
rncore: path.join(REACT_NATIVE_PACKAGE_ROOT_FOLDER, 'ReactCommon'),
3838
FBReactNativeSpec: null,
3939
};
40-
const REACT_NATIVE_DEPENDENCY_NAME = 'react-native';
40+
const REACT_NATIVE = 'react-native';
4141

4242
// HELPERS
4343

@@ -58,12 +58,16 @@ function isAppRootValid(appRootDir) {
5858
return true;
5959
}
6060

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));
6367
}
6468

6569
function printDeprecationWarningIfNeeded(dependency) {
66-
if (dependency === REACT_NATIVE_DEPENDENCY_NAME) {
70+
if (dependency === REACT_NATIVE) {
6771
return;
6872
}
6973
console.log(`[Codegen] CodegenConfig Deprecated Setup for ${dependency}.
@@ -101,44 +105,35 @@ function printDeprecationWarningIfNeeded(dependency) {
101105
}
102106

103107
// 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 => {
110110
return {
111111
config,
112112
libraryPath: dependencyPath,
113113
};
114114
});
115115
}
116116

117-
function extractLibrariesFromJSON(
118-
configFile,
119-
codegenConfigKey,
120-
dependency,
121-
dependencyPath,
122-
) {
117+
function extractLibrariesFromJSON(configFile, dependency, dependencyPath) {
123118
var isBlocking = false;
124119
if (dependency == null) {
125-
dependency = REACT_NATIVE_DEPENDENCY_NAME;
120+
dependency = REACT_NATIVE;
126121
dependencyPath = REACT_NATIVE_PACKAGE_ROOT_FOLDER;
127122
// If we are exploring the ReactNative libraries, we want to raise an error
128123
// if the codegen is not properly configured.
129124
isBlocking = true;
130125
}
131126

132-
if (configFile[codegenConfigKey] == null) {
127+
if (configFile.codegenConfig == null) {
133128
if (isBlocking) {
134129
throw `[Codegen] Error: Could not find codegen config for ${dependency} .`;
135130
}
136131
return [];
137132
}
138133

139134
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;
142137
return [
143138
{
144139
config,
@@ -147,38 +142,20 @@ function extractLibrariesFromJSON(
147142
];
148143
} else {
149144
printDeprecationWarningIfNeeded(dependency);
150-
return extractLibrariesFromConfigurationArray(
151-
configFile,
152-
codegenConfigKey,
153-
dependencyPath,
154-
);
145+
return extractLibrariesFromConfigurationArray(configFile, dependencyPath);
155146
}
156147
}
157148

158-
function handleReactNativeCoreLibraries(
159-
codegenConfigFilename,
160-
codegenConfigKey,
161-
) {
149+
function handleReactNativeCoreLibraries() {
162150
// Handle react-native core libraries.
163151
// This is required when react-native is outside of node_modules.
164152
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),
168155
);
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);
174156
}
175157

176-
function handleThirdPartyLibraries(
177-
baseCodegenConfigFileDir,
178-
dependencies,
179-
codegenConfigFilename,
180-
codegenConfigKey,
181-
) {
158+
function handleThirdPartyLibraries(baseCodegenConfigFileDir, dependencies) {
182159
// Determine which of these are codegen-enabled libraries
183160
const configDir =
184161
baseCodegenConfigFileDir ||
@@ -189,33 +166,25 @@ function handleThirdPartyLibraries(
189166

190167
// Handle third-party libraries
191168
return Object.keys(dependencies).flatMap(dependency => {
192-
if (dependency === REACT_NATIVE_DEPENDENCY_NAME) {
169+
if (dependency === REACT_NATIVE) {
193170
// react-native should already be added.
194171
return [];
195172
}
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 {
202177
return [];
203178
}
204-
const configFile = JSON.parse(fs.readFileSync(configFilePath));
205179
return extractLibrariesFromJSON(
206180
configFile,
207-
codegenConfigKey,
208181
dependency,
209182
codegenConfigFileDir,
210183
);
211184
});
212185
}
213186

214-
function handleLibrariesFromReactNativeConfig(
215-
codegenConfigKey,
216-
codegenConfigFilename,
217-
appRootDir,
218-
) {
187+
function handleLibrariesFromReactNativeConfig(appRootDir) {
219188
const rnConfigFileName = 'react-native.config.js';
220189

221190
console.log(
@@ -242,37 +211,27 @@ function handleLibrariesFromReactNativeConfig(
242211
appRootDir,
243212
dependencyConfig.root,
244213
);
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 {
250218
return [];
251219
}
252-
const pkgJsonPath = path.join(codegenConfigFileDir, 'package.json');
253-
const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath));
254-
const configFile = JSON.parse(fs.readFileSync(configFilePath));
255220

256221
return extractLibrariesFromJSON(
257222
configFile,
258-
codegenConfigKey,
259-
pkgJson.name,
223+
configFile.name,
260224
codegenConfigFileDir,
261225
);
262226
});
263227
}
264228

265-
function handleInAppLibraries(pkgJson, codegenConfigKey, appRootDir) {
229+
function handleInAppLibraries(pkgJson, appRootDir) {
266230
console.log(
267231
'\n\n[Codegen] >>>>> Searching for codegen-enabled libraries in the app',
268232
);
269233

270-
return extractLibrariesFromJSON(
271-
pkgJson,
272-
codegenConfigKey,
273-
pkgJson.name,
274-
appRootDir,
275-
);
234+
return extractLibrariesFromJSON(pkgJson, pkgJson.name, appRootDir);
276235
}
277236

278237
// CodeGen
@@ -417,28 +376,14 @@ function createComponentProvider(schemaPaths, node) {
417376
console.log(`Generated provider in: ${outputDir}`);
418377
}
419378

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);
427381
const dependencies = {...pkgJson.dependencies, ...pkgJson.devDependencies};
428382
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),
442387
];
443388
}
444389

@@ -484,24 +429,15 @@ function cleanupEmptyFilesAndFolders(filepath) {
484429
* - setups the CLI to generate the code
485430
* - generate the code
486431
*
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.
488433
* @parameter outputPath: the base output path for the CodeGen.
489434
* @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.
492435
* @parameter baseCodegenConfigFileDir: the directory of the codeGenConfigFile.
493436
* @throws If it can't find a config file for react-native.
494437
* @throws If it can't find a CodeGen configuration in the file.
495438
* @throws If it can't find a cli for the CodeGen.
496439
*/
497-
function execute(
498-
appRootDir,
499-
outputPath,
500-
node,
501-
codegenConfigFilename,
502-
codegenConfigKey,
503-
baseCodegenConfigFileDir,
504-
) {
440+
function execute(appRootDir, outputPath, node, baseCodegenConfigFileDir) {
505441
if (!isAppRootValid(appRootDir)) {
506442
return;
507443
}
@@ -510,8 +446,6 @@ function execute(
510446
const libraries = findCodegenEnabledLibraries(
511447
appRootDir,
512448
baseCodegenConfigFileDir,
513-
codegenConfigFilename,
514-
codegenConfigKey,
515449
);
516450

517451
if (libraries.length === 0) {

packages/react-native/scripts/generate-codegen-artifacts.js

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,6 @@ const argv = yargs
2121
alias: 'outputPath',
2222
description: 'Path where generated artifacts will be output to',
2323
})
24-
.option('f', {
25-
alias: 'configFilename',
26-
default: 'package.json',
27-
description: 'The file that contains the codegen configuration.',
28-
})
29-
.option('k', {
30-
alias: 'configKey',
31-
default: 'codegenConfig',
32-
description:
33-
'The key that contains the codegen configuration in the config file.',
34-
})
3524
.option('c', {
3625
alias: 'configFileDir',
3726
default: '',
@@ -46,19 +35,9 @@ const argv = yargs
4635
.usage('Usage: $0 -p [path to app]')
4736
.demandOption(['p']).argv;
4837

49-
const CODEGEN_CONFIG_FILENAME = argv.f;
50-
const CODEGEN_CONFIG_FILE_DIR = argv.c;
51-
const CODEGEN_CONFIG_KEY = argv.k;
52-
const NODE = argv.n;
53-
54-
const appRoot = argv.path;
55-
const outputPath = argv.outputPath;
56-
5738
executor.execute(
58-
appRoot,
59-
outputPath,
60-
NODE,
61-
CODEGEN_CONFIG_FILENAME,
62-
CODEGEN_CONFIG_KEY,
63-
CODEGEN_CONFIG_FILE_DIR,
39+
argv.path,
40+
argv.outputPath,
41+
argv.nodeBinary,
42+
argv.configFileDir,
6443
);

0 commit comments

Comments
 (0)