mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Use yarn when available
Summary: **Motivation** `react-native init` takes minutes even on a fast network. Yarn makes it much quicker. When yarn is not installed on the system: <img width="440" alt="screenshot 2016-10-31 22 21 19" src="https://cloud.githubusercontent.com/assets/346214/19873897/7bad5662-9fb9-11e6-85fb-ad4879949dad.png"> When yarn is installed: <img width="441" alt="screenshot 2016-10-31 22 02 20" src="https://cloud.githubusercontent.com/assets/346214/19873898/7baf4c56-9fb9-11e6-96b3-007f93d2438a.png"> Also added the option `react-native init AwesomeApp --npm` as a fallback in case yarn is not stable enough for some people (I saw some Github issues that yarn hangs for example; for me it works great though). **Test plan** 1. Publish to Sinopia: https://github.com/facebook/react-native/tree/master/react-native-cli 2. `react-native init AwesomeApp` ***Tested the following setups*** - New CLI - uses yarn, new react-native - uses yarn - Old CLI (1.0.0) - doesn't use yarn, new react-native - uses yarn - Closes https://github.com/facebook/react-native/pull/10626 Differential Revision: D4110883 Pulled By: bestander fbshipit-source-id: 8a3427e2bc9158cf5fadd8ddf5b31e4b50ce6453
This commit is contained in:
@@ -8,10 +8,39 @@
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
var execSync = require('child_process').execSync;
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
var yeoman = require('yeoman-generator');
|
||||
var semver = require('semver')
|
||||
var utils = require('../generator-utils');
|
||||
var yeoman = require('yeoman-generator');
|
||||
|
||||
// Use Yarn if available, it's much faster than the npm client.
|
||||
// Return the version of yarn installed on the system, null if yarn is not available.
|
||||
function getYarnVersionIfAvailable() {
|
||||
let yarnVersion;
|
||||
try {
|
||||
// execSync returns a Buffer -> convert to string
|
||||
if (process.platform.startsWith('win')) {
|
||||
yarnVersion = (execSync('yarn --version').toString() || '').trim();
|
||||
} else {
|
||||
yarnVersion = (execSync('yarn --version 2>/dev/null').toString() || '').trim();
|
||||
}
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
// yarn < 0.16 has a 'missing manifest' bug
|
||||
try {
|
||||
if (semver.gte(yarnVersion, '0.16.0')) {
|
||||
return yarnVersion;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Cannot parse yarn version: ' + yarnVersion);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = yeoman.generators.NamedBase.extend({
|
||||
constructor: function() {
|
||||
@@ -36,6 +65,12 @@ module.exports = yeoman.generators.NamedBase.extend({
|
||||
type: Boolean,
|
||||
defaults: false
|
||||
});
|
||||
// Temporary option until yarn becomes stable.
|
||||
this.option('npm', {
|
||||
desc: 'Use the npm client, even if yarn is available.',
|
||||
type: Boolean,
|
||||
defaults: false
|
||||
});
|
||||
|
||||
// this passes command line arguments down to the composed generators
|
||||
var args = {args: arguments[0], options: this.options};
|
||||
@@ -112,12 +147,24 @@ module.exports = yeoman.generators.NamedBase.extend({
|
||||
return;
|
||||
}
|
||||
|
||||
this.npmInstall(`react@${reactVersion}`, { '--save': true, '--save-exact': true });
|
||||
const yarnVersion = (!this.options['npm']) && getYarnVersionIfAvailable();
|
||||
|
||||
console.log('Installing React...');
|
||||
if (yarnVersion) {
|
||||
execSync(`yarn add react@${reactVersion}`);
|
||||
} else {
|
||||
this.npmInstall(`react@${reactVersion}`, { '--save': true, '--save-exact': true });
|
||||
}
|
||||
if (!this.options['skip-jest']) {
|
||||
this.npmInstall(`jest babel-jest jest-react-native babel-preset-react-native react-test-renderer@${reactVersion}`.split(' '), {
|
||||
saveDev: true,
|
||||
'--save-exact': true
|
||||
});
|
||||
console.log('Installing Jest...');
|
||||
if (yarnVersion) {
|
||||
execSync(`yarn add jest babel-jest jest-react-native babel-preset-react-native react-test-renderer@${reactVersion} --dev --exact`);
|
||||
} else {
|
||||
this.npmInstall(`jest babel-jest jest-react-native babel-preset-react-native react-test-renderer@${reactVersion}`.split(' '), {
|
||||
saveDev: true,
|
||||
'--save-exact': true
|
||||
});
|
||||
}
|
||||
fs.writeFileSync(
|
||||
path.join(
|
||||
this.destinationRoot(),
|
||||
@@ -125,7 +172,6 @@ module.exports = yeoman.generators.NamedBase.extend({
|
||||
),
|
||||
'{\n"presets": ["react-native"]\n}'
|
||||
);
|
||||
|
||||
this.fs.copy(
|
||||
this.templatePath('__tests__'),
|
||||
this.destinationPath('__tests__'),
|
||||
@@ -133,7 +179,6 @@ module.exports = yeoman.generators.NamedBase.extend({
|
||||
nodir: false
|
||||
}
|
||||
);
|
||||
|
||||
var packageJSONPath = path.join(
|
||||
this.destinationRoot(),
|
||||
'package.json'
|
||||
|
||||
Reference in New Issue
Block a user