Skip to content

Commit 1bf0d85

Browse files
feat: add manual Cocoapods installation info (#2204)
* 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]>
1 parent 6b8607c commit 1bf0d85

File tree

3 files changed

+70
-9
lines changed

3 files changed

+70
-9
lines changed

__e2e__/init.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ test('init skips installation of dependencies with --skip-install', () => {
119119

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

122+
if (process.platform === 'darwin') {
123+
expect(stdout).toContain('Install Cocoapods');
124+
}
125+
122126
// make sure we don't leave garbage
123127
expect(fs.readdirSync(DIR)).toContain('custom');
124128

packages/cli/src/commands/init/init.ts

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ interface TemplateOptions {
6060
version?: string;
6161
}
6262

63+
interface TemplateReturnType {
64+
didInstallPods?: boolean;
65+
}
66+
6367
function doesDirectoryExist(dir: string) {
6468
return fs.existsSync(dir);
6569
}
@@ -112,10 +116,13 @@ async function createFromTemplate({
112116
skipInstall,
113117
packageName,
114118
installCocoaPods,
115-
}: TemplateOptions) {
119+
}: TemplateOptions): Promise<TemplateReturnType> {
116120
logger.debug('Initializing new project');
117-
logger.log(banner);
118-
121+
// Only print out the banner if we're not in a CI
122+
if (!process.env.CI) {
123+
logger.log(banner);
124+
}
125+
let didInstallPods = String(installCocoaPods) === 'true';
119126
let packageManager = pm;
120127

121128
if (pm) {
@@ -196,6 +203,7 @@ async function createFromTemplate({
196203
const installPodsValue = String(installCocoaPods);
197204

198205
if (installPodsValue === 'true') {
206+
didInstallPods = true;
199207
await installPods(loader);
200208
loader.succeed();
201209
setEmptyHashForCachedDependencies(projectName);
@@ -207,6 +215,7 @@ async function createFromTemplate({
207215
'Only needed if you run your project in Xcode directly',
208216
)}`,
209217
});
218+
didInstallPods = installCocoapods;
210219

211220
if (installCocoapods) {
212221
await installPods(loader);
@@ -216,14 +225,34 @@ async function createFromTemplate({
216225
}
217226
}
218227
} else {
228+
didInstallPods = false;
219229
loader.succeed('Dependencies installation skipped');
220230
}
221231
} catch (e) {
232+
if (e instanceof Error) {
233+
logger.error(
234+
'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',
235+
);
236+
}
222237
loader.fail();
223-
throw e;
238+
didInstallPods = false;
224239
} finally {
225240
fs.removeSync(templateSourceDir);
226241
}
242+
243+
if (process.platform === 'darwin') {
244+
logger.log('\n');
245+
logger.info(
246+
`💡 To enable automatic CocoaPods installation when building for iOS you can create react-native.config.js with automaticPodsInstallation field. \n${chalk.reset.dim(
247+
`For more details, see ${chalk.underline(
248+
'https://github.com/react-native-community/cli/blob/main/docs/projects.md#projectiosautomaticpodsinstallation',
249+
)}`,
250+
)}
251+
`,
252+
);
253+
}
254+
255+
return {didInstallPods};
227256
}
228257

229258
async function installDependencies({
@@ -282,7 +311,7 @@ async function createProject(
282311
directory: string,
283312
version: string,
284313
options: Options,
285-
) {
314+
): Promise<TemplateReturnType> {
286315
const templateUri = createTemplateUri(options, version);
287316

288317
return createFromTemplate({
@@ -348,13 +377,20 @@ export default (async function initialize(
348377
return;
349378
}
350379

351-
await createProject(projectName, directoryName, version, options);
380+
const {didInstallPods} = await createProject(
381+
projectName,
382+
directoryName,
383+
version,
384+
options,
385+
);
352386

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

355389
if (!options.skipGitInit) {
356390
await createGitRepository(projectFolder);
357391
}
358392

359-
printRunInstructions(projectFolder, projectName);
393+
printRunInstructions(projectFolder, projectName, {
394+
showPodsInstructions: !didInstallPods,
395+
});
360396
});

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,17 @@ import process from 'process';
1313
import chalk from 'chalk';
1414
import {logger} from '@react-native-community/cli-tools';
1515

16-
function printRunInstructions(projectDir: string, projectName: string) {
16+
interface Options {
17+
showPodsInstructions?: boolean;
18+
}
19+
20+
function printRunInstructions(
21+
projectDir: string,
22+
projectName: string,
23+
options: Options = {
24+
showPodsInstructions: false,
25+
},
26+
) {
1727
let iosInstructions = '';
1828
let desktopInstructions = '';
1929

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

3545
iosInstructions = `
3646
${chalk.cyan(`Run instructions for ${chalk.bold('iOS')}`)}:
37-
• cd "${projectDir}" && npx react-native run-ios
47+
• cd "${projectDir}${options.showPodsInstructions ? '/ios' : ''}"
48+
${
49+
options.showPodsInstructions
50+
? `
51+
• Install Cocoapods
52+
• bundle install # you need to run this only once in your project.
53+
• bundle exec pod install
54+
• cd ..
55+
`
56+
: ''
57+
}
58+
• npx react-native run-ios
3859
${chalk.dim('- or -')}
3960
• Open ${relativeXcodeProjectPath} in Xcode or run "xed -b ios"
4061
• Hit the Run button

0 commit comments

Comments
 (0)