Skip to content

Commit

Permalink
feat: add manual Cocoapods installation info (#2204)
Browse files Browse the repository at this point in the history
* feat: add manual Cocoapods installation info

* feat: apply review comments

* fix: move logger.info out of nested if

* fix: hide banner in CI

* fix: check for Install cocoapods only on darwin

* test: check if cocoapods instruction is shown in another test

* Update packages/cli/src/commands/init/printRunInstructions.ts

Co-authored-by: Riccardo Cipolleschi <[email protected]>

---------

Co-authored-by: Riccardo Cipolleschi <[email protected]>
  • Loading branch information
okwasniewski and cipolleschi authored Dec 13, 2023
1 parent 6b8607c commit 1bf0d85
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 9 deletions.
4 changes: 4 additions & 0 deletions __e2e__/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ test('init skips installation of dependencies with --skip-install', () => {

expect(stdout).toContain('Run instructions');

if (process.platform === 'darwin') {
expect(stdout).toContain('Install Cocoapods');
}

// make sure we don't leave garbage
expect(fs.readdirSync(DIR)).toContain('custom');

Expand Down
50 changes: 43 additions & 7 deletions packages/cli/src/commands/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ interface TemplateOptions {
version?: string;
}

interface TemplateReturnType {
didInstallPods?: boolean;
}

function doesDirectoryExist(dir: string) {
return fs.existsSync(dir);
}
Expand Down Expand Up @@ -112,10 +116,13 @@ async function createFromTemplate({
skipInstall,
packageName,
installCocoaPods,
}: TemplateOptions) {
}: TemplateOptions): Promise<TemplateReturnType> {
logger.debug('Initializing new project');
logger.log(banner);

// Only print out the banner if we're not in a CI
if (!process.env.CI) {
logger.log(banner);
}
let didInstallPods = String(installCocoaPods) === 'true';
let packageManager = pm;

if (pm) {
Expand Down Expand Up @@ -196,6 +203,7 @@ async function createFromTemplate({
const installPodsValue = String(installCocoaPods);

if (installPodsValue === 'true') {
didInstallPods = true;
await installPods(loader);
loader.succeed();
setEmptyHashForCachedDependencies(projectName);
Expand All @@ -207,6 +215,7 @@ async function createFromTemplate({
'Only needed if you run your project in Xcode directly',
)}`,
});
didInstallPods = installCocoapods;

if (installCocoapods) {
await installPods(loader);
Expand All @@ -216,14 +225,34 @@ async function createFromTemplate({
}
}
} else {
didInstallPods = false;
loader.succeed('Dependencies installation skipped');
}
} catch (e) {
if (e instanceof Error) {
logger.error(
'Installing pods failed. This doesn\'t affect project initialization and you can safely proceed. \nHowever, you will need to install pods manually when running iOS, follow additional steps in "Run instructions for iOS" section.\n',
);
}
loader.fail();
throw e;
didInstallPods = false;
} finally {
fs.removeSync(templateSourceDir);
}

if (process.platform === 'darwin') {
logger.log('\n');
logger.info(
`💡 To enable automatic CocoaPods installation when building for iOS you can create react-native.config.js with automaticPodsInstallation field. \n${chalk.reset.dim(
`For more details, see ${chalk.underline(
'https://github.com/react-native-community/cli/blob/main/docs/projects.md#projectiosautomaticpodsinstallation',
)}`,
)}
`,
);
}

return {didInstallPods};
}

async function installDependencies({
Expand Down Expand Up @@ -282,7 +311,7 @@ async function createProject(
directory: string,
version: string,
options: Options,
) {
): Promise<TemplateReturnType> {
const templateUri = createTemplateUri(options, version);

return createFromTemplate({
Expand Down Expand Up @@ -348,13 +377,20 @@ export default (async function initialize(
return;
}

await createProject(projectName, directoryName, version, options);
const {didInstallPods} = await createProject(
projectName,
directoryName,
version,
options,
);

const projectFolder = path.join(root, directoryName);

if (!options.skipGitInit) {
await createGitRepository(projectFolder);
}

printRunInstructions(projectFolder, projectName);
printRunInstructions(projectFolder, projectName, {
showPodsInstructions: !didInstallPods,
});
});
25 changes: 23 additions & 2 deletions packages/cli/src/commands/init/printRunInstructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@ import process from 'process';
import chalk from 'chalk';
import {logger} from '@react-native-community/cli-tools';

function printRunInstructions(projectDir: string, projectName: string) {
interface Options {
showPodsInstructions?: boolean;
}

function printRunInstructions(
projectDir: string,
projectName: string,
options: Options = {
showPodsInstructions: false,
},
) {
let iosInstructions = '';
let desktopInstructions = '';

Expand All @@ -34,7 +44,18 @@ function printRunInstructions(projectDir: string, projectName: string) {

iosInstructions = `
${chalk.cyan(`Run instructions for ${chalk.bold('iOS')}`)}:
• cd "${projectDir}" && npx react-native run-ios
• cd "${projectDir}${options.showPodsInstructions ? '/ios' : ''}"
${
options.showPodsInstructions
? `
• Install Cocoapods
• bundle install # you need to run this only once in your project.
• bundle exec pod install
• cd ..
`
: ''
}
• npx react-native run-ios
${chalk.dim('- or -')}
• Open ${relativeXcodeProjectPath} in Xcode or run "xed -b ios"
• Hit the Run button
Expand Down

0 comments on commit 1bf0d85

Please sign in to comment.