From a7047d5764c67630a7925d00aff2f7c1d2035d00 Mon Sep 17 00:00:00 2001 From: Benjamin Winterberg Date: Fri, 2 Oct 2015 10:51:45 +0200 Subject: [PATCH] Add --verbose option (fixes #2797) --- react-native-cli/index.js | 43 ++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/react-native-cli/index.js b/react-native-cli/index.js index b94bcf267..66b215830 100755 --- a/react-native-cli/index.js +++ b/react-native-cli/index.js @@ -9,6 +9,7 @@ var fs = require('fs'); var path = require('path'); var exec = require('child_process').exec; +var spawn = require('child_process').spawn; var prompt = require('prompt'); var CLI_MODULE_PATH = function() { @@ -42,10 +43,11 @@ if (cli) { switch (args[0]) { case 'init': if (args[1]) { - init(args[1]); + var verbose = process.argv.indexOf('--verbose') >= 0; + init(args[1], verbose); } else { console.error( - 'Usage: react-native init ' + 'Usage: react-native init [--verbose]' ); process.exit(1); } @@ -81,17 +83,17 @@ function validatePackageName(name) { } } -function init(name) { +function init(name, verbose) { validatePackageName(name); if (fs.existsSync(name)) { - createAfterConfirmation(name); + createAfterConfirmation(name, verbose); } else { - createProject(name); + createProject(name, verbose); } } -function createAfterConfirmation(name) { +function createAfterConfirmation(name, verbose) { prompt.start(); var property = { @@ -104,7 +106,7 @@ function createAfterConfirmation(name) { prompt.get(property, function (err, result) { if (result.yesno[0] === 'y') { - createProject(name); + createProject(name, verbose); } else { console.log('Project initialization canceled'); process.exit(); @@ -112,7 +114,7 @@ function createAfterConfirmation(name) { }); } -function createProject(name) { +function createProject(name, verbose) { var root = path.resolve(name); var projectName = path.basename(root); @@ -137,6 +139,15 @@ function createProject(name) { process.chdir(root); console.log('Installing react-native package from npm...'); + + if (verbose) { + runVerbose(root, projectName); + } else { + run(root, projectName); + } +} + +function run(root, projectName) { exec('npm install --save react-native', function(e, stdout, stderr) { if (e) { console.log(stdout); @@ -145,9 +156,21 @@ function createProject(name) { process.exit(1); } - var args = [projectName].concat(process.argv.slice(4)); + var cli = require(CLI_MODULE_PATH()); + cli.init(root, projectName); + }); +} + +function runVerbose(root, projectName) { + var proc = spawn('npm', ['install', '--verbose', '--save', 'react-native'], {stdio: 'inherit'}); + proc.on('close', function (code) { + if (code !== 0) { + console.error('`npm install --save react-native` failed'); + return; + } + cli = require(CLI_MODULE_PATH()); - cli.init(root, args); + cli.init(root, projectName); }); }