forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yarn.js
59 lines (55 loc) · 1.8 KB
/
yarn.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* 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 execSync = require('child_process').execSync;
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,
};