spaceout/front_end/node_modules/webpack-cli/lib/utils/get-package-manager.js
2020-12-12 10:25:30 -05:00

34 lines
850 B
JavaScript

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;