From 9a65bce7cb1b2038e9f98d2c998270f51d064510 Mon Sep 17 00:00:00 2001 From: Lucas Bento Date: Tue, 7 May 2019 11:10:45 +0200 Subject: [PATCH 01/19] Specify `.xcworkspace` on run instructions instead of `.xcodeproj` --- packages/cli/src/commands/init/printRunInstructions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/src/commands/init/printRunInstructions.js b/packages/cli/src/commands/init/printRunInstructions.js index 220c146fe..8a9205fe6 100644 --- a/packages/cli/src/commands/init/printRunInstructions.js +++ b/packages/cli/src/commands/init/printRunInstructions.js @@ -18,7 +18,7 @@ function printRunInstructions(projectDir: string, projectName: string) { projectDir, 'ios', projectName, - )}.xcodeproj`; + )}.xcworkspace`; const relativeXcodeProjectPath = path.relative( process.cwd(), xcodeProjectPath, From 21ffb3f79c9face422eaf7b69d73e2dc03885667 Mon Sep 17 00:00:00 2001 From: Lucas Bento Date: Tue, 7 May 2019 11:19:40 +0200 Subject: [PATCH 02/19] Add `command-exists` package --- packages/cli/package.json | 1 + yarn.lock | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/packages/cli/package.json b/packages/cli/package.json index 4feb0411f..9ee5b969a 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -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", diff --git a/yarn.lock b/yarn.lock index c3750962c..2b6b68669 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" From dc72c00371ca4c8a9c63d4884ba9002c860fad48 Mon Sep 17 00:00:00 2001 From: Lucas Bento Date: Tue, 7 May 2019 11:19:58 +0200 Subject: [PATCH 03/19] Remove unneeded space after log message --- packages/cli/src/commands/init/init.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/src/commands/init/init.js b/packages/cli/src/commands/init/init.js index c3b306b71..cfdb50f76 100644 --- a/packages/cli/src/commands/init/init.js +++ b/packages/cli/src/commands/init/init.js @@ -74,7 +74,7 @@ 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 '); + loader.start('Executing post init script'); await executePostInitScript(name, postInitScript, templateSourceDir); loader.succeed(); } From 9b9e659e5dad72f0e85656867a88ba323944b16f Mon Sep 17 00:00:00 2001 From: Lucas Bento Date: Tue, 7 May 2019 11:20:50 +0200 Subject: [PATCH 04/19] Run `pod install` and check for cocoa pods when running `init` command --- packages/cli/src/commands/init/init.js | 67 ++++++++++++++++++++++-- packages/cli/src/tools/packageManager.js | 42 +++++++++++++-- 2 files changed, 100 insertions(+), 9 deletions(-) diff --git a/packages/cli/src/commands/init/init.js b/packages/cli/src/commands/init/init.js index cfdb50f76..2a6392b37 100644 --- a/packages/cli/src/commands/init/init.js +++ b/packages/cli/src/commands/init/init.js @@ -2,8 +2,10 @@ 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 inquirer from 'inquirer'; import type {ConfigT} from 'types'; import {validateProjectName} from './validate'; import DirectoryAlreadyExistsError from './errors/DirectoryAlreadyExistsError'; @@ -80,8 +82,7 @@ async function createFromExternalTemplate( } 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); @@ -90,6 +91,65 @@ async function createFromExternalTemplate( } } +async function installDependencies({ + npm, + loader, +}: { + npm?: boolean, + loader: typeof Ora, +}) { + const {error} = await PackageManager.installAll({ + preferYarn: !npm, + silent: true, + }); + loader.succeed(); + + // TODO: if this is different than debian just return + if (!error) { + return; + } + + if (error === PackageManager.ERRORS.failedToRunPodInstall) { + return logger.warn( + 'Failed to run "pod install", please try to run it manually.', + ); + } + + if (error === PackageManager.ERRORS.noCocoaPods) { + const {shouldInstallCocoaPods} = await inquirer.prompt([ + { + type: 'confirm', + name: 'shouldInstallCocoaPods', + message: 'CocoaPods is not installed, Do you want to install it?', + }, + ]); + + if (shouldInstallCocoaPods) { + try { + await PackageManager.installCocoaPods(); + } catch (err) { + throw new Error( + 'Error occurred while trying to install CocoaPods, please install it manually.', + ); + } + + try { + loader.start('Installing pods'); + await PackageManager.installPods({silent: true}); + return loader.succeed(); + } catch (err) { + return logger.warn( + 'Failed to run "pod install", please try to run it manually.', + ); + } + } + + return logger.warn( + 'Please install CocoaPods and run "pod install" to install all dependencies.', + ); + } +} + async function createFromReactNativeTemplate( projectName: string, version: string, @@ -145,8 +205,7 @@ async function createFromReactNativeTemplate( } 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); diff --git a/packages/cli/src/tools/packageManager.js b/packages/cli/src/tools/packageManager.js index b42519b68..14990363a 100644 --- a/packages/cli/src/tools/packageManager.js +++ b/packages/cli/src/tools/packageManager.js @@ -1,5 +1,6 @@ // @flow import execa from 'execa'; +import commandExists from 'command-exists'; import logger from './logger'; import {getYarnVersionIfAvailable, isProjectUsingYarn} from './yarn'; @@ -11,7 +12,12 @@ type Options = {| let projectDir; -function executeCommand( +export const ERRORS = { + noCocoaPods: 'noCocoaPods', + failedToRunPodInstall: 'failedToRunPodInstall', +}; + +export function executeCommand( command: string, args: Array, options?: Options, @@ -61,8 +67,34 @@ export function uninstall(packageNames: Array, options?: Options) { : executeCommand('npm', ['uninstall', ...packageNames, '--save'], options); } -export function installAll(options?: Options) { - return shouldUseYarn(options) - ? executeCommand('yarn', ['install'], options) - : executeCommand('npm', ['install'], options); +export function installCocoaPods() { + return executeCommand('sudo', ['gem', 'install', 'cocoapods'], { + silent: false, + }); +} + +export async function installPods(options?: Options) { + try { + await commandExists('pod'); + } catch (err) { + return {error: ERRORS.noCocoaPods}; + } + + process.chdir('ios'); + + try { + return await executeCommand('pod', ['install'], options); + } catch (err) { + return {error: ERRORS.failedToRunPodInstall}; + } +} + +export async function installAll(options?: Options) { + shouldUseYarn(options) + ? await executeCommand('yarn', ['install'], options) + : await executeCommand('npm', ['install'], options); + + // TODO: only do this on macOS + + return installPods(options); } From 2c9b8baddf8105ddaded5daa914257bca9da7bd2 Mon Sep 17 00:00:00 2001 From: Lucas Bento Date: Tue, 7 May 2019 13:53:29 +0200 Subject: [PATCH 05/19] Put pods installation directly on `init` file and verify if `Podfile` exists before running --- packages/cli/src/commands/init/init.js | 75 +++++++++++++++--------- packages/cli/src/tools/packageManager.js | 37 ++---------- 2 files changed, 52 insertions(+), 60 deletions(-) diff --git a/packages/cli/src/commands/init/init.js b/packages/cli/src/commands/init/init.js index 2a6392b37..408cc1b11 100644 --- a/packages/cli/src/commands/init/init.js +++ b/packages/cli/src/commands/init/init.js @@ -2,10 +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'; @@ -22,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, @@ -81,7 +84,6 @@ async function createFromExternalTemplate( loader.succeed(); } - loader.start('Installing all required dependencies'); await installDependencies({npm, loader}); } catch (e) { loader.fail(); @@ -91,31 +93,20 @@ async function createFromExternalTemplate( } } -async function installDependencies({ - npm, - loader, -}: { - npm?: boolean, - loader: typeof Ora, -}) { - const {error} = await PackageManager.installAll({ - preferYarn: !npm, - silent: true, - }); - loader.succeed(); +async function installPods(loader: typeof Ora) { + process.chdir('ios'); + + const hasPods = await fs.pathExists('Podfile'); - // TODO: if this is different than debian just return - if (!error) { + if (!hasPods) { return; } - if (error === PackageManager.ERRORS.failedToRunPodInstall) { - return logger.warn( - 'Failed to run "pod install", please try to run it manually.', - ); - } + try { + await commandExists('pod'); + } catch (err) { + loader.succeed(); - if (error === PackageManager.ERRORS.noCocoaPods) { const {shouldInstallCocoaPods} = await inquirer.prompt([ { type: 'confirm', @@ -126,30 +117,59 @@ async function installDependencies({ if (shouldInstallCocoaPods) { try { - await PackageManager.installCocoaPods(); + await execa('sudo', ['gem', 'install', 'cocoapods'], { + stdio: 'pipe', + }); } catch (err) { throw new Error( 'Error occurred while trying to install CocoaPods, please install it manually.', + err, ); } try { loader.start('Installing pods'); - await PackageManager.installPods({silent: true}); - return loader.succeed(); } catch (err) { - return logger.warn( + console.log(err); + throw new CLIError( 'Failed to run "pod install", please try to run it manually.', + err, ); } } + } - return logger.warn( - 'Please install CocoaPods and run "pod install" to install all dependencies.', + try { + await execa('pod', ['install'], { + stdio: 'pipe', + }); + } catch (err) { + throw new CLIError( + 'Failed to run "pod install", please try to run it manually.', + err, ); } } +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, @@ -204,7 +224,6 @@ async function createFromReactNativeTemplate( loader.succeed(); } - loader.start('Installing all required dependencies'); await installDependencies({npm, loader}); } catch (e) { loader.fail(); diff --git a/packages/cli/src/tools/packageManager.js b/packages/cli/src/tools/packageManager.js index 14990363a..b257485ec 100644 --- a/packages/cli/src/tools/packageManager.js +++ b/packages/cli/src/tools/packageManager.js @@ -1,6 +1,5 @@ // @flow import execa from 'execa'; -import commandExists from 'command-exists'; import logger from './logger'; import {getYarnVersionIfAvailable, isProjectUsingYarn} from './yarn'; @@ -17,7 +16,7 @@ export const ERRORS = { failedToRunPodInstall: 'failedToRunPodInstall', }; -export function executeCommand( +function executeCommand( command: string, args: Array, options?: Options, @@ -67,34 +66,8 @@ export function uninstall(packageNames: Array, options?: Options) { : executeCommand('npm', ['uninstall', ...packageNames, '--save'], options); } -export function installCocoaPods() { - return executeCommand('sudo', ['gem', 'install', 'cocoapods'], { - silent: false, - }); -} - -export async function installPods(options?: Options) { - try { - await commandExists('pod'); - } catch (err) { - return {error: ERRORS.noCocoaPods}; - } - - process.chdir('ios'); - - try { - return await executeCommand('pod', ['install'], options); - } catch (err) { - return {error: ERRORS.failedToRunPodInstall}; - } -} - -export async function installAll(options?: Options) { - shouldUseYarn(options) - ? await executeCommand('yarn', ['install'], options) - : await executeCommand('npm', ['install'], options); - - // TODO: only do this on macOS - - return installPods(options); +export function installAll(options?: Options) { + return shouldUseYarn(options) + ? executeCommand('yarn', ['install'], options) + : executeCommand('npm', ['install'], options); } From 33d4d7647b46b25578819e585565b5088271ced0 Mon Sep 17 00:00:00 2001 From: Lucas Bento Date: Tue, 7 May 2019 14:07:57 +0200 Subject: [PATCH 06/19] Revert change to space --- packages/cli/src/commands/init/init.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/src/commands/init/init.js b/packages/cli/src/commands/init/init.js index 408cc1b11..4a57ba9d9 100644 --- a/packages/cli/src/commands/init/init.js +++ b/packages/cli/src/commands/init/init.js @@ -79,7 +79,7 @@ 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'); + loader.start('Executing post init script '); await executePostInitScript(name, postInitScript, templateSourceDir); loader.succeed(); } From a539f7ad7819fb5fbf473f30523cb82626b5b8f4 Mon Sep 17 00:00:00 2001 From: Lucas Bento Date: Tue, 7 May 2019 14:08:08 +0200 Subject: [PATCH 07/19] Remove unneeded `console.log` --- packages/cli/src/commands/init/init.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/cli/src/commands/init/init.js b/packages/cli/src/commands/init/init.js index 4a57ba9d9..b697ae085 100644 --- a/packages/cli/src/commands/init/init.js +++ b/packages/cli/src/commands/init/init.js @@ -130,7 +130,6 @@ async function installPods(loader: typeof Ora) { try { loader.start('Installing pods'); } catch (err) { - console.log(err); throw new CLIError( 'Failed to run "pod install", please try to run it manually.', err, From 20e2922c3f3e34b895533d1fb73466fdc30d2f25 Mon Sep 17 00:00:00 2001 From: Lucas Bento Date: Tue, 7 May 2019 14:37:18 +0200 Subject: [PATCH 08/19] `cd` back to the directory after pod installation --- packages/cli/src/commands/init/init.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/cli/src/commands/init/init.js b/packages/cli/src/commands/init/init.js index b697ae085..60f450adc 100644 --- a/packages/cli/src/commands/init/init.js +++ b/packages/cli/src/commands/init/init.js @@ -164,7 +164,15 @@ async function installDependencies({ silent: true, }); - await installPods(loader); + try { + await installPods(loader); + + process.chdir('..'); + } catch (err) { + process.chdir('..'); + + throw err; + } loader.succeed(); } From 7d52e53f3b8f91533e4ac5eafc44bdf4b378af3e Mon Sep 17 00:00:00 2001 From: Lucas Bento Date: Tue, 7 May 2019 15:09:24 +0200 Subject: [PATCH 09/19] Throw with `CLIError` instead of `Error` --- packages/cli/src/commands/init/init.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/src/commands/init/init.js b/packages/cli/src/commands/init/init.js index 60f450adc..7b4db713b 100644 --- a/packages/cli/src/commands/init/init.js +++ b/packages/cli/src/commands/init/init.js @@ -121,7 +121,7 @@ async function installPods(loader: typeof Ora) { stdio: 'pipe', }); } catch (err) { - throw new Error( + throw new CLIError( 'Error occurred while trying to install CocoaPods, please install it manually.', err, ); From df3544492a700e9f2f252228dc73df1e4a47aa39 Mon Sep 17 00:00:00 2001 From: Lucas Bento Date: Thu, 9 May 2019 13:09:05 +0200 Subject: [PATCH 10/19] =?UTF-8?q?Remove=20unneeded=20`try=E2=80=A6catch`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/cli/src/commands/init/init.js | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/packages/cli/src/commands/init/init.js b/packages/cli/src/commands/init/init.js index 7b4db713b..f7d399818 100644 --- a/packages/cli/src/commands/init/init.js +++ b/packages/cli/src/commands/init/init.js @@ -127,14 +127,7 @@ async function installPods(loader: typeof Ora) { ); } - try { - loader.start('Installing pods'); - } catch (err) { - throw new CLIError( - 'Failed to run "pod install", please try to run it manually.', - err, - ); - } + loader.start('Installing pods'); } } From 5b06aaaea08e059d9a9b0799575071fd27f3b689 Mon Sep 17 00:00:00 2001 From: Lucas Bento Date: Thu, 9 May 2019 13:09:18 +0200 Subject: [PATCH 11/19] Fix wrong uppercased word --- packages/cli/src/commands/init/init.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/src/commands/init/init.js b/packages/cli/src/commands/init/init.js index f7d399818..d21f8e72e 100644 --- a/packages/cli/src/commands/init/init.js +++ b/packages/cli/src/commands/init/init.js @@ -111,7 +111,7 @@ async function installPods(loader: typeof Ora) { { type: 'confirm', name: 'shouldInstallCocoaPods', - message: 'CocoaPods is not installed, Do you want to install it?', + message: 'CocoaPods is not installed, do you want to install it?', }, ]); From d424c8d411f94d2020786c8819c50e5d278a4b26 Mon Sep 17 00:00:00 2001 From: Lucas Bento Date: Thu, 9 May 2019 13:09:47 +0200 Subject: [PATCH 12/19] Try to install `cocoapods` without sudo before trying with it --- packages/cli/src/commands/init/init.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/cli/src/commands/init/init.js b/packages/cli/src/commands/init/init.js index d21f8e72e..b2f9e716f 100644 --- a/packages/cli/src/commands/init/init.js +++ b/packages/cli/src/commands/init/init.js @@ -117,14 +117,22 @@ async function installPods(loader: typeof Ora) { if (shouldInstallCocoaPods) { try { - await execa('sudo', ['gem', 'install', 'cocoapods'], { + // First attempt to install `cocoapods` + await execa('gem', ['install', 'cocoapods'], { stdio: 'pipe', }); } catch (err) { - throw new CLIError( - 'Error occurred while trying to install CocoaPods, please install it manually.', - err, - ); + try { + // If that doesn't work then try with sudo + await execa('sudo', ['gem', 'install', 'cocoapods'], { + stdio: 'pipe', + }); + } catch (err) { + throw new CLIError( + 'Error occurred while trying to install CocoaPods, please run this command again.', + err, + ); + } } loader.start('Installing pods'); From e7342d6d4f5f029094110b05447c039554bc6bd7 Mon Sep 17 00:00:00 2001 From: Lucas Bento Date: Thu, 9 May 2019 13:10:00 +0200 Subject: [PATCH 13/19] Run pods installation only on macOS --- packages/cli/src/commands/init/init.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/cli/src/commands/init/init.js b/packages/cli/src/commands/init/init.js index b2f9e716f..664c3c66d 100644 --- a/packages/cli/src/commands/init/init.js +++ b/packages/cli/src/commands/init/init.js @@ -165,14 +165,10 @@ async function installDependencies({ silent: true, }); - try { + if (process.platform === 'darwin') { await installPods(loader); process.chdir('..'); - } catch (err) { - process.chdir('..'); - - throw err; } loader.succeed(); From db8e0bb5f88a4ac263a4629cf5b84556d59c124f Mon Sep 17 00:00:00 2001 From: Lucas Bento Date: Thu, 9 May 2019 13:10:29 +0200 Subject: [PATCH 14/19] Check which extension for the project to print out on the run instructions for iOS --- packages/cli/src/commands/init/init.js | 2 +- .../src/commands/init/printRunInstructions.js | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/packages/cli/src/commands/init/init.js b/packages/cli/src/commands/init/init.js index 664c3c66d..aebc121df 100644 --- a/packages/cli/src/commands/init/init.js +++ b/packages/cli/src/commands/init/init.js @@ -272,7 +272,7 @@ export default (async function initialize( try { await createProject(projectName, options, version); - printRunInstructions(process.cwd(), projectName); + await printRunInstructions(process.cwd(), projectName); } catch (e) { logger.error(e.message); fs.removeSync(projectName); diff --git a/packages/cli/src/commands/init/printRunInstructions.js b/packages/cli/src/commands/init/printRunInstructions.js index 8a9205fe6..e958b6229 100644 --- a/packages/cli/src/commands/init/printRunInstructions.js +++ b/packages/cli/src/commands/init/printRunInstructions.js @@ -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, - )}.xcworkspace`; + const iosProjectDir = path.resolve(projectDir, 'ios'); + + const iosPodsFile = path.resolve(iosProjectDir, `${projectName}.xcworkspace`); + const isUsingPods = await fs.pathExists(iosPodsFile); + const relativeXcodeProjectPath = path.relative( process.cwd(), - xcodeProjectPath, + isUsingPods + ? iosPodsFile + : path.resolve(iosProjectDir, `${projectName}.xcodeproj`), ); logger.log(` From 3785aeafb060912c41e6b2b2ed01a2b498dba9a8 Mon Sep 17 00:00:00 2001 From: Lucas Bento Date: Sat, 11 May 2019 19:57:06 +0200 Subject: [PATCH 15/19] Use `fs` instead of `fs-extra` --- packages/cli/src/commands/init/init.js | 2 +- packages/cli/src/commands/init/printRunInstructions.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/cli/src/commands/init/init.js b/packages/cli/src/commands/init/init.js index 063dbd574..ff39acd29 100644 --- a/packages/cli/src/commands/init/init.js +++ b/packages/cli/src/commands/init/init.js @@ -228,7 +228,7 @@ export default (async function initialize( try { await createProject(projectName, options, version); - await printRunInstructions(process.cwd(), projectName); + printRunInstructions(process.cwd(), projectName); } catch (e) { logger.error(e.message); fs.removeSync(projectName); diff --git a/packages/cli/src/commands/init/printRunInstructions.js b/packages/cli/src/commands/init/printRunInstructions.js index e958b6229..43258c85a 100644 --- a/packages/cli/src/commands/init/printRunInstructions.js +++ b/packages/cli/src/commands/init/printRunInstructions.js @@ -9,16 +9,16 @@ */ import path from 'path'; -import fs from 'fs-extra'; +import fs from 'fs'; import chalk from 'chalk'; import {logger} from '@react-native-community/cli-tools'; -async function printRunInstructions(projectDir: string, projectName: string) { +function printRunInstructions(projectDir: string, projectName: string) { const absoluteProjectDir = path.resolve(projectDir); const iosProjectDir = path.resolve(projectDir, 'ios'); const iosPodsFile = path.resolve(iosProjectDir, `${projectName}.xcworkspace`); - const isUsingPods = await fs.pathExists(iosPodsFile); + const isUsingPods = fs.existsSync(iosPodsFile); const relativeXcodeProjectPath = path.relative( process.cwd(), From a590772871f5eef2e5cd425c04500142db72d139 Mon Sep 17 00:00:00 2001 From: Lucas Bento Date: Sat, 11 May 2019 19:58:59 +0200 Subject: [PATCH 16/19] Move `installPods` to separate file --- packages/cli/src/commands/init/init.js | 71 ++---------------------- packages/cli/src/tools/installPods.js | 77 ++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 66 deletions(-) create mode 100644 packages/cli/src/tools/installPods.js diff --git a/packages/cli/src/commands/init/init.js b/packages/cli/src/commands/init/init.js index ff39acd29..288214046 100644 --- a/packages/cli/src/commands/init/init.js +++ b/packages/cli/src/commands/init/init.js @@ -2,12 +2,9 @@ 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'; @@ -21,10 +18,10 @@ 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'; -import {CLIError} from '@react-native-community/cli-tools/build/errors'; type Options = {| template?: string, @@ -98,7 +95,7 @@ async function createFromTemplate({ loader.succeed(); } - await installDependencies({npm, loader}); + await installDependencies({projectName, npm, loader}); } catch (e) { loader.fail(); throw new Error(e); @@ -107,68 +104,12 @@ async function createFromTemplate({ } } -async function installPods(loader: typeof Ora) { - process.chdir('ios'); - - const hasPods = await fs.pathExists('Podfile'); - - if (!hasPods) { - return; - } - - try { - await commandExists('pod'); - } catch (err) { - loader.succeed(); - - 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', - }); - } catch (err) { - try { - // If that doesn't work then try with sudo - await execa('sudo', ['gem', 'install', 'cocoapods'], { - stdio: 'pipe', - }); - } catch (err) { - throw new CLIError( - 'Error occurred while trying to install CocoaPods, please run this command again.', - err, - ); - } - } - - loader.start('Installing pods'); - } - } - - try { - await execa('pod', ['install'], { - stdio: 'pipe', - }); - } catch (err) { - throw new CLIError( - 'Failed to run "pod install", please try to run it manually.', - err, - ); - } -} - async function installDependencies({ + projectName, npm, loader, }: { + projectName: string, npm?: boolean, loader: typeof Ora, }) { @@ -180,9 +121,7 @@ async function installDependencies({ }); if (process.platform === 'darwin') { - await installPods(loader); - - process.chdir('..'); + await installPods({projectName, loader}); } loader.succeed(); diff --git a/packages/cli/src/tools/installPods.js b/packages/cli/src/tools/installPods.js new file mode 100644 index 000000000..7693d86c6 --- /dev/null +++ b/packages/cli/src/tools/installPods.js @@ -0,0 +1,77 @@ +import fs from 'fs-extra'; +import execa from 'execa'; +import chalk from 'chalk'; +import inquirer from 'inquirer'; +import commandExists from 'command-exists'; + +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 (err) { + 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 (err) { + try { + // If that doesn't work then try with sudo + await execa('sudo', ['gem', 'install', 'cocoapods']); + } catch (err) { + 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 (err) { + logger.log(err.stderr || err.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 (err) { + throw err; + } finally { + process.chdir('..'); + } +} + +export default installPods; From 39f76f900420990e265d95ae22b7e19c051dc0f0 Mon Sep 17 00:00:00 2001 From: Lucas Bento Date: Sat, 11 May 2019 20:05:50 +0200 Subject: [PATCH 17/19] Add missing `logger` import --- packages/cli/src/tools/installPods.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/cli/src/tools/installPods.js b/packages/cli/src/tools/installPods.js index 7693d86c6..d33dc2667 100644 --- a/packages/cli/src/tools/installPods.js +++ b/packages/cli/src/tools/installPods.js @@ -3,6 +3,7 @@ import execa from 'execa'; import chalk from 'chalk'; import inquirer from 'inquirer'; import commandExists from 'command-exists'; +import {logger} from '@react-native-community/cli-tools'; async function installPods({ projectName, From 8be9c93b87df5c2c8e628e42f5b90eca485d681f Mon Sep 17 00:00:00 2001 From: Lucas Bento Date: Sat, 11 May 2019 20:06:10 +0200 Subject: [PATCH 18/19] Fix warnings on duplicated variable declaration --- packages/cli/src/tools/installPods.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/tools/installPods.js b/packages/cli/src/tools/installPods.js index d33dc2667..fd0af43b3 100644 --- a/packages/cli/src/tools/installPods.js +++ b/packages/cli/src/tools/installPods.js @@ -23,7 +23,7 @@ async function installPods({ try { await commandExists('pod'); - } catch (err) { + } catch (e) { loader.stop(); const {shouldInstallCocoaPods} = await inquirer.prompt([ @@ -42,7 +42,7 @@ async function installPods({ try { // If that doesn't work then try with sudo await execa('sudo', ['gem', 'install', 'cocoapods']); - } catch (err) { + } catch (error) { 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/', From c5e5dae6b255bc55b61d8f35fed0d26afa3de111 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Sun, 12 May 2019 09:29:28 +0200 Subject: [PATCH 19/19] fixups --- packages/cli/src/tools/installPods.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/cli/src/tools/installPods.js b/packages/cli/src/tools/installPods.js index fd0af43b3..d59b7781e 100644 --- a/packages/cli/src/tools/installPods.js +++ b/packages/cli/src/tools/installPods.js @@ -1,6 +1,8 @@ +// @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'; @@ -38,11 +40,13 @@ async function installPods({ try { // First attempt to install `cocoapods` await execa('gem', ['install', 'cocoapods']); - } catch (err) { + } 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/', @@ -59,8 +63,9 @@ async function installPods({ try { await execa('pod', ['install']); - } catch (err) { - logger.log(err.stderr || err.stdout); + } 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( @@ -68,8 +73,8 @@ async function installPods({ )}`, ); } - } catch (err) { - throw err; + } catch (error) { + throw error; } finally { process.chdir('..'); }