Skip to content

Commit bcdfcd7

Browse files
authored
feat: convert command options and dependencyConfig into getters (#2226)
1 parent ae056a1 commit bcdfcd7

File tree

10 files changed

+178
-136
lines changed

10 files changed

+178
-136
lines changed

packages/cli-platform-apple/README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Inside of `<oot-platform>/packages/react-native/react-native.config.js`:
1818

1919
```js
2020
const {
21-
buildOptions,
21+
getBuildOptions,
2222
createBuild,
2323
} = require('@react-native-community/cli-platform-apple');
2424

@@ -32,11 +32,29 @@ const buildVisionOS = {
3232
cmd: 'npx react-native build-visionos --mode "Release"',
3333
},
3434
],
35-
options: buildOptions,
35+
options: getBuildOptions({platformName: 'visionos'}),
3636
};
3737

3838
module.exports = {
3939
commands: [buildVisionOS], // <- Add command here
4040
//..
4141
};
4242
```
43+
44+
`cli-platform-apple` also exports utilities to create OOT platform config.
45+
46+
- `getProjectConfig()` - creates project config for given platform
47+
- `getDependencyConfg()` - creates dependency config for given platform
48+
49+
Example (`<oot-platform>/packages/react-native/react-native.config.js`):
50+
51+
```js
52+
platforms: {
53+
visionos: {
54+
npmPackageName: '@callstack/react-native-visionos',
55+
projectConfig: getProjectConfig({platformName: 'visionos'}),
56+
dependencyConfig: getDependencyConfg({platformName: 'visionos'}),
57+
},
58+
..
59+
},
60+
```
Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import {BuilderCommand} from '../../types';
2+
import {getPlatformInfo} from '../runCommand/getPlatformInfo';
3+
14
export type BuildFlags = {
25
mode?: string;
36
target?: string;
@@ -11,49 +14,51 @@ export type BuildFlags = {
1114
forcePods?: boolean;
1215
};
1316

14-
export const buildOptions = [
15-
{
16-
name: '--mode <string>',
17-
description:
18-
'Explicitly set the scheme configuration to use. This option is case sensitive.',
19-
},
20-
{
21-
name: '--scheme <string>',
22-
description: 'Explicitly set Xcode scheme to use',
23-
},
24-
{
25-
name: '--destination <string>',
26-
description: 'Explicitly extend destination e.g. "arch=x86_64"',
27-
},
28-
{
29-
name: '--verbose',
30-
description: 'Do not use xcbeautify or xcpretty even if installed',
31-
},
32-
{
33-
name: '--xcconfig [string]',
34-
description: 'Explicitly set xcconfig to use',
35-
},
36-
{
37-
name: '--buildFolder <string>',
38-
description:
39-
'Location for iOS build artifacts. Corresponds to Xcode\'s "-derivedDataPath".',
40-
},
41-
{
42-
name: '--extra-params <string>',
43-
description: 'Custom params that will be passed to xcodebuild command.',
44-
parse: (val: string) => val.split(' '),
45-
},
46-
{
47-
name: '--target <string>',
48-
description: 'Explicitly set Xcode target to use.',
49-
},
50-
{
51-
name: '--interactive',
52-
description:
53-
'Explicitly select which scheme and configuration to use before running a build',
54-
},
55-
{
56-
name: '--force-pods',
57-
description: 'Force CocoaPods installation',
58-
},
59-
];
17+
export const getBuildOptions = ({platformName}: BuilderCommand) => {
18+
const {readableName} = getPlatformInfo(platformName);
19+
return [
20+
{
21+
name: '--mode <string>',
22+
description:
23+
'Explicitly set the scheme configuration to use. This option is case sensitive.',
24+
},
25+
{
26+
name: '--scheme <string>',
27+
description: 'Explicitly set Xcode scheme to use',
28+
},
29+
{
30+
name: '--destination <string>',
31+
description: 'Explicitly extend destination e.g. "arch=x86_64"',
32+
},
33+
{
34+
name: '--verbose',
35+
description: 'Do not use xcbeautify or xcpretty even if installed',
36+
},
37+
{
38+
name: '--xcconfig [string]',
39+
description: 'Explicitly set xcconfig to use',
40+
},
41+
{
42+
name: '--buildFolder <string>',
43+
description: `Location for ${readableName} build artifacts. Corresponds to Xcode's "-derivedDataPath".`,
44+
},
45+
{
46+
name: '--extra-params <string>',
47+
description: 'Custom params that will be passed to xcodebuild command.',
48+
parse: (val: string) => val.split(' '),
49+
},
50+
{
51+
name: '--target <string>',
52+
description: 'Explicitly set Xcode target to use.',
53+
},
54+
{
55+
name: '--interactive',
56+
description:
57+
'Explicitly select which scheme and configuration to use before running a build',
58+
},
59+
{
60+
name: '--force-pods',
61+
description: 'Force CocoaPods installation',
62+
},
63+
];
64+
};

packages/cli-platform-apple/src/commands/logCommand/logOptions.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
export const logOptions = [
1+
import {BuilderCommand} from '../../types';
2+
3+
export const getLogOptions = ({}: BuilderCommand) => [
24
{
35
name: '--interactive',
46
description:
Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,53 @@
11
import {getDefaultUserTerminal} from '@react-native-community/cli-tools';
2-
import {buildOptions} from '../buildCommand/buildOptions';
2+
import {BuilderCommand} from '../../types';
3+
import {getPlatformInfo} from './getPlatformInfo';
4+
import {getBuildOptions} from '../buildCommand/buildOptions';
35

4-
export const runOptions = [
5-
{
6-
name: '--no-packager',
7-
description: 'Do not launch packager while running the app',
8-
},
9-
{
10-
name: '--port <number>',
11-
default: process.env.RCT_METRO_PORT || 8081,
12-
parse: Number,
13-
},
14-
{
15-
name: '--terminal <string>',
16-
description:
17-
'Launches the Metro Bundler in a new window using the specified terminal path.',
18-
default: getDefaultUserTerminal(),
19-
},
20-
{
21-
name: '--binary-path <string>',
22-
description:
23-
'Path relative to project root where pre-built .app binary lives.',
24-
},
25-
{
26-
name: '--list-devices',
27-
description:
28-
'List all available iOS devices and simulators and let you choose one to run the app. ',
29-
},
30-
{
31-
name: '--simulator <string>',
32-
description:
33-
'Explicitly set the simulator to use. Optionally set the iOS version ' +
34-
'between parentheses at the end to match an exact version: ' +
35-
'"iPhone 15 (17.0)"',
36-
},
37-
{
38-
name: '--device <string>',
39-
description:
40-
'Explicitly set the device to use by name. The value is not required ' +
41-
'if you have a single device connected.',
42-
},
43-
{
44-
name: '--udid <string>',
45-
description: 'Explicitly set the device to use by UDID',
46-
},
47-
...buildOptions,
48-
];
6+
export const getRunOptions = ({platformName}: BuilderCommand) => {
7+
const {readableName} = getPlatformInfo(platformName);
8+
const isMac = platformName === 'macos';
9+
return [
10+
{
11+
name: '--no-packager',
12+
description: 'Do not launch packager while running the app',
13+
},
14+
{
15+
name: '--port <number>',
16+
default: process.env.RCT_METRO_PORT || 8081,
17+
parse: Number,
18+
},
19+
{
20+
name: '--terminal <string>',
21+
description:
22+
'Launches the Metro Bundler in a new window using the specified terminal path.',
23+
default: getDefaultUserTerminal(),
24+
},
25+
{
26+
name: '--binary-path <string>',
27+
description:
28+
'Path relative to project root where pre-built .app binary lives.',
29+
},
30+
{
31+
name: '--list-devices',
32+
description: `List all available ${readableName} devices and simulators and let you choose one to run the app. `,
33+
},
34+
{
35+
name: '--udid <string>',
36+
description: 'Explicitly set the device to use by UDID',
37+
},
38+
!isMac && {
39+
name: '--simulator <string>',
40+
description:
41+
`Explicitly set the simulator to use. Optionally set the ${readableName} version ` +
42+
'between parentheses at the end to match an exact version: ' +
43+
'"iPhone 15 (17.0)"',
44+
},
45+
!isMac && {
46+
name: '--device <string>',
47+
description:
48+
'Explicitly set the device to use by name. The value is not required ' +
49+
'if you have a single device connected.',
50+
},
51+
...getBuildOptions({platformName}),
52+
];
53+
};

packages/cli-platform-apple/src/config/index.ts

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -55,42 +55,47 @@ export const getProjectConfig =
5555
};
5656
};
5757

58-
export function dependencyConfig(
59-
folder: string,
60-
userConfig: IOSDependencyParams | null = {},
61-
): IOSDependencyConfig | null {
62-
if (userConfig === null) {
63-
return null;
64-
}
58+
/**
59+
* Make getDependencyConfig follow the same pattern as getProjectConfig
60+
*/
61+
export const getDependencyConfig =
62+
({}: BuilderCommand) =>
63+
(
64+
folder: string,
65+
userConfig: IOSDependencyParams | null = {},
66+
): IOSDependencyConfig | null => {
67+
if (userConfig === null) {
68+
return null;
69+
}
6570

66-
const podspecPath = findPodspec(folder);
71+
const podspecPath = findPodspec(folder);
6772

68-
if (!podspecPath) {
69-
return null;
70-
}
73+
if (!podspecPath) {
74+
return null;
75+
}
7176

72-
let version = 'unresolved';
77+
let version = 'unresolved';
7378

74-
try {
75-
const packageJson = require(path.join(folder, 'package.json'));
79+
try {
80+
const packageJson = require(path.join(folder, 'package.json'));
7681

77-
if (packageJson.version) {
78-
version = packageJson.version;
82+
if (packageJson.version) {
83+
version = packageJson.version;
84+
}
85+
} catch {
86+
throw new CLIError(
87+
`Failed to locate package.json file from ${chalk.underline(
88+
folder,
89+
)}. This is most likely issue with your node_modules folder being corrupted. Please force install dependencies and try again`,
90+
);
7991
}
80-
} catch {
81-
throw new CLIError(
82-
`Failed to locate package.json file from ${chalk.underline(
83-
folder,
84-
)}. This is most likely issue with your node_modules folder being corrupted. Please force install dependencies and try again`,
85-
);
86-
}
8792

88-
return {
89-
podspecPath,
90-
version,
91-
configurations: userConfig.configurations || [],
92-
scriptPhases: userConfig.scriptPhases || [],
93+
return {
94+
podspecPath,
95+
version,
96+
configurations: userConfig.configurations || [],
97+
scriptPhases: userConfig.scriptPhases || [],
98+
};
9399
};
94-
}
95100

96101
export const findPodfilePaths = findAllPodfilePaths;

packages/cli-platform-apple/src/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
export {dependencyConfig, getProjectConfig, findPodfilePaths} from './config';
1+
export {
2+
getDependencyConfig,
3+
getProjectConfig,
4+
findPodfilePaths,
5+
} from './config';
26

3-
export {buildOptions} from './commands/buildCommand/buildOptions';
4-
export {logOptions} from './commands/logCommand/logOptions';
5-
export {runOptions} from './commands/runCommand/runOptions';
7+
export {getBuildOptions} from './commands/buildCommand/buildOptions';
8+
export {getLogOptions} from './commands/logCommand/logOptions';
9+
export {getRunOptions} from './commands/runCommand/runOptions';
610

711
export {default as createBuild} from './commands/buildCommand/createBuild';
812
export {default as createLog} from './commands/logCommand/createLog';

packages/cli-platform-ios/src/commands/buildIOS/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {
10-
buildOptions,
10+
getBuildOptions,
1111
createBuild,
1212
} from '@react-native-community/cli-platform-apple';
1313

@@ -21,5 +21,5 @@ export default {
2121
cmd: 'npx react-native build-ios --mode "Release"',
2222
},
2323
],
24-
options: buildOptions,
24+
options: getBuildOptions({platformName: 'ios'}),
2525
};

packages/cli-platform-ios/src/commands/logIOS/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
import {
1010
createLog,
11-
logOptions,
11+
getLogOptions,
1212
} from '@react-native-community/cli-platform-apple';
1313

1414
export default {
1515
name: 'log-ios',
1616
description: 'starts iOS device syslog tail',
1717
func: createLog({platformName: 'ios'}),
18-
options: logOptions,
18+
options: getLogOptions({platformName: 'ios'}),
1919
};

packages/cli-platform-ios/src/commands/runIOS/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {
1010
createRun,
11-
runOptions,
11+
getRunOptions,
1212
} from '@react-native-community/cli-platform-apple';
1313

1414
export default {
@@ -29,5 +29,5 @@ export default {
2929
cmd: 'npx react-native run-ios --simulator "Apple TV" --scheme "helloworld-tvOS"',
3030
},
3131
],
32-
options: runOptions,
32+
options: getRunOptions({platformName: 'ios'}),
3333
};

0 commit comments

Comments
 (0)