Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/cli-config/src/__tests__/index-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -578,3 +578,22 @@ test('should convert project sourceDir relative path to absolute', async () => {
path.join(DIR, androidProjectDir),
);
});

test('should be able to resolve platform-specific react-native path', async () => {
DIR = getTempDirectory('config_test_apply_platform_react_native_path');
writeFiles(DIR, {
...REACT_NATIVE_MOCK,
...PLATFORM_MOCK,
'package.json': `{
"dependencies": {
"react-native": "0.0.1",
"react-native-os": "0.0.1"
}
}`,
});
const {reactNativePath} = await loadConfigAsync({
projectRoot: DIR,
reactNativePackageName: 'react-native-os',
});
expect(reactNativePath).toMatch(/\/react-native-os$/);
});
5 changes: 5 additions & 0 deletions packages/cli-config/src/commands/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ export default {
name: '--platform <platform>',
description: 'Output configuration for a specific platform',
},
{
name: '--react-native-package-name <platform>',
description:
'Optional package name to use when resolving react-native, passable by out-of-tree platforms.',
},
],
func: async (_argv: string[], ctx: Config) => {
console.log(JSON.stringify(filterConfig(ctx), null, 2));
Expand Down
8 changes: 6 additions & 2 deletions packages/cli-config/src/loadConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ const removeDuplicateCommands = <T extends boolean>(commands: Command<T>[]) => {
export default function loadConfig({
projectRoot = findProjectRoot(),
selectedPlatform,
reactNativePackageName,
}: {
projectRoot?: string;
selectedPlatform?: string;
reactNativePackageName?: string;
}): Config {
let lazyProject: ProjectConfig;
const userConfig = readConfigFromDisk(projectRoot);
Expand All @@ -102,7 +104,7 @@ export default function loadConfig({
get reactNativePath() {
return userConfig.reactNativePath
? path.resolve(projectRoot, userConfig.reactNativePath)
: resolveReactNativePath(projectRoot);
: resolveReactNativePath(projectRoot, reactNativePackageName);
},
get reactNativeVersion() {
return getReactNativeVersion(initialConfig.reactNativePath);
Expand Down Expand Up @@ -188,9 +190,11 @@ export default function loadConfig({
export async function loadConfigAsync({
projectRoot = findProjectRoot(),
selectedPlatform,
reactNativePackageName,
}: {
projectRoot?: string;
selectedPlatform?: string;
reactNativePackageName?: string;
}): Promise<Config> {
let lazyProject: ProjectConfig;
const userConfig = await readConfigFromDiskAsync(projectRoot);
Expand All @@ -200,7 +204,7 @@ export async function loadConfigAsync({
get reactNativePath() {
return userConfig.reactNativePath
? path.resolve(projectRoot, userConfig.reactNativePath)
: resolveReactNativePath(projectRoot);
: resolveReactNativePath(projectRoot, reactNativePackageName);
},
get reactNativeVersion() {
return getReactNativeVersion(initialConfig.reactNativePath);
Expand Down
9 changes: 6 additions & 3 deletions packages/cli-config/src/resolveReactNativePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import {
* Finds path to React Native inside `node_modules` or throws
* an error otherwise.
*/
export default function resolveReactNativePath(root: string) {
export default function resolveReactNativePath(
root: string,
reactNativePackageName = 'react-native',
) {
try {
return resolveNodeModuleDir(root, 'react-native');
return resolveNodeModuleDir(root, reactNativePackageName);
} catch (_ignored) {
throw new CLIError(`
Unable to find React Native files looking up from ${root}. Make sure "react-native" module is installed
Unable to find React Native files looking up from ${root}. Make sure "${reactNativePackageName}" module is installed
in your project dependencies.

If you are using React Native from a non-standard location, consider setting:
Expand Down
13 changes: 13 additions & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ async function setupAndRun(platformName?: string) {
let config: Config | undefined;
try {
let selectedPlatform: string | undefined;
let reactNativePackageName: string | undefined;

/*
When linking dependencies in iOS and Android build we're passing `--platform` argument,
Expand All @@ -193,10 +194,22 @@ async function setupAndRun(platformName?: string) {
if (platformIndex !== -1 && platformIndex < process.argv.length - 1) {
selectedPlatform = process.argv[platformIndex + 1];
}

const reactNativePackageNameIndex = process.argv.indexOf(
'--react-native-package-name',
);

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

config = await loadConfigAsync({
selectedPlatform,
reactNativePackageName,
});

logger.enable();
Expand Down
Loading