Skip to content

Commit 6eb9595

Browse files
authored
feat: add strict typing for supported platforms (#2218)
1 parent 91c64cc commit 6eb9595

File tree

14 files changed

+64
-27
lines changed

14 files changed

+64
-27
lines changed

packages/cli-platform-apple/src/__tests__/pods.test.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ describe('compareMd5Hashes', () => {
7676

7777
describe('getPlatformDependencies', () => {
7878
it('should return only dependencies with native code', () => {
79-
const result = getPlatformDependencies(dependenciesConfig);
79+
const result = getPlatformDependencies(dependenciesConfig, 'ios');
8080
expect(result).toEqual(['[email protected]', '[email protected]']);
8181
});
8282
});
@@ -85,7 +85,7 @@ describe('resolvePods', () => {
8585
it('should install pods if they are not installed', async () => {
8686
createTempFiles({'ios/Podfile/Manifest.lock': ''});
8787

88-
await resolvePods(DIR, {});
88+
await resolvePods(DIR, {}, 'ios');
8989

9090
expect(installPods).toHaveBeenCalled();
9191
});
@@ -101,7 +101,7 @@ describe('resolvePods', () => {
101101
it('should install pods when there is no cached hash of dependencies', async () => {
102102
createTempFiles();
103103

104-
await resolvePods(DIR, {});
104+
await resolvePods(DIR, {}, 'ios');
105105

106106
expect(mockSet).toHaveBeenCalledWith(
107107
packageJson.name,
@@ -115,7 +115,7 @@ describe('resolvePods', () => {
115115

116116
mockGet.mockImplementation(() => dependencyHash);
117117

118-
await resolvePods(DIR, {});
118+
await resolvePods(DIR, {}, 'ios');
119119

120120
expect(installPods).not.toHaveBeenCalled();
121121
});
@@ -125,12 +125,16 @@ describe('resolvePods', () => {
125125

126126
mockGet.mockImplementation(() => dependencyHash);
127127

128-
await resolvePods(DIR, {
129-
dep1: {
130-
name: 'dep1',
131-
...commonDepConfig,
128+
await resolvePods(
129+
DIR,
130+
{
131+
dep1: {
132+
name: 'dep1',
133+
...commonDepConfig,
134+
},
132135
},
133-
});
136+
'ios',
137+
);
134138

135139
expect(installPods).toHaveBeenCalled();
136140
});

packages/cli-platform-apple/src/commands/buildCommand/buildProject.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ import {
1212
} from '@react-native-community/cli-tools';
1313
import type {BuildFlags} from './buildOptions';
1414
import {simulatorDestinationMap} from './simulatorDestinationMap';
15+
import {supportedPlatforms} from '../../config/supportedPlatforms';
16+
import {ApplePlatform} from '../../types';
1517

1618
export function buildProject(
1719
xcodeProject: IOSProjectInfo,
18-
platform: string,
20+
platform: ApplePlatform,
1921
udid: string | undefined,
2022
mode: string,
2123
scheme: string,
@@ -27,7 +29,9 @@ export function buildProject(
2729
if (!simulatorDest) {
2830
reject(
2931
new CLIError(
30-
`Unknown platform: ${platform}. Please, use one of: ios, macos, visionos, tvos.`,
32+
`Unknown platform: ${platform}. Please, use one of: ${Object.values(
33+
supportedPlatforms,
34+
).join(', ')}.`,
3135
),
3236
);
3337
return;

packages/cli-platform-apple/src/commands/buildCommand/createBuild.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ import {buildProject} from './buildProject';
77
import {getConfiguration} from './getConfiguration';
88
import {getXcodeProjectAndDir} from './getXcodeProjectAndDir';
99
import {BuilderCommand} from '../../types';
10+
import {supportedPlatforms} from '../../config/supportedPlatforms';
1011

1112
const createBuild =
1213
({platformName}: BuilderCommand) =>
1314
async (_: Array<string>, ctx: Config, args: BuildFlags) => {
1415
const platform = ctx.project[platformName] as IOSProjectConfig;
15-
if (platform === undefined) {
16+
if (
17+
platform === undefined ||
18+
supportedPlatforms[platformName] === undefined
19+
) {
1620
throw new CLIError(`Unable to find ${platform} platform config`);
1721
}
1822

packages/cli-platform-apple/src/commands/buildCommand/simulatorDestinationMap.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
export const simulatorDestinationMap: Record<string, string> = {
1+
import {ApplePlatform} from '../../types';
2+
3+
export const simulatorDestinationMap: Record<ApplePlatform, string> = {
24
ios: 'iOS Simulator',
35
macos: 'macOS',
46
visionos: 'visionOS Simulator',

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import getSimulators from '../../tools/getSimulators';
77
import listDevices from '../../tools/listDevices';
88
import {getPlatformInfo} from '../runCommand/getPlatformInfo';
99
import {BuilderCommand} from '../../types';
10+
import {supportedPlatforms} from '../../config/supportedPlatforms';
1011

1112
/**
1213
* Starts Apple device syslog tail
@@ -22,7 +23,10 @@ const createLog =
2223
const platform = ctx.project[platformName] as IOSProjectConfig;
2324
const {readableName: platformReadableName} = getPlatformInfo(platformName);
2425

25-
if (platform === undefined) {
26+
if (
27+
platform === undefined ||
28+
supportedPlatforms[platformName] === undefined
29+
) {
2630
throw new CLIError(`Unable to find ${platform} platform config`);
2731
}
2832

packages/cli-platform-apple/src/commands/runCommand/createRun.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {printFoundDevices, matchingDevice} from './matchingDevice';
3333
import {runOnDevice} from './runOnDevice';
3434
import {runOnSimulator} from './runOnSimulator';
3535
import {BuilderCommand} from '../../types';
36+
import {supportedPlatforms} from '../../config/supportedPlatforms';
3637

3738
export interface FlagsT extends BuildFlags {
3839
simulator?: string;
@@ -54,7 +55,10 @@ const createRun =
5455
const {sdkNames, readableName: platformReadableName} =
5556
getPlatformInfo(platformName);
5657

57-
if (platform === undefined) {
58+
if (
59+
platform === undefined ||
60+
supportedPlatforms[platformName] === undefined
61+
) {
5862
throw new CLIError(
5963
`Unable to find ${platformReadableName} platform config`,
6064
);

packages/cli-platform-apple/src/commands/runCommand/getPlatformInfo.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {ApplePlatform} from '../../types';
2+
13
interface PlatformInfo {
24
readableName: string;
35
sdkNames: string[];
@@ -9,7 +11,7 @@ interface PlatformInfo {
911
*
1012
* Falls back to iOS if platform is not supported.
1113
*/
12-
export function getPlatformInfo(platform: string): PlatformInfo {
14+
export function getPlatformInfo(platform: ApplePlatform): PlatformInfo {
1315
const iosPlatformInfo: PlatformInfo = {
1416
readableName: 'iOS',
1517
sdkNames: ['iphonesimulator', 'iphoneos'],

packages/cli-platform-apple/src/commands/runCommand/runOnDevice.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import child_process from 'child_process';
2-
import {Device} from '../../types';
2+
import {ApplePlatform, Device} from '../../types';
33
import {IOSProjectInfo} from '@react-native-community/cli-types';
44
import {CLIError, logger} from '@react-native-community/cli-tools';
55
import chalk from 'chalk';
@@ -9,7 +9,7 @@ import {FlagsT} from './createRun';
99

1010
export async function runOnDevice(
1111
selectedDevice: Device,
12-
platform: string,
12+
platform: ApplePlatform,
1313
mode: string,
1414
scheme: string,
1515
xcodeProject: IOSProjectInfo,

packages/cli-platform-apple/src/commands/runCommand/runOnSimulator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import {IOSProjectInfo} from '@react-native-community/cli-types';
33
import path from 'path';
44
import {logger} from '@react-native-community/cli-tools';
55
import chalk from 'chalk';
6-
import {Device} from '../../types';
6+
import {ApplePlatform, Device} from '../../types';
77
import {buildProject} from '../buildCommand/buildProject';
88
import {formattedDeviceName} from './matchingDevice';
99
import {getBuildPath} from './getBuildPath';
1010
import {FlagsT} from './createRun';
1111

1212
export async function runOnSimulator(
1313
xcodeProject: IOSProjectInfo,
14-
platform: string,
14+
platform: ApplePlatform,
1515
mode: string,
1616
scheme: string,
1717
args: FlagsT,

packages/cli-platform-apple/src/config/__tests__/findPodfilePath.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ afterEach(() => {
1414
describe('ios::findPodfilePath', () => {
1515
it('returns null if there is no Podfile', () => {
1616
fs.__setMockFilesystem({});
17-
expect(findPodfilePath('/')).toBeNull();
17+
expect(findPodfilePath('/', 'ios')).toBeNull();
1818
});
1919

2020
it('returns Podfile path if it exists', () => {
2121
fs.__setMockFilesystem(projects.project);
22-
expect(findPodfilePath('/')).toContain('ios/Podfile');
22+
expect(findPodfilePath('/', 'ios')).toContain('ios/Podfile');
2323
});
2424

2525
it('prints a warning when multile Podfiles are found', () => {
@@ -28,7 +28,7 @@ describe('ios::findPodfilePath', () => {
2828
foo: projects.project,
2929
bar: projects.project,
3030
});
31-
expect(findPodfilePath('/')).toContain('bar/ios/Podfile');
31+
expect(findPodfilePath('/', 'ios')).toContain('bar/ios/Podfile');
3232
expect(warn.mock.calls).toMatchSnapshot();
3333
});
3434

0 commit comments

Comments
 (0)