Skip to content

Commit b787c89

Browse files
perf(autolinking): get platform's specific properties (#2379)
* perf(autolinking): get platform's specific properties * fix: switch param order * fix: accept object as param in `loadConfig` function
1 parent 264d7dd commit b787c89

File tree

8 files changed

+51
-25
lines changed

8 files changed

+51
-25
lines changed

packages/cli-config/src/__tests__/index-test.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ test('should have a valid structure by default', () => {
6666
reactNativePath: "."
6767
}`,
6868
});
69-
const config = loadConfig(DIR);
69+
const config = loadConfig({projectRoot: DIR});
7070
expect(removeString(config, DIR)).toMatchSnapshot();
7171
});
7272

@@ -83,7 +83,7 @@ test('should return dependencies from package.json', () => {
8383
}
8484
}`,
8585
});
86-
const {dependencies} = loadConfig(DIR);
86+
const {dependencies} = loadConfig({projectRoot: DIR});
8787
expect(removeString(dependencies, DIR)).toMatchSnapshot();
8888
});
8989

@@ -122,7 +122,7 @@ test('should read a config of a dependency and use it to load other settings', (
122122
}
123123
}`,
124124
});
125-
const {dependencies} = loadConfig(DIR);
125+
const {dependencies} = loadConfig({projectRoot: DIR});
126126
expect(
127127
removeString(dependencies['react-native-test'], DIR),
128128
).toMatchSnapshot();
@@ -173,7 +173,7 @@ test('command specified in root config should overwrite command in "react-native
173173
],
174174
};`,
175175
});
176-
const {commands} = loadConfig(DIR);
176+
const {commands} = loadConfig({projectRoot: DIR});
177177
const commandsNames = commands.map(({name}) => name);
178178
const commandIndex = commandsNames.indexOf('foo-command');
179179

@@ -206,7 +206,7 @@ test('should merge project configuration with default values', () => {
206206
}
207207
}`,
208208
});
209-
const {dependencies} = loadConfig(DIR);
209+
const {dependencies} = loadConfig({projectRoot: DIR});
210210
expect(removeString(dependencies['react-native-test'], DIR)).toMatchSnapshot(
211211
'snapshoting `react-native-test` config',
212212
);
@@ -241,7 +241,7 @@ test('should load commands from "react-native-foo" and "react-native-bar" packag
241241
}
242242
}`,
243243
});
244-
const {commands} = loadConfig(DIR);
244+
const {commands} = loadConfig({projectRoot: DIR});
245245
expect(commands).toMatchSnapshot();
246246
});
247247

@@ -261,7 +261,7 @@ test('should not skip packages that have invalid configuration (to avoid breakin
261261
}
262262
}`,
263263
});
264-
const {dependencies} = loadConfig(DIR);
264+
const {dependencies} = loadConfig({projectRoot: DIR});
265265
expect(removeString(dependencies, DIR)).toMatchSnapshot(
266266
'dependencies config',
267267
);
@@ -281,7 +281,7 @@ test('does not use restricted "react-native" key to resolve config from package.
281281
}
282282
}`,
283283
});
284-
const {dependencies} = loadConfig(DIR);
284+
const {dependencies} = loadConfig({projectRoot: DIR});
285285
expect(dependencies).toHaveProperty('react-native-netinfo');
286286
expect(spy).not.toHaveBeenCalled();
287287
});
@@ -327,7 +327,7 @@ module.exports = {
327327
}`,
328328
});
329329

330-
const {dependencies} = loadConfig(DIR);
330+
const {dependencies} = loadConfig({projectRoot: DIR});
331331
expect(removeString(dependencies['local-lib'], DIR)).toMatchInlineSnapshot(`
332332
Object {
333333
"name": "local-lib",
@@ -367,7 +367,7 @@ test('should apply build types from dependency config', () => {
367367
}
368368
}`,
369369
});
370-
const {dependencies} = loadConfig(DIR);
370+
const {dependencies} = loadConfig({projectRoot: DIR});
371371
expect(
372372
removeString(dependencies['react-native-test'], DIR),
373373
).toMatchSnapshot();
@@ -400,7 +400,7 @@ test('supports dependencies from user configuration with custom build type', ()
400400
}`,
401401
});
402402

403-
const {dependencies} = loadConfig(DIR);
403+
const {dependencies} = loadConfig({projectRoot: DIR});
404404
expect(
405405
removeString(dependencies['react-native-test'], DIR),
406406
).toMatchSnapshot();
@@ -429,7 +429,7 @@ test('supports disabling dependency for ios platform', () => {
429429
}`,
430430
});
431431

432-
const {dependencies} = loadConfig(DIR);
432+
const {dependencies} = loadConfig({projectRoot: DIR});
433433
expect(
434434
removeString(dependencies['react-native-test'], DIR),
435435
).toMatchSnapshot();
@@ -494,7 +494,7 @@ test('should convert project sourceDir relative path to absolute', () => {
494494
`,
495495
});
496496

497-
const config = loadConfig(DIR);
497+
const config = loadConfig({projectRoot: DIR});
498498

499499
expect(config.project.ios?.sourceDir).toBe(path.join(DIR, iosProjectDir));
500500
expect(config.project.android?.sourceDir).toBe(

packages/cli-config/src/commands/config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ function filterConfig(config: Config) {
2121
export default {
2222
name: 'config',
2323
description: 'Print CLI configuration',
24+
options: [
25+
{
26+
name: '--platform <platform>',
27+
description: 'Output configuration for a specific platform',
28+
},
29+
],
2430
func: async (_argv: string[], ctx: Config) => {
2531
console.log(JSON.stringify(filterConfig(ctx), null, 2));
2632
},

packages/cli-config/src/loadConfig.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ function getDependencyConfig(
2828
finalConfig: Config,
2929
config: UserDependencyConfig,
3030
userConfig: UserConfig,
31-
isPlatform: boolean,
3231
): DependencyConfig {
3332
return merge(
3433
{
@@ -39,7 +38,7 @@ function getDependencyConfig(
3938
const platformConfig = finalConfig.platforms[platform];
4039
dependency[platform] =
4140
// Linking platforms is not supported
42-
isPlatform || !platformConfig
41+
Object.keys(config.platforms).length > 0 || !platformConfig
4342
? null
4443
: platformConfig.dependencyConfig(
4544
root,
@@ -86,7 +85,13 @@ const removeDuplicateCommands = <T extends boolean>(commands: Command<T>[]) => {
8685
/**
8786
* Loads CLI configuration
8887
*/
89-
function loadConfig(projectRoot: string = findProjectRoot()): Config {
88+
function loadConfig({
89+
projectRoot = findProjectRoot(),
90+
selectedPlatform,
91+
}: {
92+
projectRoot?: string;
93+
selectedPlatform?: string;
94+
}): Config {
9095
let lazyProject: ProjectConfig;
9196
const userConfig = readConfigFromDisk(projectRoot);
9297

@@ -140,8 +145,6 @@ function loadConfig(projectRoot: string = findProjectRoot()): Config {
140145
resolveNodeModuleDir(projectRoot, dependencyName);
141146
let config = readDependencyConfigFromDisk(root, dependencyName);
142147

143-
const isPlatform = Object.keys(config.platforms).length > 0;
144-
145148
return assign({}, acc, {
146149
dependencies: assign({}, acc.dependencies, {
147150
get [dependencyName](): DependencyConfig {
@@ -151,7 +154,6 @@ function loadConfig(projectRoot: string = findProjectRoot()): Config {
151154
finalConfig,
152155
config,
153156
userConfig,
154-
isPlatform,
155157
);
156158
},
157159
}),
@@ -161,7 +163,9 @@ function loadConfig(projectRoot: string = findProjectRoot()): Config {
161163
]),
162164
platforms: {
163165
...acc.platforms,
164-
...config.platforms,
166+
...(selectedPlatform && config.platforms[selectedPlatform]
167+
? {[selectedPlatform]: config.platforms[selectedPlatform]}
168+
: config.platforms),
165169
},
166170
healthChecks: [...acc.healthChecks, ...config.healthChecks],
167171
}) as Config;

packages/cli-doctor/src/commands/__tests__/info.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ beforeEach(() => {
1414
jest.resetAllMocks();
1515
});
1616

17-
const config = loadConfig();
17+
const config = loadConfig({});
1818

1919
test('prints output without arguments', async () => {
2020
await info.func([], config);

packages/cli-doctor/src/tools/healthchecks/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const getHealthchecks = ({contributor}: Options): Healthchecks => {
3636

3737
// Doctor can run in a detached mode, where there isn't a config so this can fail
3838
try {
39-
config = loadConfig();
39+
config = loadConfig({});
4040
additionalChecks = config.healthChecks;
4141

4242
if (config.reactNativePath) {

packages/cli-platform-android/native_modules.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ class ReactNativeModules {
432432
String[] nodeCommand = ["node", "-e", cliResolveScript]
433433
def cliPath = this.getCommandOutput(nodeCommand, this.root)
434434

435-
String[] reactNativeConfigCommand = ["node", cliPath, "config"]
435+
String[] reactNativeConfigCommand = ["node", cliPath, "config", "--platform", "android"]
436436
def reactNativeConfigOutput = this.getCommandOutput(reactNativeConfigCommand, this.root)
437437

438438
def json

packages/cli-platform-ios/native_modules.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def use_native_modules!(config = nil)
2828
if (!config)
2929
json = []
3030

31-
IO.popen(["node", cli_bin, "config"]) do |data|
31+
IO.popen(["node", cli_bin, "config", '--platform', 'ios']) do |data|
3232
while line = data.gets
3333
json << line
3434
end

packages/cli/src/index.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,23 @@ async function setupAndRun(platformName?: string) {
179179

180180
let config: Config | undefined;
181181
try {
182-
config = loadConfig();
182+
let selectedPlatform: string | undefined;
183+
184+
/*
185+
When linking dependencies in iOS and Android build we're passing `--platform` argument,
186+
to only load the configuration for the specific platform.
187+
*/
188+
if (isCommandPassed('config')) {
189+
const platformIndex = process.argv.indexOf('--platform');
190+
191+
if (platformIndex !== -1 && platformIndex < process.argv.length - 1) {
192+
selectedPlatform = process.argv[platformIndex + 1];
193+
}
194+
}
195+
196+
config = loadConfig({
197+
selectedPlatform,
198+
});
183199

184200
logger.enable();
185201

0 commit comments

Comments
 (0)