|
40 | 40 |
|
41 | 41 | var fs = require('fs'); |
42 | 42 | var path = require('path'); |
| 43 | +var execSync = require('child_process').execSync; |
43 | 44 | var spawn = require('cross-spawn'); |
44 | 45 | var chalk = require('chalk'); |
45 | 46 | var semver = require('semver'); |
@@ -107,49 +108,33 @@ function createApp(name, verbose, version) { |
107 | 108 | run(root, appName, version, verbose, originalDirectory); |
108 | 109 | } |
109 | 110 |
|
| 111 | +function shouldUseYarn() { |
| 112 | + try { |
| 113 | + execSync('yarn --version', {stdio: 'ignore'}); |
| 114 | + return true; |
| 115 | + } catch (e) { |
| 116 | + return false; |
| 117 | + } |
| 118 | +} |
| 119 | + |
110 | 120 | 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]; |
123 | 129 | } |
124 | 130 |
|
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'); |
141 | 133 | } |
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); |
153 | 138 | }); |
154 | 139 | } |
155 | 140 |
|
|
0 commit comments