Skip to content

Commit

Permalink
feat: add strict typing for supported platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
okwasniewski committed Dec 20, 2023
1 parent a4417a1 commit 7ab0750
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import {
} from '@react-native-community/cli-tools';
import type {BuildFlags} from './buildOptions';
import {simulatorDestinationMap} from './simulatorDestinationMap';
import {supportedPlatforms} from '../../config/supportedPlatforms';
import {ApplePlatform} from '../../types';

export function buildProject(
xcodeProject: IOSProjectInfo,
platform: string,
platform: ApplePlatform,
udid: string | undefined,
mode: string,
scheme: string,
Expand All @@ -27,7 +29,9 @@ export function buildProject(
if (!simulatorDest) {
reject(
new CLIError(
`Unknown platform: ${platform}. Please, use one of: ios, macos, visionos, tvos.`,
`Unknown platform: ${platform}. Please, use one of: ${Object.values(
supportedPlatforms,
).join(', ')}.`,
),
);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ import {getConfiguration} from './getConfiguration';
import {getXcodeProjectAndDir} from './getXcodeProjectAndDir';
import {BuilderCommand} from '../../types';
import findXcodeProject from '../../config/findXcodeProject';
import {supportedPlatforms} from '../../config/supportedPlatforms';

const createBuild =
({platformName}: BuilderCommand) =>
async (_: Array<string>, ctx: Config, args: BuildFlags) => {
const platform = ctx.project[platformName] as IOSProjectConfig;
if (platform === undefined) {
if (
platform === undefined ||
supportedPlatforms[platformName] === undefined
) {
throw new CLIError(`Unable to find ${platform} platform config`);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const simulatorDestinationMap: Record<string, string> = {
import {ApplePlatform} from '../../types';

export const simulatorDestinationMap: Record<ApplePlatform, string> = {
ios: 'iOS Simulator',
macos: 'macOS',
visionos: 'visionOS Simulator',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import getSimulators from '../../tools/getSimulators';
import listDevices from '../../tools/listDevices';
import {getPlatformInfo} from '../runCommand/getPlatformInfo';
import {BuilderCommand} from '../../types';
import {supportedPlatforms} from '../../config/supportedPlatforms';

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

if (platform === undefined) {
if (
platform === undefined ||
supportedPlatforms[platformName] === undefined
) {
throw new CLIError(`Unable to find ${platform} platform config`);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {printFoundDevices, matchingDevice} from './matchingDevice';
import {runOnDevice} from './runOnDevice';
import {runOnSimulator} from './runOnSimulator';
import {BuilderCommand} from '../../types';
import {supportedPlatforms} from '../../config/supportedPlatforms';

export interface FlagsT extends BuildFlags {
simulator?: string;
Expand All @@ -55,7 +56,10 @@ const createRun =
const {sdkNames, readableName: platformReadableName} =
getPlatformInfo(platformName);

if (platform === undefined) {
if (
platform === undefined ||
supportedPlatforms[platformName] === undefined
) {
throw new CLIError(
`Unable to find ${platformReadableName} platform config`,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {ApplePlatform} from '../../types';

interface PlatformInfo {
readableName: string;
sdkNames: string[];
Expand All @@ -9,7 +11,7 @@ interface PlatformInfo {
*
* Falls back to iOS if platform is not supported.
*/
export function getPlatformInfo(platform: string): PlatformInfo {
export function getPlatformInfo(platform: ApplePlatform): PlatformInfo {
const iosPlatformInfo: PlatformInfo = {
readableName: 'iOS',
sdkNames: ['iphonesimulator', 'iphoneos'],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import child_process from 'child_process';
import {Device} from '../../types';
import {ApplePlatform, Device} from '../../types';
import {IOSProjectInfo} from '@react-native-community/cli-types';
import {CLIError, logger} from '@react-native-community/cli-tools';
import chalk from 'chalk';
Expand All @@ -9,7 +9,7 @@ import {FlagsT} from './createRun';

export async function runOnDevice(
selectedDevice: Device,
platform: string,
platform: ApplePlatform,
mode: string,
scheme: string,
xcodeProject: IOSProjectInfo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import {IOSProjectInfo} from '@react-native-community/cli-types';
import path from 'path';
import {logger} from '@react-native-community/cli-tools';
import chalk from 'chalk';
import {Device} from '../../types';
import {ApplePlatform, Device} from '../../types';
import {buildProject} from '../buildCommand/buildProject';
import {formattedDeviceName} from './matchingDevice';
import {getBuildPath} from './getBuildPath';
import {FlagsT} from './createRun';

export async function runOnSimulator(
xcodeProject: IOSProjectInfo,
platform: string,
platform: ApplePlatform,
mode: string,
scheme: string,
args: FlagsT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ afterEach(() => {
describe('ios::findPodfilePath', () => {
it('returns null if there is no Podfile', () => {
fs.__setMockFilesystem({});
expect(findPodfilePath('/')).toBeNull();
expect(findPodfilePath('/', 'ios')).toBeNull();
});

it('returns Podfile path if it exists', () => {
fs.__setMockFilesystem(projects.project);
expect(findPodfilePath('/')).toContain('ios/Podfile');
expect(findPodfilePath('/', 'ios')).toContain('ios/Podfile');
});

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

Expand Down
3 changes: 2 additions & 1 deletion packages/cli-platform-apple/src/config/findPodfilePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import {inlineString, logger} from '@react-native-community/cli-tools';
import path from 'path';
import findAllPodfilePaths from './findAllPodfilePaths';
import {ApplePlatform} from '../types';

// Regexp matching all test projects
const TEST_PROJECTS = /test|example|sample/i;
Expand All @@ -18,7 +19,7 @@ const BUNDLE_VENDORED_PODFILE = 'vendor/bundle/ruby';

export default function findPodfilePath(
cwd: string,
platformName: string = 'ios',
platformName: ApplePlatform,
) {
const podfiles = findAllPodfilePaths(cwd)
/**
Expand Down
6 changes: 6 additions & 0 deletions packages/cli-platform-apple/src/config/supportedPlatforms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const supportedPlatforms = {
ios: 'ios',
macos: 'macos',
visionos: 'visionos',
tvos: 'tvos',
} as const;
5 changes: 3 additions & 2 deletions packages/cli-platform-apple/src/tools/pods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
DependencyConfig,
IOSDependencyConfig,
} from '@react-native-community/cli-types';
import {ApplePlatform} from '../types';

interface ResolvePodsOptions {
forceInstall?: boolean;
Expand All @@ -35,7 +36,7 @@ export function getPackageJson(root: string) {

export function getPlatformDependencies(
dependencies: NativeDependencies,
platformName: string = 'ios',
platformName: ApplePlatform,
) {
return Object.keys(dependencies)
.filter((dependency) => dependencies[dependency].platforms?.[platformName])
Expand Down Expand Up @@ -91,7 +92,7 @@ async function install(
export default async function resolvePods(
root: string,
nativeDependencies: NativeDependencies,
platformName: string = 'ios',
platformName: ApplePlatform,
options?: ResolvePodsOptions,
) {
const packageJson = getPackageJson(root);
Expand Down
7 changes: 6 additions & 1 deletion packages/cli-platform-apple/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import {supportedPlatforms} from './config/supportedPlatforms';

type ObjectValues<T> = T[keyof T];

export type ApplePlatform = ObjectValues<typeof supportedPlatforms>;
export interface Device {
name: string;
udid: string;
Expand All @@ -23,5 +28,5 @@ export interface BuilderCommand {
* Lowercase name of the platform.
* Example: 'ios', 'visionos'
*/
platformName: string;
platformName: ApplePlatform;
}

0 comments on commit 7ab0750

Please sign in to comment.