mirror of
https://github.com/Rushilwiz/spaceout.git
synced 2025-04-22 20:39:50 -04:00
34 lines
850 B
JavaScript
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;
|