Skip to content

Commit

Permalink
perf(autolinking): get platform's specific properties (#2379)
Browse files Browse the repository at this point in the history
* perf(autolinking): get platform's specific properties

* fix: switch param order

* fix: accept object as param in `loadConfig` function
  • Loading branch information
szymonrybczak committed Jun 3, 2024
1 parent 264d7dd commit b787c89
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 25 deletions.
26 changes: 13 additions & 13 deletions packages/cli-config/src/__tests__/index-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ test('should have a valid structure by default', () => {
reactNativePath: "."
}`,
});
const config = loadConfig(DIR);
const config = loadConfig({projectRoot: DIR});
expect(removeString(config, DIR)).toMatchSnapshot();
});

Expand All @@ -83,7 +83,7 @@ test('should return dependencies from package.json', () => {
}
}`,
});
const {dependencies} = loadConfig(DIR);
const {dependencies} = loadConfig({projectRoot: DIR});
expect(removeString(dependencies, DIR)).toMatchSnapshot();
});

Expand Down Expand Up @@ -122,7 +122,7 @@ test('should read a config of a dependency and use it to load other settings', (
}
}`,
});
const {dependencies} = loadConfig(DIR);
const {dependencies} = loadConfig({projectRoot: DIR});
expect(
removeString(dependencies['react-native-test'], DIR),
).toMatchSnapshot();
Expand Down Expand Up @@ -173,7 +173,7 @@ test('command specified in root config should overwrite command in "react-native
],
};`,
});
const {commands} = loadConfig(DIR);
const {commands} = loadConfig({projectRoot: DIR});
const commandsNames = commands.map(({name}) => name);
const commandIndex = commandsNames.indexOf('foo-command');

Expand Down Expand Up @@ -206,7 +206,7 @@ test('should merge project configuration with default values', () => {
}
}`,
});
const {dependencies} = loadConfig(DIR);
const {dependencies} = loadConfig({projectRoot: DIR});
expect(removeString(dependencies['react-native-test'], DIR)).toMatchSnapshot(
'snapshoting `react-native-test` config',
);
Expand Down Expand Up @@ -241,7 +241,7 @@ test('should load commands from "react-native-foo" and "react-native-bar" packag
}
}`,
});
const {commands} = loadConfig(DIR);
const {commands} = loadConfig({projectRoot: DIR});
expect(commands).toMatchSnapshot();
});

Expand All @@ -261,7 +261,7 @@ test('should not skip packages that have invalid configuration (to avoid breakin
}
}`,
});
const {dependencies} = loadConfig(DIR);
const {dependencies} = loadConfig({projectRoot: DIR});
expect(removeString(dependencies, DIR)).toMatchSnapshot(
'dependencies config',
);
Expand All @@ -281,7 +281,7 @@ test('does not use restricted "react-native" key to resolve config from package.
}
}`,
});
const {dependencies} = loadConfig(DIR);
const {dependencies} = loadConfig({projectRoot: DIR});
expect(dependencies).toHaveProperty('react-native-netinfo');
expect(spy).not.toHaveBeenCalled();
});
Expand Down Expand Up @@ -327,7 +327,7 @@ module.exports = {
}`,
});

const {dependencies} = loadConfig(DIR);
const {dependencies} = loadConfig({projectRoot: DIR});
expect(removeString(dependencies['local-lib'], DIR)).toMatchInlineSnapshot(`
Object {
"name": "local-lib",
Expand Down Expand Up @@ -367,7 +367,7 @@ test('should apply build types from dependency config', () => {
}
}`,
});
const {dependencies} = loadConfig(DIR);
const {dependencies} = loadConfig({projectRoot: DIR});
expect(
removeString(dependencies['react-native-test'], DIR),
).toMatchSnapshot();
Expand Down Expand Up @@ -400,7 +400,7 @@ test('supports dependencies from user configuration with custom build type', ()
}`,
});

const {dependencies} = loadConfig(DIR);
const {dependencies} = loadConfig({projectRoot: DIR});
expect(
removeString(dependencies['react-native-test'], DIR),
).toMatchSnapshot();
Expand Down Expand Up @@ -429,7 +429,7 @@ test('supports disabling dependency for ios platform', () => {
}`,
});

const {dependencies} = loadConfig(DIR);
const {dependencies} = loadConfig({projectRoot: DIR});
expect(
removeString(dependencies['react-native-test'], DIR),
).toMatchSnapshot();
Expand Down Expand Up @@ -494,7 +494,7 @@ test('should convert project sourceDir relative path to absolute', () => {
`,
});

const config = loadConfig(DIR);
const config = loadConfig({projectRoot: DIR});

expect(config.project.ios?.sourceDir).toBe(path.join(DIR, iosProjectDir));
expect(config.project.android?.sourceDir).toBe(
Expand Down
6 changes: 6 additions & 0 deletions packages/cli-config/src/commands/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ function filterConfig(config: Config) {
export default {
name: 'config',
description: 'Print CLI configuration',
options: [
{
name: '--platform <platform>',
description: 'Output configuration for a specific platform',
},
],
func: async (_argv: string[], ctx: Config) => {
console.log(JSON.stringify(filterConfig(ctx), null, 2));
},
Expand Down
18 changes: 11 additions & 7 deletions packages/cli-config/src/loadConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ function getDependencyConfig(
finalConfig: Config,
config: UserDependencyConfig,
userConfig: UserConfig,
isPlatform: boolean,
): DependencyConfig {
return merge(
{
Expand All @@ -39,7 +38,7 @@ function getDependencyConfig(
const platformConfig = finalConfig.platforms[platform];
dependency[platform] =
// Linking platforms is not supported
isPlatform || !platformConfig
Object.keys(config.platforms).length > 0 || !platformConfig
? null
: platformConfig.dependencyConfig(
root,
Expand Down Expand Up @@ -86,7 +85,13 @@ const removeDuplicateCommands = <T extends boolean>(commands: Command<T>[]) => {
/**
* Loads CLI configuration
*/
function loadConfig(projectRoot: string = findProjectRoot()): Config {
function loadConfig({
projectRoot = findProjectRoot(),
selectedPlatform,
}: {
projectRoot?: string;
selectedPlatform?: string;
}): Config {
let lazyProject: ProjectConfig;
const userConfig = readConfigFromDisk(projectRoot);

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

const isPlatform = Object.keys(config.platforms).length > 0;

return assign({}, acc, {
dependencies: assign({}, acc.dependencies, {
get [dependencyName](): DependencyConfig {
Expand All @@ -151,7 +154,6 @@ function loadConfig(projectRoot: string = findProjectRoot()): Config {
finalConfig,
config,
userConfig,
isPlatform,
);
},
}),
Expand All @@ -161,7 +163,9 @@ function loadConfig(projectRoot: string = findProjectRoot()): Config {
]),
platforms: {
...acc.platforms,
...config.platforms,
...(selectedPlatform && config.platforms[selectedPlatform]
? {[selectedPlatform]: config.platforms[selectedPlatform]}
: config.platforms),
},
healthChecks: [...acc.healthChecks, ...config.healthChecks],
}) as Config;
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-doctor/src/commands/__tests__/info.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ beforeEach(() => {
jest.resetAllMocks();
});

const config = loadConfig();
const config = loadConfig({});

test('prints output without arguments', async () => {
await info.func([], config);
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-doctor/src/tools/healthchecks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const getHealthchecks = ({contributor}: Options): Healthchecks => {

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

if (config.reactNativePath) {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-platform-android/native_modules.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ class ReactNativeModules {
String[] nodeCommand = ["node", "-e", cliResolveScript]
def cliPath = this.getCommandOutput(nodeCommand, this.root)

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

def json
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-platform-ios/native_modules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def use_native_modules!(config = nil)
if (!config)
json = []

IO.popen(["node", cli_bin, "config"]) do |data|
IO.popen(["node", cli_bin, "config", '--platform', 'ios']) do |data|
while line = data.gets
json << line
end
Expand Down
18 changes: 17 additions & 1 deletion packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,23 @@ async function setupAndRun(platformName?: string) {

let config: Config | undefined;
try {
config = loadConfig();
let selectedPlatform: string | undefined;

/*
When linking dependencies in iOS and Android build we're passing `--platform` argument,
to only load the configuration for the specific platform.
*/
if (isCommandPassed('config')) {
const platformIndex = process.argv.indexOf('--platform');

if (platformIndex !== -1 && platformIndex < process.argv.length - 1) {
selectedPlatform = process.argv[platformIndex + 1];
}
}

config = loadConfig({
selectedPlatform,
});

logger.enable();

Expand Down

0 comments on commit b787c89

Please sign in to comment.