Commit 2e02e36b authored by Ville Immonen's avatar Ville Immonen Committed by GitHub
Browse files

Clean up Yarn detection and install code (#1223)

* 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`.
parent 39a5b4de
No related merge requests found
Showing with 24 additions and 39 deletions
+24 -39
...@@ -40,6 +40,7 @@ ...@@ -40,6 +40,7 @@
var fs = require('fs'); var fs = require('fs');
var path = require('path'); var path = require('path');
var execSync = require('child_process').execSync;
var spawn = require('cross-spawn'); var spawn = require('cross-spawn');
var chalk = require('chalk'); var chalk = require('chalk');
var semver = require('semver'); var semver = require('semver');
...@@ -107,49 +108,33 @@ function createApp(name, verbose, version) { ...@@ -107,49 +108,33 @@ function createApp(name, verbose, version) {
run(root, appName, version, verbose, originalDirectory); 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 install(packageToInstall, verbose, callback) {
function fallbackToNpm() { var command;
var npmArgs = [ var args;
'install', if (shouldUseYarn()) {
verbose && '--verbose', command = 'yarn';
'--save-dev', args = [ 'add', '--dev', '--exact', packageToInstall];
'--save-exact', } else {
packageToInstall, command = 'npm';
].filter(function(e) { return e; }); args = ['install', '--save-dev', '--save-exact', packageToInstall];
var npmProc = spawn('npm', npmArgs, {stdio: 'inherit'});
npmProc.on('close', function (code) {
callback(code, 'npm', npmArgs);
});
} }
var yarnArgs = [ if (verbose) {
'add', args.push('--verbose');
'--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;
} }
yarnProc.on('error', function (err) {
if (err.code === 'ENOENT') { var child = spawn(command, args, {stdio: 'inherit'});
yarnExists = false; child.on('close', function(code) {
} callback(code, command, args);
});
yarnProc.on('close', function (code) {
if (yarnExists) {
callback(code, 'yarn', yarnArgs);
} else {
fallbackToNpm();
}
}); });
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment