Skip to content

Commit

Permalink
feat: automatically run pod install when running init command (re…
Browse files Browse the repository at this point in the history
…act-native-community#373)

* Specify `.xcworkspace` on run instructions instead of `.xcodeproj`

* Add `command-exists` package

* Remove unneeded space after log message

* Run `pod install` and check for cocoa pods when running `init` command

* Put pods installation directly on `init` file and verify if `Podfile` exists before running

* Revert change to space

* Remove unneeded `console.log`

* `cd` back to the directory after pod installation

* Throw with `CLIError` instead of `Error`

* Remove unneeded `try…catch`

* Fix wrong uppercased word

* Try to install `cocoapods` without sudo before trying with it

* Run pods installation only on macOS

* Check which extension for the project to print out on the run instructions for iOS

* Use `fs` instead of `fs-extra`

* Move `installPods` to separate file

* Add missing `logger` import

* Fix warnings on duplicated variable declaration

* fixups
  • Loading branch information
Lucas Bento authored and dratwas committed Jul 12, 2019
1 parent 236a1aa commit f687ff4
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@react-native-community/cli-platform-ios": "^2.0.0-alpha.18",
"@react-native-community/cli-tools": "^2.0.0-alpha.18",
"chalk": "^1.1.1",
"command-exists": "^1.2.8",
"commander": "^2.19.0",
"compression": "^1.7.1",
"connect": "^3.6.5",
Expand Down
29 changes: 26 additions & 3 deletions packages/cli/src/commands/init/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os from 'os';
import path from 'path';
import fs from 'fs-extra';
import Ora from 'ora';
import minimist from 'minimist';
import semver from 'semver';
import type {ConfigT} from 'types';
Expand All @@ -17,6 +18,7 @@ import {
} from './template';
import {changePlaceholderInTemplate} from './editTemplate';
import * as PackageManager from '../../tools/packageManager';
import installPods from '../../tools/installPods';
import {processTemplateName} from './templateName';
import banner from './banner';
import {getLoader} from '../../tools/loader';
Expand Down Expand Up @@ -93,9 +95,7 @@ async function createFromTemplate({
loader.succeed();
}

loader.start('Installing all required dependencies');
await PackageManager.installAll({preferYarn: !npm, silent: true});
loader.succeed();
await installDependencies({projectName, npm, loader});
} catch (e) {
loader.fail();
throw new Error(e);
Expand All @@ -104,6 +104,29 @@ async function createFromTemplate({
}
}

async function installDependencies({
projectName,
npm,
loader,
}: {
projectName: string,
npm?: boolean,
loader: typeof Ora,
}) {
loader.start('Installing all required dependencies');

await PackageManager.installAll({
preferYarn: !npm,
silent: true,
});

if (process.platform === 'darwin') {
await installPods({projectName, loader});
}

loader.succeed();
}

function createProject(projectName: string, options: Options, version: string) {
fs.mkdirSync(projectName);
process.chdir(projectName);
Expand Down
15 changes: 9 additions & 6 deletions packages/cli/src/commands/init/printRunInstructions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@
*/

import path from 'path';
import fs from 'fs';
import chalk from 'chalk';
import {logger} from '@react-native-community/cli-tools';

function printRunInstructions(projectDir: string, projectName: string) {
const absoluteProjectDir = path.resolve(projectDir);
const xcodeProjectPath = `${path.resolve(
projectDir,
'ios',
projectName,
)}.xcodeproj`;
const iosProjectDir = path.resolve(projectDir, 'ios');

const iosPodsFile = path.resolve(iosProjectDir, `${projectName}.xcworkspace`);
const isUsingPods = fs.existsSync(iosPodsFile);

const relativeXcodeProjectPath = path.relative(
process.cwd(),
xcodeProjectPath,
isUsingPods
? iosPodsFile
: path.resolve(iosProjectDir, `${projectName}.xcodeproj`),
);

logger.log(`
Expand Down
83 changes: 83 additions & 0 deletions packages/cli/src/tools/installPods.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// @flow
import fs from 'fs-extra';
import execa from 'execa';
import chalk from 'chalk';
import Ora from 'ora';
import inquirer from 'inquirer';
import commandExists from 'command-exists';
import {logger} from '@react-native-community/cli-tools';

async function installPods({
projectName,
loader,
}: {
projectName: string,
loader: typeof Ora,
}) {
try {
process.chdir('ios');

const hasPods = await fs.pathExists('Podfile');

if (!hasPods) {
return;
}

try {
await commandExists('pod');
} catch (e) {
loader.stop();

const {shouldInstallCocoaPods} = await inquirer.prompt([
{
type: 'confirm',
name: 'shouldInstallCocoaPods',
message: 'CocoaPods is not installed, do you want to install it?',
},
]);

if (shouldInstallCocoaPods) {
try {
// First attempt to install `cocoapods`
await execa('gem', ['install', 'cocoapods']);
} catch (_error) {
try {
// If that doesn't work then try with sudo
await execa('sudo', ['gem', 'install', 'cocoapods']);
} catch (error) {
logger.log(error.stderr);

throw new Error(
`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/',
)}`,
);
}
}

// This only shows when `CocoaPods` is automatically installed,
// if it's already installed then we just show the `Installing dependencies` step
loader.start('Installing CocoaPods dependencies');
}
}

try {
await execa('pod', ['install']);
} catch (error) {
// "pod" command outputs errors to stdout (at least some of them)
logger.log(error.stderr || error.stdout);

throw new Error(
`Failed to install CocoaPods dependencies for iOS project, which is required by this template.\nPlease try again manually: "cd ./${projectName}/ios && pod install".\nCocoaPods documentation: ${chalk.dim.underline(
'https://cocoapods.org/',
)}`,
);
}
} catch (error) {
throw error;
} finally {
process.chdir('..');
}
}

export default installPods;
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2787,6 +2787,11 @@ combined-stream@^1.0.6, combined-stream@~1.0.6:
dependencies:
delayed-stream "~1.0.0"

command-exists@^1.2.8:
version "1.2.8"
resolved "https://registry.yarnpkg.com/command-exists/-/command-exists-1.2.8.tgz#715acefdd1223b9c9b37110a149c6392c2852291"
integrity sha512-PM54PkseWbiiD/mMsbvW351/u+dafwTJ0ye2qB60G1aGQP9j3xK2gmMDc+R34L3nDtx4qMCitXT75mkbkGJDLw==

commander@^2.19.0, commander@~2.19.0:
version "2.19.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-2.19.0.tgz#f6198aa84e5b83c46054b94ddedbfed5ee9ff12a"
Expand Down

0 comments on commit f687ff4

Please sign in to comment.