Skip to content
This repository was archived by the owner on Jan 26, 2019. It is now read-only.

Commit 2e02e36

Browse files
authored
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`.
1 parent 39a5b4d commit 2e02e36

File tree

1 file changed

+24
-39
lines changed

1 file changed

+24
-39
lines changed

packages/create-react-app/index.js

+24-39
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
var fs = require('fs');
4242
var path = require('path');
43+
var execSync = require('child_process').execSync;
4344
var spawn = require('cross-spawn');
4445
var chalk = require('chalk');
4546
var semver = require('semver');
@@ -107,49 +108,33 @@ function createApp(name, verbose, version) {
107108
run(root, appName, version, verbose, originalDirectory);
108109
}
109110

111+
function shouldUseYarn() {
112+
try {
113+
execSync('yarn --version', {stdio: 'ignore'});
114+
return true;
115+
} catch (e) {
116+
return false;
117+
}
118+
}
119+
110120
function install(packageToInstall, verbose, callback) {
111-
function fallbackToNpm() {
112-
var npmArgs = [
113-
'install',
114-
verbose && '--verbose',
115-
'--save-dev',
116-
'--save-exact',
117-
packageToInstall,
118-
].filter(function(e) { return e; });
119-
var npmProc = spawn('npm', npmArgs, {stdio: 'inherit'});
120-
npmProc.on('close', function (code) {
121-
callback(code, 'npm', npmArgs);
122-
});
121+
var command;
122+
var args;
123+
if (shouldUseYarn()) {
124+
command = 'yarn';
125+
args = [ 'add', '--dev', '--exact', packageToInstall];
126+
} else {
127+
command = 'npm';
128+
args = ['install', '--save-dev', '--save-exact', packageToInstall];
123129
}
124130

125-
var yarnArgs = [
126-
'add',
127-
'--dev',
128-
'--exact',
129-
packageToInstall,
130-
];
131-
var yarnProc;
132-
var yarnExists = true;
133-
try {
134-
yarnProc = spawn('yarn', yarnArgs, {stdio: 'inherit'});
135-
} catch (err) {
136-
// It's not clear why we end up here in some cases but we need this.
137-
// https://github.com/facebookincubator/create-react-app/issues/1200
138-
yarnExists = false;
139-
fallbackToNpm();
140-
return;
131+
if (verbose) {
132+
args.push('--verbose');
141133
}
142-
yarnProc.on('error', function (err) {
143-
if (err.code === 'ENOENT') {
144-
yarnExists = false;
145-
}
146-
});
147-
yarnProc.on('close', function (code) {
148-
if (yarnExists) {
149-
callback(code, 'yarn', yarnArgs);
150-
} else {
151-
fallbackToNpm();
152-
}
134+
135+
var child = spawn(command, args, {stdio: 'inherit'});
136+
child.on('close', function(code) {
137+
callback(code, command, args);
153138
});
154139
}
155140

0 commit comments

Comments
 (0)