-
Notifications
You must be signed in to change notification settings - Fork 25k
[cli] Use yarn when available #10626
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
By analyzing the blame information on this pull request, we identified @kentaromiura and @bestander to be potential reviewers. |
bestander
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few WIP comments :)
Thanks for taking this one.
local-cli/generator/index.js
Outdated
|
|
||
| function isYarnInstalled(boolCallback) { | ||
| exec('yarn --version', function (error) { | ||
| boolCallback(!e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo here !error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup 😨
local-cli/generator/index.js
Outdated
| this.npmInstall(`react@${reactVersion}`, { '--save': true, '--save-exact': true }); | ||
| isYarnInstalled(function(hasYarn) { | ||
| if (hasYarn) { | ||
| exec(`yarn add react@${reactVersion} --exact`, function(err, stdout, stderr) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
--exact is not needed for Yarn react@${reactVersion} will do the trick
local-cli/generator/index.js
Outdated
| this.npmInstall(`jest babel-jest jest-react-native babel-preset-react-native react-test-renderer@${reactVersion}`.split(' '), { | ||
| saveDev: true, | ||
| '--save-exact': true | ||
| isYarnInstalled(function(hasYarn) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should run isYarnInstalled once and with execSync in the beginning of the script, it will be easier then
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point!
react-native-cli/index.js
Outdated
| isYarnInstalled(function (hasYarn) { | ||
| var installCommand; | ||
| if (hasYarn) { | ||
| installCommand = 'yarn add ' + getInstallPackage(rnPackage) + ' --exact'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here about --exact.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getInstallPackage() can return "react-native" so better to keep --exact I think?
|
Thanks a lot for the review Konstantin! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey Martin, the code looks fine, just a few nits.
But considering that you are going through the trouble of making it work across systems and fallback to NPM what do you think of bundling yarn.js into CLI instead?
Then no npm code is needed and we can be sure yarn is always available
local-cli/generator/index.js
Outdated
| let yarnVersion; | ||
| try { | ||
| if (process.platform.startsWith('win')) { | ||
| // TODO implement on Windows |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Plan to implement this? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, I don't have a Windows machine. A contributor should implement this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
http://stackoverflow.com/questions/4507312/how-to-redirect-stderr-to-null-in-cmd-exe
Thanks to @kentaromiura
What do you think of doing it in one go maybe?
| // yarn < 0.16 has a 'missing manifest' bug | ||
| try { | ||
| if (semver.gte(yarnVersion, '0.16.0')) { | ||
| return yarnVersion; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be better to merge 2 try-catch blocks for readability?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also considering all the trouble...
What if we just ship yarn 0.16 with the CLI?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bestander I don't love bundling Yarn because react-native-cli is supposed to not change very often and we put the changes in react-native/local-cli instead. As Yarn 0.17 and 0.18 etc. are released, we'll have to tell everyone to update react-native-cli too.
Also because of differences in the lockfile format and installation behavior between Yarn versions, it could be confusing if you already have Yarn installed but "rn init" creates a lockfile or node_modules that's different than what you're used to.
I suppose we could include yarn@^0.16 with r-n-cli and use it if the user doesn't have Yarn installed, but I do think it's good to first look for the user's own copy of Yarn. To me the question is: is it better to use [user's own copy of Yarn -> fallback to included Yarn 0.16] or is it better to use [user's own copy of Yarn -> fallback to user's own copy of npm]? There are good arguments to be made for both sides, on one hand we could focus on supporting just Yarn (especially for new projects), on the other using the user's own package manager might be less surprising.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point, @ide .
In that case I agree, let's proceed as Martin started
react-native-cli/index.js
Outdated
|
|
||
| // 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 yarnVersionIfAvailable() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This really asks for being extracted into a separate file.
It is rather complex and has hardcoded versions
local-cli/generator/index.js
Outdated
|
|
||
| // 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 yarnVersionIfAvailable() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yarnVersionIfAvailable -> getYarnVersionIfAvailable
|
Fixed nits. The function |
|
@bestander has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator. |
| let yarnVersion; | ||
| try { | ||
| // execSync returns a Buffer -> convert to string | ||
| if (process.platform.startsWith('win')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be process.platform === 'win32'. It's always win32, even on 64-bit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to know, thanks! Wanted to be safe in case some future node versions start returning 'win64' or something else, even though they shouldn't as that would be a breaking change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will never change. win32 is the identifier for the Windows platform, not the architecture.
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 #10626 Differential Revision: D4110883 Pulled By: bestander fbshipit-source-id: 8a3427e2bc9158cf5fadd8ddf5b31e4b50ce6453
|
|
|
No verbose mode in Yarn On Friday, 4 November 2016, Siddharth Jain [email protected] wrote:
|
|
Is this resolved? I'm using react-native for the first time, and running "react-native init [myproject]" and getting this error: I tried installing yarn via npm but still get the error. |
|
Ah the above appears to be a result of this issue: So it shouldn't be an issue with react-native itself. |
|
Yeah, Yarn does not support node < 4.0 On 10 November 2016 at 12:58, Katharine Osborne [email protected]
|
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 facebook#10626 Differential Revision: D4110883 Pulled By: bestander fbshipit-source-id: 8a3427e2bc9158cf5fadd8ddf5b31e4b50ce6453
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 facebook/react-native#10626 Differential Revision: D4110883 Pulled By: bestander fbshipit-source-id: 8a3427e2bc9158cf5fadd8ddf5b31e4b50ce6453
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 facebook/react-native#10626 Differential Revision: D4110883 Pulled By: bestander fbshipit-source-id: 8a3427e2bc9158cf5fadd8ddf5b31e4b50ce6453
Motivation
react-native inittakes minutes even on a fast network. Yarn makes it much quicker.When yarn is not installed on the system:
When yarn is installed:
Also added the option
react-native init AwesomeApp --npmas 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
react-native init AwesomeAppTested the following setups
NOTE: Yarn 14 failed with a missing manifest error: yarnpkg/yarn#454. Upgrading to Yarn 0.16.1 fixed it. To be safe let's check yarn version is at least 16.
Output (total time 45s):
The node DeprecationWarnings might be because I'm using node v7.0.0.
Running the generated app on iOS (reload JS works):