package-manager-manager v0.0.0-14a4d01
package-manager-manager is a library aimed at providing information regarding the package manager currently being used in a given project.
It can be used in CLIs or similar projects which may at some point need to know and adapt their behavior based on the package manager currently used by the developer (e.g. A project scaffolding tool, a bundling tool, etc...).
Usage
To use the library first install it in your project, via:
npm i package-manager-manager(or your package manager's equivalent)
Then simply import and use the getPackageManager() function to get an object containing all the information you need regarding the package manager currently being used:
const packageManager = await getPackageManager();
console.log(packageManager.name);
// logs 'npm', 'yarn', 'pnpm' or 'bun'
console.log(packageManager.version);
// logs the version of the package manager e.g. '8.11.0'Note This library comes with properly defined and documented typescript types, meaning that once you obtain the
PackageManagerobject you will be able to easily see what's available on it and get all necessary details directly in your IDE
API
getPackageInfo
packageManager.getPackageInfo allows you to get the information regarding a locally installed package that your client application is using, it can for example be used to make sure your user's application has a certain dependency or to gather and display the package version of such dependency.
Example
const zodPackage = await packageManager.getPackageInfo('zod');
if (zodPackage) {
console.log(`starting validation using zod (version: ${zodPackage.version}`);
} else {
throw new Error('Error: zod is not installed');
}Note This method only returns the information of a locally installed package, or null in case the package is not installed, it does not return information of packages not locally installed (the API could be extended in the future to also include such use case)
getRunScript
packageManager.getRunScript let's you create a command that can be used to run a script defined in the package.json file.
For more advanced use cases you can use
packageManager.getRunScriptStructto obtain an object representing the command and extract whatever information you need from that
Example
const runBuildCommand = packageManager.getRunScript('build', {
args: ['./dist', '--verbose'],
});
exec(runBuildCommand);getRunExec
packageManager.getRunExec let's you create a command that can be used to execute a command from a target package (which may or may not be locally installed).
For more advanced use cases you can use
packageManager.getRunExecStructto obtain an object representing the command and extract whatever information you need from that
Example
const eslintCommand = packageManager.getRunExec('eslint', {
args: ['./src', '--quiet'],
});
exec(eslintCommand);2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago