Skip to content

Conversation

@mkonicek
Copy link
Contributor

@mkonicek mkonicek commented Oct 29, 2016

Motivation

react-native init takes minutes even on a fast network. Yarn makes it much quicker.

When yarn is not installed on the system:

screenshot 2016-10-31 22 21 19

When yarn is installed:

screenshot 2016-10-31 22 02 20

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
  • New CLI - uses yarn, react-native 0.36.0 - doesn't use yarn

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):

$ react-native init ytest
This will walk you through creating a new React Native project in /Users/mkonicek/code/testapps/ytest
Using yarn v0.16.1
Installing react-native...
Setting up new React Native app in /Users/mkonicek/code/testapps/ytest
(node:79575) DeprecationWarning: os.tmpDir() is deprecated. Use os.tmpdir() instead.
(node:79575) DeprecationWarning: Using Buffer without `new` will soon stop working. Use `new Buffer()`, or preferably `Buffer.from()`, `Buffer.allocUnsafe()` or `Buffer.alloc()` instead.
Installing React...
Installing Jest...
warning jest > jest-cli > istanbul-api > fileset > [email protected]: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
warning Unmet peer dependency "whatwg-fetch@^1.0.0".
To run your app on iOS:
   cd /Users/mkonicek/code/testapps/ytest
   react-native run-ios
   - or -
   Open /Users/mkonicek/code/testapps/ytest/ios/ytest.xcodeproj in Xcode
   Hit the Run button
To run your app on Android:
   Have an Android emulator running (quickest way to get started), or a device connected
   cd /Users/mkonicek/code/testapps/ytest
   react-native run-android

The node DeprecationWarnings might be because I'm using node v7.0.0.

Running the generated app on iOS (reload JS works):

screenshot 2016-10-29 17 04 37

@facebook-github-bot
Copy link
Contributor

By analyzing the blame information on this pull request, we identified @kentaromiura and @bestander to be potential reviewers.

Copy link
Contributor

@bestander bestander left a 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.


function isYarnInstalled(boolCallback) {
exec('yarn --version', function (error) {
boolCallback(!e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo here !error?

Copy link
Contributor Author

@mkonicek mkonicek Oct 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup 😨

this.npmInstall(`react@${reactVersion}`, { '--save': true, '--save-exact': true });
isYarnInstalled(function(hasYarn) {
if (hasYarn) {
exec(`yarn add react@${reactVersion} --exact`, function(err, stdout, stderr) {
Copy link
Contributor

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

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) {
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point!

isYarnInstalled(function (hasYarn) {
var installCommand;
if (hasYarn) {
installCommand = 'yarn add ' + getInstallPackage(rnPackage) + ' --exact';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here about --exact.

Copy link
Contributor Author

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?

@mkonicek
Copy link
Contributor Author

Thanks a lot for the review Konstantin!

Copy link
Contributor

@bestander bestander left a 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

let yarnVersion;
try {
if (process.platform.startsWith('win')) {
// TODO implement on Windows
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plan to implement this? :)

Copy link
Contributor Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// yarn < 0.16 has a 'missing manifest' bug
try {
if (semver.gte(yarnVersion, '0.16.0')) {
return yarnVersion;
Copy link
Contributor

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?

Copy link
Contributor

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?

Copy link
Contributor

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.

Copy link
Contributor

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


// 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() {
Copy link
Contributor

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


// 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() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yarnVersionIfAvailable -> getYarnVersionIfAvailable

@mkonicek
Copy link
Contributor Author

mkonicek commented Nov 1, 2016

Fixed nits. The function getYarnVersionIfAvailable is duplicated because the files are in two separate npm packages. We could try a prepublish script to copy the file over from one to another but duplicating this bit of code seems simpler and easier to understand..

@facebook-github-bot
Copy link
Contributor

@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')) {

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.

Copy link
Contributor Author

@mkonicek mkonicek Nov 1, 2016

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.

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.

mkonicek pushed a commit that referenced this pull request Nov 3, 2016
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
@f0rr0
Copy link

f0rr0 commented Nov 4, 2016

--verbose forces npm. Is there a verbose version for yarn?

@bestander
Copy link
Contributor

No verbose mode in Yarn

On Friday, 4 November 2016, Siddharth Jain [email protected] wrote:

--verbose forces npm. Is there a verbose version for yarn?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#10626 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/ACBdWAHaaSl8o4uLJD5YV6BFkwAtswLIks5q6oHvgaJpZM4KkI8T
.

@hawaiikaos
Copy link

Is this resolved?

I'm using react-native for the first time, and running "react-native init [myproject]" and getting this error:

/[...]/.node/lib/node_modules/react-native-cli/index.js:81
  let yarnVersion;
  ^^^
SyntaxError: Unexpected strict mode reserved word
    at exports.runInThisContext (vm.js:73:16)
    at Module._compile (module.js:443:25)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

I tried installing yarn via npm but still get the error.

@hawaiikaos
Copy link

Ah the above appears to be a result of this issue:

yarnpkg/yarn#1288

So it shouldn't be an issue with react-native itself.

@bestander
Copy link
Contributor

Yeah, Yarn does not support node < 4.0

On 10 November 2016 at 12:58, Katharine Osborne [email protected]
wrote:

Ah the above appears to be a result of this issue:

yarnpkg/yarn#1288 yarnpkg/yarn#1288

So it shouldn't be an issue with react-native itself.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#10626 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/ACBdWP7mSFM9OwU6dLM_pcB-0WWMRW2mks5q8xT0gaJpZM4KkI8T
.

@hramos hramos added the Import Started This pull request has been imported. This does not imply the PR has been approved. label Nov 11, 2016
DanielMSchmidt pushed a commit to DanielMSchmidt/react-native that referenced this pull request Jan 4, 2017
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
grabbou pushed a commit to react-native-community/cli that referenced this pull request Sep 26, 2018
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
grabbou pushed a commit to react-native-community/cli that referenced this pull request Sep 28, 2018
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Import Started This pull request has been imported. This does not imply the PR has been approved.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants