Skip to content

Commit

Permalink
refactor: extract duplicated code to getXcodeProjectAndDir
Browse files Browse the repository at this point in the history
  • Loading branch information
okwasniewski committed Dec 20, 2023
1 parent 49426fd commit b4126f0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fs from 'fs';
import {CLIError} from '@react-native-community/cli-tools';
import {Config, IOSProjectConfig} from '@react-native-community/cli-types';
import getArchitecture from '../../tools/getArchitecture';
Expand All @@ -8,7 +7,6 @@ import {buildProject} from './buildProject';
import {getConfiguration} from './getConfiguration';
import {getXcodeProjectAndDir} from './getXcodeProjectAndDir';
import {BuilderCommand} from '../../types';
import findXcodeProject from '../../config/findXcodeProject';

const createBuild =
({platformName}: BuilderCommand) =>
Expand All @@ -18,8 +16,6 @@ const createBuild =
throw new CLIError(`Unable to find ${platform} platform config`);
}

let {xcodeProject, sourceDir} = getXcodeProjectAndDir(platform);

let installedPods = false;
if (platform?.automaticPodsInstallation || args.forcePods) {
const isAppRunningNewArchitecture = platform?.sourceDir
Expand All @@ -34,14 +30,10 @@ const createBuild =
installedPods = true;
}

// if project is freshly created, revisit Xcode project to verify Pods are installed correctly.
// This is needed because ctx project is created before Pods are installed, so it might have outdated information.
if (installedPods) {
const recheckXcodeProject = findXcodeProject(fs.readdirSync(sourceDir));
if (recheckXcodeProject) {
xcodeProject = recheckXcodeProject;
}
}
let {xcodeProject, sourceDir} = getXcodeProjectAndDir(
platform,
installedPods,
);

process.chdir(sourceDir);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
import fs from 'fs';
import {IOSProjectConfig} from '@react-native-community/cli-types';
import {CLIError} from '@react-native-community/cli-tools';
import findXcodeProject from '../../config/findXcodeProject';

export function getXcodeProjectAndDir(
iosProjectConfig: IOSProjectConfig | undefined,
installedPods?: boolean,
) {
if (!iosProjectConfig) {
throw new CLIError(
'iOS project folder not found. Are you sure this is a React Native project?',
);
}

const {xcodeProject, sourceDir} = iosProjectConfig;
let {xcodeProject, sourceDir} = iosProjectConfig;

if (!xcodeProject) {
throw new CLIError(
`Could not find Xcode project files in "${sourceDir}" folder`,
);
}

// if project is freshly created, revisit Xcode project to verify Pods are installed correctly.
// This is needed because ctx project is created before Pods are installed, so it might have outdated information.
if (installedPods) {
const recheckXcodeProject = findXcodeProject(fs.readdirSync(sourceDir));
if (recheckXcodeProject) {
xcodeProject = recheckXcodeProject;
}
}

return {xcodeProject, sourceDir};
}
15 changes: 4 additions & 11 deletions packages/cli-platform-apple/src/commands/runCommand/createRun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
findDevServerPort,
cacheManager,
} from '@react-native-community/cli-tools';
import findXcodeProject from '../../config/findXcodeProject';
import getArchitecture from '../../tools/getArchitecture';
import getSimulators from '../../tools/getSimulators';
import listDevices from '../../tools/listDevices';
Expand Down Expand Up @@ -97,16 +96,10 @@ const createRun =
link.setVersion(ctx.reactNativeVersion);
}

let {xcodeProject, sourceDir} = getXcodeProjectAndDir(platform);

// if project is freshly created, revisit Xcode project to verify Pods are installed correctly.
// This is needed because ctx project is created before Pods are installed, so it might have outdated information.
if (installedPods) {
const recheckXcodeProject = findXcodeProject(fs.readdirSync(sourceDir));
if (recheckXcodeProject) {
xcodeProject = recheckXcodeProject;
}
}
let {xcodeProject, sourceDir} = getXcodeProjectAndDir(
platform,
installedPods,
);

process.chdir(sourceDir);

Expand Down

0 comments on commit b4126f0

Please sign in to comment.