Skip to content

Commit

Permalink
Catch synchronous errors from spawning yarn (facebook#1204)
Browse files Browse the repository at this point in the history
* Catch synchronous errors from spawning yarn

* Fix issues
  • Loading branch information
gaearon authored and randycoulman committed May 8, 2017
1 parent bebf7cd commit 74850e5
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions packages/create-react-app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
}

Expand Down

0 comments on commit 74850e5

Please sign in to comment.