Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up Yarn detection and install code #1223

Merged
merged 1 commit into from
Dec 10, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Clean up Yarn detection and install code
* Remove the “‘yarn’ is not recognized as an internal or external
  command, ...” message on Windows
* Simplify the detection code: just run `yarn --version` – if it
  succeeds use `yarn`, if it fails use `npm`.
  • Loading branch information
fson committed Dec 10, 2016
commit f0ecbf3adb32ed3f89f9bfc7585f48fd9161a15d
63 changes: 24 additions & 39 deletions packages/create-react-app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

var fs = require('fs');
var path = require('path');
var execSync = require('child_process').execSync;
var spawn = require('cross-spawn');
var chalk = require('chalk');
var semver = require('semver');
Expand Down Expand Up @@ -107,49 +108,33 @@ function createApp(name, verbose, version) {
run(root, appName, version, verbose, originalDirectory);
}

function shouldUseYarn() {
try {
execSync('yarn --version', {stdio: 'ignore'});
return true;
} catch (e) {
return false;
}
}

function install(packageToInstall, verbose, callback) {
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 command;
var args;
if (shouldUseYarn()) {
command = 'yarn';
args = [ 'add', '--dev', '--exact', packageToInstall];
} else {
command = 'npm';
args = ['install', '--save-dev', '--save-exact', packageToInstall];
}

var yarnArgs = [
'add',
'--dev',
'--exact',
packageToInstall,
];
var yarnProc;
var yarnExists = true;
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;
if (verbose) {
args.push('--verbose');
}
yarnProc.on('error', function (err) {
if (err.code === 'ENOENT') {
yarnExists = false;
}
});
yarnProc.on('close', function (code) {
if (yarnExists) {
callback(code, 'yarn', yarnArgs);
} else {
fallbackToNpm();
}

var child = spawn(command, args, {stdio: 'inherit'});
child.on('close', function(code) {
callback(code, command, args);
});
}

Expand Down