2016-12-02 08:04:37 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2018-05-11 20:32:37 +00:00
|
|
|
*
|
|
|
|
* @format
|
2016-12-02 08:04:37 +00:00
|
|
|
*/
|
2018-05-11 20:32:37 +00:00
|
|
|
|
2016-12-02 08:04:37 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-12-05 13:55:39 +00:00
|
|
|
const execSync = require('child_process').execSync;
|
2016-12-02 08:04:37 +00:00
|
|
|
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 {
|
2018-05-11 20:32:37 +00:00
|
|
|
yarnVersion = (
|
|
|
|
execSync('yarn --version 2>/dev/null').toString() || ''
|
|
|
|
).trim();
|
2016-12-02 08:04:37 +00:00
|
|
|
}
|
|
|
|
} 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 react-native-cli@1.0.0 (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,
|
|
|
|
};
|