Skip to content

Commit fb78816

Browse files
authored
fix: config with --platform flag to work with platforms defined in multiple packages (#2562)
1 parent 5075c8f commit fb78816

File tree

3 files changed

+86
-5
lines changed

3 files changed

+86
-5
lines changed

.vscode/settings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"**/build": true
99
},
1010
"editor.codeActionsOnSave": {
11-
"source.fixAll.eslint": true
11+
"source.fixAll.eslint": "explicit"
1212
},
1313
"flow.useNPMPackagedFlow": true,
1414
"javascript.validate.enable": false,

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

+79-2
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@ const REACT_NATIVE_MOCK = {
2424
module.exports = {
2525
platforms: {
2626
ios: {
27-
linkConfig: ios.linkConfig,
2827
projectConfig: ios.projectConfig,
2928
dependencyConfig: ios.dependencyConfig,
3029
},
3130
android: {
32-
linkConfig: android.linkConfig,
3331
projectConfig: android.projectConfig,
3432
dependencyConfig: android.dependencyConfig,
3533
},
@@ -38,6 +36,21 @@ const REACT_NATIVE_MOCK = {
3836
`,
3937
};
4038

39+
const PLATFORM_MOCK = {
40+
'node_modules/react-native-os/package.json': '{}',
41+
'node_modules/react-native-os/react-native.config.js': `
42+
const os = require("${iosPath}");
43+
module.exports = {
44+
platforms: {
45+
os: {
46+
projectConfig: os.projectConfig,
47+
dependencyConfig: os.dependencyConfig,
48+
},
49+
},
50+
};
51+
`,
52+
};
53+
4154
// Removes string from all key/values within an object
4255
const removeString = (config, str) =>
4356
JSON.parse(
@@ -373,6 +386,70 @@ test('should apply build types from dependency config', async () => {
373386
).toMatchSnapshot();
374387
});
375388

389+
test('should be able to read multiple platforms from many packages', async () => {
390+
DIR = getTempDirectory('config_test_apply_dependency_config');
391+
writeFiles(DIR, {
392+
...REACT_NATIVE_MOCK,
393+
...PLATFORM_MOCK,
394+
'package.json': `{
395+
"dependencies": {
396+
"react-native": "0.0.1",
397+
"react-native-os": "0.0.1"
398+
}
399+
}`,
400+
});
401+
const {platforms} = await loadConfigAsync({projectRoot: DIR});
402+
expect(removeString(platforms, DIR)).toMatchInlineSnapshot(`
403+
Object {
404+
"android": Object {},
405+
"ios": Object {},
406+
"os": Object {},
407+
}
408+
`);
409+
});
410+
411+
test('should be able to read only selected platform', async () => {
412+
DIR = getTempDirectory('config_test_apply_dependency_config');
413+
writeFiles(DIR, {
414+
...REACT_NATIVE_MOCK,
415+
...PLATFORM_MOCK,
416+
'package.json': `{
417+
"dependencies": {
418+
"react-native": "0.0.1",
419+
"react-native-os": "0.0.1"
420+
}
421+
}`,
422+
});
423+
const {platforms} = await loadConfigAsync({
424+
projectRoot: DIR,
425+
selectedPlatform: 'os',
426+
});
427+
expect(removeString(platforms, DIR)).toMatchInlineSnapshot(`
428+
Object {
429+
"os": Object {},
430+
}
431+
`);
432+
});
433+
434+
test('should be able to read no platforms when non-existent selected', async () => {
435+
DIR = getTempDirectory('config_test_apply_dependency_config');
436+
writeFiles(DIR, {
437+
...REACT_NATIVE_MOCK,
438+
...PLATFORM_MOCK,
439+
'package.json': `{
440+
"dependencies": {
441+
"react-native": "0.0.1",
442+
"react-native-os": "0.0.1"
443+
}
444+
}`,
445+
});
446+
const {platforms} = await loadConfigAsync({
447+
projectRoot: DIR,
448+
selectedPlatform: 'macos',
449+
});
450+
expect(removeString(platforms, DIR)).toMatchInlineSnapshot(`Object {}`);
451+
});
452+
376453
test('supports dependencies from user configuration with custom build type', async () => {
377454
DIR = getTempDirectory('config_test_apply_custom_build_config');
378455
writeFiles(DIR, {

packages/cli-config/src/loadConfig.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,9 @@ export default function loadConfig({
167167
...acc.platforms,
168168
...(selectedPlatform && config.platforms[selectedPlatform]
169169
? {[selectedPlatform]: config.platforms[selectedPlatform]}
170-
: config.platforms),
170+
: !selectedPlatform
171+
? config.platforms
172+
: undefined),
171173
},
172174
healthChecks: [...acc.healthChecks, ...config.healthChecks],
173175
}) as Config;
@@ -267,7 +269,9 @@ export async function loadConfigAsync({
267269
...acc.platforms,
268270
...(selectedPlatform && config.platforms[selectedPlatform]
269271
? {[selectedPlatform]: config.platforms[selectedPlatform]}
270-
: config.platforms),
272+
: !selectedPlatform
273+
? config.platforms
274+
: undefined),
271275
},
272276
healthChecks: [...acc.healthChecks, ...config.healthChecks],
273277
}) as Config;

0 commit comments

Comments
 (0)