Skip to content

Commit 4a3ca21

Browse files
authored
fix: pass user config to commands and config from rnccli (#265)
* fix: pass user config to commands and config from rnccli * changeset * adjust comment * update docs * docs
1 parent 0a64b81 commit 4a3ca21

File tree

17 files changed

+143
-42
lines changed

17 files changed

+143
-42
lines changed

.changeset/dirty-parents-repair.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
'@rnef/plugin-brownfield-android': patch
3+
'@rnef/platform-apple-helpers': patch
4+
'@rnef/plugin-brownfield-ios': patch
5+
'@rnef/platform-android': patch
6+
'@rnef/platform-ios': patch
7+
'@rnef/config': patch
8+
'@rnef/cli': patch
9+
---
10+
11+
fix: pass user config to commands and config from rnccli

packages/cli/src/config.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type {
22
Config,
33
DependencyConfig,
44
} from '@react-native-community/cli-types';
5+
import type { ConfigOutput } from '@rnef/config';
56

67
function isValidRNDependency(config: DependencyConfig) {
78
return (
@@ -25,13 +26,29 @@ function filterConfig(config: Config) {
2526
return filtered;
2627
}
2728

28-
export const logConfig = async (options: { platform?: string }) => {
29+
export const logConfig = async (
30+
args: { platform?: string },
31+
ownConfig: ConfigOutput
32+
) => {
2933
const { loadConfigAsync } = await import(
3034
'@react-native-community/cli-config'
3135
);
3236
const config = await loadConfigAsync({
33-
selectedPlatform: options.platform,
37+
selectedPlatform: args.platform,
3438
});
3539

40+
for (const platform in ownConfig.platforms) {
41+
for (const projectEntry in config.project[platform]) {
42+
if (
43+
ownConfig.platforms[platform].autolinkingConfig &&
44+
projectEntry in ownConfig.platforms[platform].autolinkingConfig
45+
) {
46+
config.project[platform][projectEntry] =
47+
// @ts-expect-error todo: type it better
48+
ownConfig.platforms[platform].autolinkingConfig[projectEntry];
49+
}
50+
}
51+
}
52+
3653
console.log(JSON.stringify(filterConfig(config), null, 2));
3754
};

packages/cli/src/lib/cli.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ export const cli = async ({ cwd, argv }: CliOptions = {}) => {
3131
.option('--verbose', 'enable verbose logging')
3232
.version(version);
3333

34+
// Register commands from the config
35+
const config = await getConfig(cwd);
36+
3437
program
3538
.command('config')
3639
.option('-p, --platform <string>', 'Select platform, e.g. ios or android')
37-
.action(logConfig);
38-
39-
// Register commands from the config
40-
const config = await getConfig(cwd);
40+
.action((args) => logConfig(args, config));
4141

4242
program
4343
.command('fingerprint [path]')

packages/config/src/lib/config.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ export type PluginOutput = {
1111
description: string;
1212
};
1313

14+
export type PlatformOutput = PluginOutput & {
15+
autolinkingConfig: object | undefined;
16+
};
17+
1418
export type PluginApi = {
1519
registerCommand: (command: CommandType) => void;
1620
getProjectRoot: () => string;
@@ -28,6 +32,8 @@ type SupportedRemoteCacheProviders = 'github-actions';
2832

2933
type PluginType = (args: PluginApi) => PluginOutput;
3034

35+
type PlatformType = (args: PluginApi) => PlatformOutput;
36+
3137
type ArgValue = string | string[] | number | boolean;
3238

3339
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -60,7 +66,7 @@ export type ConfigType = {
6066
reactNativePath?: string;
6167
bundler?: PluginType;
6268
plugins?: PluginType[];
63-
platforms?: Record<string, PluginType>;
69+
platforms?: Record<string, PlatformType>;
6470
commands?: Array<CommandType>;
6571
remoteCacheProvider?: SupportedRemoteCacheProviders;
6672
fingerprint?: {
@@ -69,8 +75,9 @@ export type ConfigType = {
6975
};
7076
};
7177

72-
type ConfigOutput = {
78+
export type ConfigOutput = {
7379
commands?: Array<CommandType>;
80+
platforms?: Record<string, PlatformOutput>;
7481
} & PluginApi;
7582

7683
const extensions = ['.js', '.ts', '.mjs'];
@@ -147,10 +154,11 @@ export async function getConfig(
147154
getReactNativePath: () => config.reactNativePath as string,
148155
getPlatforms: () => config.platforms as { [platform: string]: object },
149156
getRemoteCacheProvider: () => config.remoteCacheProvider,
150-
getFingerprintOptions: () => config.fingerprint as {
151-
extraSources: string[];
152-
ignorePaths: string[];
153-
},
157+
getFingerprintOptions: () =>
158+
config.fingerprint as {
159+
extraSources: string[];
160+
ignorePaths: string[];
161+
},
154162
};
155163

156164
if (config.plugins) {
@@ -160,10 +168,12 @@ export async function getConfig(
160168
}
161169
}
162170

171+
const platforms: Record<string, PlatformOutput> = {};
163172
if (config.platforms) {
164173
// platforms register commands and custom platform functionality (TBD)
165174
for (const platform in config.platforms) {
166-
config.platforms[platform](api);
175+
const platformOutput = config.platforms[platform](api);
176+
platforms[platform] = platformOutput;
167177
}
168178
}
169179

@@ -173,6 +183,7 @@ export async function getConfig(
173183

174184
const outputConfig: ConfigOutput = {
175185
commands: config.commands ?? [],
186+
platforms: platforms ?? {},
176187
...api,
177188
};
178189

packages/platform-android/src/lib/commands/buildAndroid/command.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { projectConfig } from '@react-native-community/cli-config-android';
2+
import type { AndroidProjectConfig } from '@react-native-community/cli-types';
23
import type { PluginApi } from '@rnef/config';
34
import { RnefError } from '@rnef/tools';
45
import type { BuildFlags } from './buildAndroid.js';
56
import { buildAndroid, options } from './buildAndroid.js';
67

7-
export function registerBuildCommand(api: PluginApi) {
8+
export function registerBuildCommand(api: PluginApi, pluginConfig?: AndroidProjectConfig) {
89
api.registerCommand({
910
name: 'build:android',
1011
description: 'Builds your app for Android platform.',
1112
action: async (args) => {
1213
const projectRoot = api.getProjectRoot();
13-
const androidConfig = projectConfig(projectRoot);
14+
const androidConfig = projectConfig(projectRoot, pluginConfig);
1415
if (androidConfig) {
1516
await buildAndroid(androidConfig, args as BuildFlags);
1617
} else {

packages/platform-android/src/lib/commands/generateKeystore.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ import {
1414
spawn,
1515
} from '@rnef/tools';
1616

17-
export function registerCreateKeystoreCommand(api: PluginApi) {
17+
export function registerCreateKeystoreCommand(api: PluginApi, pluginConfig?: AndroidProjectConfig) {
1818
api.registerCommand({
1919
name: 'create-keystore:android',
2020
description: 'Creates a keystore file for signing Android release builds.',
2121
action: async (args) => {
2222
const projectRoot = api.getProjectRoot();
23-
const androidConfig = projectConfig(projectRoot);
23+
const androidConfig = projectConfig(projectRoot, pluginConfig);
2424
if (androidConfig) {
2525
await generateKeystore(androidConfig, args);
2626
} else {

packages/platform-android/src/lib/commands/runAndroid/command.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ export function registerRunCommand(
1515
'Builds your app and starts it on a connected Android emulator or a device.',
1616
action: async (args) => {
1717
const projectRoot = api.getProjectRoot();
18-
const androidConfig = projectConfig(projectRoot);
18+
const androidConfig = projectConfig(projectRoot, pluginConfig);
1919
if (androidConfig) {
2020
await runAndroid(
2121
androidConfig,
22-
{ ...pluginConfig, ...(args as Flags) },
22+
args as Flags,
2323
projectRoot,
2424
api.getRemoteCacheProvider(),
2525
api.getFingerprintOptions()

packages/platform-android/src/lib/platformAndroid.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import path from 'node:path';
12
import type { AndroidProjectConfig } from '@react-native-community/cli-types';
2-
import type { PluginApi, PluginOutput } from '@rnef/config';
3+
import type { PlatformOutput, PluginApi } from '@rnef/config';
34
import { registerBuildCommand } from './commands/buildAndroid/command.js';
45
import { registerCreateKeystoreCommand } from './commands/generateKeystore.js';
56
import { registerRunCommand } from './commands/runAndroid/command.js';
@@ -9,15 +10,21 @@ type PluginConfig = AndroidProjectConfig;
910

1011
export const platformAndroid =
1112
(pluginConfig?: PluginConfig) =>
12-
(api: PluginApi): PluginOutput => {
13-
registerBuildCommand(api);
13+
(api: PluginApi): PlatformOutput => {
14+
registerBuildCommand(api, pluginConfig);
1415
registerRunCommand(api, pluginConfig);
15-
registerCreateKeystoreCommand(api);
16+
registerCreateKeystoreCommand(api, pluginConfig);
1617
registerSignCommand(api);
1718

1819
return {
1920
name: '@rnef/platform-android',
2021
description: 'RNEF plugin for everything Android.',
22+
autolinkingConfig: {
23+
...pluginConfig,
24+
sourceDir: pluginConfig?.sourceDir
25+
? path.join(api.getProjectRoot(), pluginConfig.sourceDir)
26+
: undefined,
27+
},
2128
};
2229
};
2330

packages/platform-apple-helpers/src/lib/utils/buildApp.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from 'node:path';
2+
import type { IOSProjectConfig } from '@react-native-community/cli-types';
23
import { RnefError } from '@rnef/tools';
34
import type { BuildFlags } from '../commands/build/buildOptions.js';
45
import { buildProject } from '../commands/build/buildProject.js';
@@ -15,13 +16,15 @@ import { installPodsIfNeeded } from './pods.js';
1516
export async function buildApp({
1617
args,
1718
projectConfig,
19+
pluginConfig,
1820
platformName,
1921
platformSDK,
2022
udid,
2123
projectRoot,
2224
}: {
2325
args: RunFlags | BuildFlags;
2426
projectConfig: ProjectConfig;
27+
pluginConfig?: IOSProjectConfig;
2528
platformName: ApplePlatform;
2629
platformSDK: PlatformSDK;
2730
udid?: string;
@@ -51,7 +54,7 @@ export async function buildApp({
5154
// because running pods install might have generated .xcworkspace project.
5255
// This should be only case in new project.
5356
if (xcodeProject.isWorkspace === false) {
54-
const newProjectConfig = getValidProjectConfig(platformName, projectRoot);
57+
const newProjectConfig = getValidProjectConfig(platformName, projectRoot, pluginConfig);
5558
xcodeProject = newProjectConfig.xcodeProject;
5659
sourceDir = newProjectConfig.sourceDir;
5760
}

packages/platform-ios/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
},
1818
"dependencies": {
1919
"@react-native-community/cli-config-apple": "^16.0.2",
20+
"@react-native-community/cli-types": "^16.0.2",
2021
"@rnef/platform-apple-helpers": "^0.6.0",
2122
"@rnef/tools": "^0.6.0",
2223
"tslib": "^2.3.0"

0 commit comments

Comments
 (0)