@skarab/detect-package-manager
Version:
Detects which package manager (bun, pnpm, yarn, npm) is used based on the current working directory.
38 lines (37 loc) • 1.15 kB
JavaScript
import { execAsync } from './process.js';
export const validVersionPattern = /^\d+\.\d+\.\d+$/;
/**
* Returns the version number for a given command name.
*
* @example
* ```
* const pnpmVersion = await getCommandVersion('pnpm');
* // -> '7.6.0'
* ```
*
* @param commandName command name such as 'pnpm'.
* @throws {Error} if the version is not valid or the command does not exist.
*/
export async function getCommandVersion(commandName) {
const childProcess = await execAsync(`${commandName} --version`);
const version = childProcess.stdout.trim().replace(/^v/i, '');
if (!validVersionPattern.test(version)) {
throw new Error(`Unknown command version from "${commandName} --version".`);
}
return version;
}
/**
* Returns the version number for a given command name or undefined if the version is not valid or the command does not exist.
*
* @see {@link getCommandVersion}
*
* @param commandName command name such as 'pnpm'.
*/
export async function tryGetCommandVersion(commandName) {
try {
return await getCommandVersion(commandName);
}
catch (_a) {
return undefined;
}
}