From 74850e50602b761605bf10c237c3b26fb990f0fe Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Thu, 8 Dec 2016 07:36:46 -0800 Subject: [PATCH] Catch synchronous errors from spawning yarn (#1204) * Catch synchronous errors from spawning yarn * Fix issues --- packages/create-react-app/index.js | 49 ++++++++++++++++++------------ 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/packages/create-react-app/index.js b/packages/create-react-app/index.js index 23d8b5e8de..403e89edfe 100644 --- a/packages/create-react-app/index.js +++ b/packages/create-react-app/index.js @@ -108,37 +108,48 @@ function createApp(name, verbose, version) { } function install(packageToInstall, verbose, callback) { - var args = [ + function fallbackToNpm() { + var npmArgs = [ + 'install', + verbose && '--verbose', + '--save-dev', + '--save-exact', + packageToInstall, + ].filter(function(e) { return e; }); + var npmProc = spawn('npm', npmArgs, {stdio: 'inherit'}); + npmProc.on('close', function (code) { + callback(code, 'npm', npmArgs); + }); + } + + var yarnArgs = [ 'add', '--dev', '--exact', packageToInstall, ]; - var proc = spawn('yarn', args, {stdio: 'inherit'}); - + var yarnProc; var yarnExists = true; - proc.on('error', function (err) { + try { + yarnProc = spawn('yarn', yarnArgs, {stdio: 'inherit'}); + } catch (err) { + // It's not clear why we end up here in some cases but we need this. + // https://github.com/facebookincubator/create-react-app/issues/1200 + yarnExists = false; + fallbackToNpm(); + return; + } + yarnProc.on('error', function (err) { if (err.code === 'ENOENT') { yarnExists = false; } }); - proc.on('close', function (code) { + yarnProc.on('close', function (code) { if (yarnExists) { - callback(code, 'yarn', args); - return; + callback(code, 'yarn', yarnArgs); + } else { + fallbackToNpm(); } - // No Yarn installed, continuing with npm. - args = [ - 'install', - verbose && '--verbose', - '--save-dev', - '--save-exact', - packageToInstall, - ].filter(function(e) { return e; }); - var npmProc = spawn('npm', args, {stdio: 'inherit'}); - npmProc.on('close', function (code) { - callback(code, 'npm', args); - }); }); }