| 161 | } |
| 162 | |
| 163 | function install(root, useYarn, dependencies, verbose, isOnline, isDevDependencies) { |
| 164 | return new Promise((resolve, reject) => { |
| 165 | let command; |
| 166 | let args; |
| 167 | if (useYarn) { |
| 168 | command = 'yarnpkg'; |
| 169 | //args = ['add', '--exact'] |
| 170 | args = ['add']; |
| 171 | if (!isOnline) { |
| 172 | args.push('--offline'); |
| 173 | } |
| 174 | if (isDevDependencies) { |
| 175 | args.push('--dev'); |
| 176 | } |
| 177 | [].push.apply(args, dependencies); |
| 178 | |
| 179 | // Explicitly set cwd() to work around issues like |
| 180 | // https://github.com/facebook/create-react-app/issues/3326. |
| 181 | // Unfortunately we can only do this for Yarn because npm support for |
| 182 | // equivalent --prefix flag doesn't help with this issue. |
| 183 | // This is why for npm, we run checkThatNpmCanReadCwd() early instead. |
| 184 | args.push('--cwd'); |
| 185 | args.push(root); |
| 186 | |
| 187 | if (!isOnline) { |
| 188 | console.log(chalk.yellow('You appear to be offline.')); |
| 189 | console.log(chalk.yellow('Falling back to the local Yarn cache.')); |
| 190 | console.log(); |
| 191 | } |
| 192 | } else { |
| 193 | command = 'npm'; |
| 194 | args = ['install', '--loglevel', 'error']; |
| 195 | if (isDevDependencies) { |
| 196 | args.push('--save-dev'); |
| 197 | } else { |
| 198 | args.push('--save'); |
| 199 | } |
| 200 | |
| 201 | [].push.apply(args, dependencies); |
| 202 | } |
| 203 | |
| 204 | if (verbose) { |
| 205 | args.push('--verbose'); |
| 206 | } |
| 207 | |
| 208 | const child = spawn(command, args, { stdio: 'inherit' }); |
| 209 | child.on('close', (code) => { |
| 210 | if (code !== 0) { |
| 211 | reject({ |
| 212 | command: `${command} ${args.join(' ')}`, |
| 213 | }); |
| 214 | return; |
| 215 | } |
| 216 | resolve(); |
| 217 | }); |
| 218 | }); |
| 219 | } |
| 220 | |