Skip to content

Commit

Permalink
fix: remove installing cocoapods with brew (#1851)
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonrybczak authored Mar 10, 2023
1 parent 6494cc5 commit 71be657
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 107 deletions.
63 changes: 20 additions & 43 deletions packages/cli-doctor/src/tools/healthchecks/cocoaPods.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import execa from 'execa';
import {doesSoftwareNeedToBeFixed} from '../checkInstallation';
import {promptCocoaPodsInstallationQuestion, runSudo} from '../installPods';
import {removeMessage, logError} from './common';
import {brewInstall} from '../brewInstall';
import {runSudo} from '../installPods';
import {logError} from './common';
import {HealthCheckInterface} from '../../types';
import versionRanges from '../versionRanges';

Expand All @@ -22,56 +21,34 @@ export default {
runAutomaticFix: async ({loader}) => {
loader.stop();

const {
installMethod,
promptQuestion,
} = await promptCocoaPodsInstallationQuestion();

// Capitalise `Homebrew` when printing on the screen
const installMethodCapitalized =
installMethod === 'homebrew'
? installMethod.substr(0, 1).toUpperCase() + installMethod.substr(1)
: installMethod;
const installMethodCapitalized = 'Gem';
const loaderInstallationMessage = `${label} (installing with ${installMethodCapitalized})`;
const loaderSucceedMessage = `${label} (installed with ${installMethodCapitalized})`;

// Remove the prompt after the question of how to install CocoaPods is answered
removeMessage(promptQuestion);
loader.start(loaderInstallationMessage);

if (installMethod === 'gem') {
loader.start(loaderInstallationMessage);
const options = ['install', 'cocoapods', '--no-document'];

const options = ['install', 'cocoapods', '--no-document'];
try {
// First attempt to install `cocoapods`
await execa('gem', options);

return loader.succeed(loaderSucceedMessage);
} catch (_error) {
// If that doesn't work then try with sudo
try {
// First attempt to install `cocoapods`
await execa('gem', options);
await runSudo(`gem ${options.join(' ')}`);

return loader.succeed(loaderSucceedMessage);
} catch (_error) {
// If that doesn't work then try with sudo
try {
await runSudo(`gem ${options.join(' ')}`);

return loader.succeed(loaderSucceedMessage);
} catch (error) {
logError({
healthcheck: label,
loader,
error: error as any,
command: 'sudo gem install cocoapods',
});
}
} catch (error) {
logError({
healthcheck: label,
loader,
error: error as any,
command: 'sudo gem install cocoapods',
});
}
}

if (installMethod === 'homebrew') {
return await brewInstall({
pkg: 'cocoapods',
label: loaderInstallationMessage,
loader,
onSuccess: () => loader.succeed(loaderSucceedMessage),
});
}
return;
},
} as HealthCheckInterface;
77 changes: 13 additions & 64 deletions packages/cli-doctor/src/tools/installPods.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import fs from 'fs';
import execa from 'execa';
import chalk from 'chalk';
import prompts from 'prompts';
import {logger, NoopLoader} from '@react-native-community/cli-tools';
import sudo from 'sudo-prompt';
import runBundleInstall from './runBundleInstall';
import {brewInstall} from './brewInstall';
import {Loader} from '../types';

type PromptCocoaPodsInstallation = {
installMethod: 'gem' | 'homebrew';
promptQuestion: string;
};

async function runPodInstall(
loader: Loader,
directory: string,
Expand Down Expand Up @@ -84,38 +77,6 @@ function runSudo(command: string): Promise<void> {
});
}

async function promptCocoaPodsInstallationQuestion(): Promise<
PromptCocoaPodsInstallation
> {
const promptQuestion = `CocoaPods ${chalk.dim.underline(
'(https://cocoapods.org/)',
)} ${chalk.reset.bold(
'is not installed. CocoaPods is necessary for the iOS project to run correctly. Do you want to install it?',
)}`;
const installWithGem = 'Yes, with gem (may require sudo)';
const installWithHomebrew = 'Yes, with Homebrew';

const {installMethod} = await prompts([
{
type: 'select',
name: 'installMethod',
message: promptQuestion,
choices: [
{title: installWithGem, value: 'gem'},
{title: installWithHomebrew, value: 'homebrew'},
],
},
]);

return {
installMethod,
// This is used for removing the message in `doctor` after it's answered
promptQuestion: `? ${promptQuestion} ${
installMethod === 'gem' ? installWithGem : installWithHomebrew
}`,
};
}

async function installCocoaPodsWithGem() {
const options = ['install', 'cocoapods', '--no-document'];

Expand All @@ -131,33 +92,21 @@ async function installCocoaPodsWithGem() {
async function installCocoaPods(loader: Loader) {
loader.stop();

const {installMethod} = await promptCocoaPodsInstallationQuestion();

if (installMethod === 'gem') {
loader.start('Installing CocoaPods');

try {
await installCocoaPodsWithGem();
loader.start('Installing CocoaPods');

return loader.succeed();
} catch (error) {
loader.fail();
logger.error((error as any).stderr);
try {
await installCocoaPodsWithGem();

throw new Error(
`An error occured while trying to install CocoaPods, which is required by this template.\nPlease try again manually: sudo gem install cocoapods.\nCocoaPods documentation: ${chalk.dim.underline(
'https://cocoapods.org/',
)}`,
);
}
}
return loader.succeed();
} catch (error) {
loader.fail();
logger.error((error as any).stderr);

if (installMethod === 'homebrew') {
return await brewInstall({
pkg: 'cocoapods',
label: 'Installing CocoaPods',
loader,
});
throw new Error(
`An error occured while trying to install CocoaPods, which is required by this template.\nPlease try again manually: sudo gem install cocoapods.\nCocoaPods documentation: ${chalk.dim.underline(
'https://cocoapods.org/',
)}`,
);
}
}

Expand Down Expand Up @@ -202,6 +151,6 @@ async function installPods({
}
}

export {promptCocoaPodsInstallationQuestion, runSudo, installCocoaPods};
export {runSudo, installCocoaPods};

export default installPods;

0 comments on commit 71be657

Please sign in to comment.