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 5 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: 85 additions & 7 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 @@ -74,14 +79,12 @@ async function createFromExternalTemplate(
const {postInitScript} = templateConfig;
if (postInitScript) {
// Leaving trailing space because there may be stdout from the script
loader.start('Executing post init script ');
lucasbento marked this conversation as resolved.
Show resolved Hide resolved
loader.start('Executing post init script');
await executePostInitScript(name, postInitScript, templateSourceDir);
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 @@ -90,6 +93,83 @@ async function createFromExternalTemplate(
}
}

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?',
grabbou marked this conversation as resolved.
Show resolved Hide resolved
},
]);

if (shouldInstallCocoaPods) {
try {
await execa('sudo', ['gem', 'install', 'cocoapods'], {
stdio: 'pipe',
});
lucasbento marked this conversation as resolved.
Show resolved Hide resolved
} catch (err) {
throw new Error(
'Error occurred while trying to install CocoaPods, please install it manually.',
grabbou marked this conversation as resolved.
Show resolved Hide resolved
err,
);
}

try {
loader.start('Installing pods');
} catch (err) {
console.log(err);
throw new CLIError(
'Failed to run "pod install", please try to run it manually.',
lucasbento marked this conversation as resolved.
Show resolved Hide resolved
err,
);
}
}
}

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,
});

await installPods(loader);

loader.succeed();
}

async function createFromReactNativeTemplate(
projectName: string,
version: string,
Expand Down Expand Up @@ -144,9 +224,7 @@ async function createFromReactNativeTemplate(
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 Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/init/printRunInstructions.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function printRunInstructions(projectDir: string, projectName: string) {
projectDir,
'ios',
projectName,
)}.xcodeproj`;
)}.xcworkspace`;
const relativeXcodeProjectPath = path.relative(
process.cwd(),
xcodeProjectPath,
Expand Down
5 changes: 5 additions & 0 deletions packages/cli/src/tools/packageManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ type Options = {|

let projectDir;

export const ERRORS = {
lucasbento marked this conversation as resolved.
Show resolved Hide resolved
noCocoaPods: 'noCocoaPods',
failedToRunPodInstall: 'failedToRunPodInstall',
};

function executeCommand(
command: string,
args: Array<string>,
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