You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

35 lines
859 B

const fs = require('fs');
const path = require('path');
const { sync } = require('execa');
/**
*
* Returns the name of package manager to use,
* preferring yarn over npm if available
*
* @returns {String} - The package manager name
*/
function getPackageManager() {
const hasLocalYarn = fs.existsSync(path.resolve(process.cwd(), 'yarn.lock'));
const hasLocalNpm = fs.existsSync(path.resolve(process.cwd(), 'package-lock.json'));
if (hasLocalYarn) {
return 'yarn';
} else if (hasLocalNpm) {
return 'npm';
}
try {
// if the sync function below fails because yarn is not installed,
// an error will be thrown
if (sync('yarn', ['--version'])) {
return 'yarn';
}
} catch (e) {
// Nothing
}
return 'npm';
}
module.exports = {
getPackageManager,
};