-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: **Motivation** If the project is using yarn to manage its dependencies, we should be running 'yarn add' to upgrade the React Native dependency, rather than 'npm install'. **Test plan (required)** Running in a project that's in a bad state: Error: react-native version in "package.json" (0.29.2) doesn't match the installed version in "node_modules" (0.38.0). Try running "yarn" to fix this. Removed yarn.lock file, ran again: Error: react-native version in "package.json" (0.29.2) doesn't match the installed version in "node_modules" (0.38.0). Try running "npm install" to fix this. Running inside a folder that doesn't contain a `package.json` file: Error: Unable to find "/Users/mkonicek/Zero29App/package.json" or "/Users/mkonicek/Zero29App/node_modules/react-native/package.json". Make sure you ran "yarn" and that you are inside a React Native project. Removed yarn.lock file, ran again: Error: Unable to find "/Users/mkonicek/Zero29App/package.json" or "/Users/ Closes #11225 Differential Revision: D4261102 Pulled By: mkonicek fbshipit-source-id: b44ae91fe46f2c81b14616ca2868cc171692c895
- Loading branch information
Martin Konicek
committed
Dec 12, 2016
1 parent
70d4023
commit ca403f0
Showing
4 changed files
with
121 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
const semver = require('semver'); | ||
|
||
/** | ||
* 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; | ||
} | ||
} | ||
|
||
/** | ||
* Check that 'react-native init' itself used yarn to install React Native. | ||
* When using an old global [email protected] (or older), we don't want | ||
* to install React Native with npm, and React + Jest with yarn. | ||
* Let's be safe and not mix yarn and npm in a single project. | ||
* @param projectDir e.g. /Users/martin/AwesomeApp | ||
*/ | ||
function isProjectUsingYarn(projectDir) { | ||
return fs.existsSync(path.join(projectDir, 'yarn.lock')); | ||
} | ||
|
||
module.exports = { | ||
getYarnVersionIfAvailable: getYarnVersionIfAvailable, | ||
isProjectUsingYarn: isProjectUsingYarn, | ||
}; |