Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: automatically run pod install when running init command #373

Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.17",
"@react-native-community/cli-tools": "^2.0.0-alpha.17",
"chalk": "^1.1.1",
"command-exists": "^1.2.8",
"commander": "^2.19.0",
"compression": "^1.7.1",
"connect": "^3.6.5",
Expand Down
92 changes: 88 additions & 4 deletions packages/cli/src/commands/init/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
import os from 'os';
import path from 'path';
import fs from 'fs-extra';
import execa from 'execa';
import Ora from 'ora';
import minimist from 'minimist';
import semver from 'semver';
import inquirer from 'inquirer';
import commandExists from 'command-exists';
import type {ConfigT} from 'types';
import {validateProjectName} from './validate';
import DirectoryAlreadyExistsError from './errors/DirectoryAlreadyExistsError';
Expand All @@ -20,6 +24,7 @@ import * as PackageManager from '../../tools/packageManager';
import {processTemplateName} from './templateName';
import banner from './banner';
import {getLoader} from '../../tools/loader';
import {CLIError} from '@react-native-community/cli-tools/build/errors';

type Options = {|
template?: string,
Expand Down Expand Up @@ -93,9 +98,7 @@ async function createFromTemplate({
loader.succeed();
}

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

async function installPods(loader: typeof Ora) {
process.chdir('ios');
lucasbento marked this conversation as resolved.
Show resolved Hide resolved

const hasPods = await fs.pathExists('Podfile');
lucasbento marked this conversation as resolved.
Show resolved Hide resolved

if (!hasPods) {
return;
}

try {
await commandExists('pod');
} catch (err) {
loader.succeed();
grabbou marked this conversation as resolved.
Show resolved Hide resolved
lucasbento marked this conversation as resolved.
Show resolved Hide resolved

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'], {
stdio: 'pipe',
});
lucasbento marked this conversation as resolved.
Show resolved Hide resolved
} catch (err) {
try {
// If that doesn't work then try with sudo
await execa('sudo', ['gem', 'install', 'cocoapods'], {
stdio: 'pipe',
lucasbento marked this conversation as resolved.
Show resolved Hide resolved
});
} catch (err) {
throw new CLIError(
'Error occurred while trying to install CocoaPods, please run this command again.',
lucasbento marked this conversation as resolved.
Show resolved Hide resolved
err,
);
}
}

loader.start('Installing pods');
lucasbento marked this conversation as resolved.
Show resolved Hide resolved
}
}

try {
lucasbento marked this conversation as resolved.
Show resolved Hide resolved
await execa('pod', ['install'], {
stdio: 'pipe',
});
} catch (err) {
throw new CLIError(
'Failed to run "pod install", please try to run it manually.',
err,
lucasbento marked this conversation as resolved.
Show resolved Hide resolved
);
}
}

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

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

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

lucasbento marked this conversation as resolved.
Show resolved Hide resolved
process.chdir('..');
lucasbento marked this conversation as resolved.
Show resolved Hide resolved
}

loader.succeed();
}

function createProject(projectName: string, options: Options, version: string) {
fs.mkdirSync(projectName);
process.chdir(projectName);
Expand Down Expand Up @@ -144,7 +228,7 @@ export default (async function initialize(
try {
await createProject(projectName, options, version);

printRunInstructions(process.cwd(), projectName);
await printRunInstructions(process.cwd(), projectName);
lucasbento marked this conversation as resolved.
Show resolved Hide resolved
} catch (e) {
logger.error(e.message);
fs.removeSync(projectName);
Expand Down
17 changes: 10 additions & 7 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-extra';
import chalk from 'chalk';
import {logger} from '@react-native-community/cli-tools';

function printRunInstructions(projectDir: string, projectName: string) {
async 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 = await fs.pathExists(iosPodsFile);
lucasbento marked this conversation as resolved.
Show resolved Hide resolved

const relativeXcodeProjectPath = path.relative(
process.cwd(),
xcodeProjectPath,
isUsingPods
thymikee marked this conversation as resolved.
Show resolved Hide resolved
? iosPodsFile
: path.resolve(iosProjectDir, `${projectName}.xcodeproj`),
);

logger.log(`
Expand Down
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