From 413778662d738964ec7cb38e4b51fb6a4ddaabf9 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Thu, 8 Dec 2016 15:13:37 +0000 Subject: [PATCH 1/2] Catch synchronous errors from spawning yarn --- packages/create-react-app/index.js | 57 +++++++++++++++++------------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/packages/create-react-app/index.js b/packages/create-react-app/index.js index 23d8b5e8de..80faf23bfc 100644 --- a/packages/create-react-app/index.js +++ b/packages/create-react-app/index.js @@ -108,38 +108,47 @@ function createApp(name, verbose, version) { } function install(packageToInstall, verbose, callback) { - var args = [ - 'add', - '--dev', - '--exact', - packageToInstall, - ]; - var proc = spawn('yarn', args, {stdio: 'inherit'}); - - var yarnExists = true; - proc.on('error', function (err) { - if (err.code === 'ENOENT') { - yarnExists = false; - } - }); - proc.on('close', function (code) { - if (yarnExists) { - callback(code, 'yarn', args); - return; - } - // No Yarn installed, continuing with npm. - args = [ + function fallbackToNpm() { + var npmArgs = [ 'install', verbose && '--verbose', '--save-dev', '--save-exact', packageToInstall, ].filter(function(e) { return e; }); - var npmProc = spawn('npm', args, {stdio: 'inherit'}); + var npmProc = spawn('npm', npmArgs, {stdio: 'inherit'}); npmProc.on('close', function (code) { - callback(code, 'npm', args); + callback(code, 'npm', npmArgs); }); - }); + } + + var yarnArgs = [ + 'add', + '--dev', + '--exact', + packageToInstall, + ]; + var yarnProc; + var yarnExists = true; + try { + yarnProc = spawn('yarn', yarnArgs, {stdio: 'inherit'}); + yarnProc.on('error', function (err) { + if (err.code === 'ENOENT') { + yarnExists = false; + } + }); + yarnProc.on('close', function (code) { + if (yarnExists) { + callback(code, 'yarn', yarnArgs); + return; + } + }); + } 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(); + } } function run(root, appName, version, verbose, originalDirectory) { From a87b2796424ceb4d21fdc1e09ff5a0c20bbe9c1d Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Thu, 8 Dec 2016 15:29:04 +0000 Subject: [PATCH 2/2] Fix issues --- packages/create-react-app/index.js | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/packages/create-react-app/index.js b/packages/create-react-app/index.js index 80faf23bfc..403e89edfe 100644 --- a/packages/create-react-app/index.js +++ b/packages/create-react-app/index.js @@ -132,23 +132,25 @@ function install(packageToInstall, verbose, callback) { var yarnExists = true; try { yarnProc = spawn('yarn', yarnArgs, {stdio: 'inherit'}); - yarnProc.on('error', function (err) { - if (err.code === 'ENOENT') { - yarnExists = false; - } - }); - yarnProc.on('close', function (code) { - if (yarnExists) { - callback(code, 'yarn', yarnArgs); - return; - } - }); } 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; + } + }); + yarnProc.on('close', function (code) { + if (yarnExists) { + callback(code, 'yarn', yarnArgs); + } else { + fallbackToNpm(); + } + }); } function run(root, appName, version, verbose, originalDirectory) {