0.2.0 • Published 7 months ago

package-manager-manager v0.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago


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 PackageManager object 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.

For 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 example:

const buildStr = packageManager.getRunScript('build', {
	args: ['./dist', '--verbose'],
});

console.log(`To build your application run: ${buildStr}`);

If you need more fine grained control over the command you can use its packageManager.getRunScriptStruct alternative to obtain an object representing the command.

For example:

import { spawn } from 'child_process';

const buildCmd = packageManager.getRunScriptStruct('build', {
	args: ['./dist', '--verbose'],
});

// run the command for the user
spawn(buildCmd.cmd, buildCmd.cmdArgs);

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 example:

const eslintStr = packageManager.getRunExec('eslint', {
	args: ['./src', '--quiet'],
});

console.log(`To run eslint on your application run: ${eslintStr}`);

If you need more fine grained control over the command you can use its packageManager.getRunExecStruct alternative to obtain an object representing the command.

For example:

import { spawn } from 'child_process';

const eslintCmd = packageManager.getRunExec('eslint', {
	args: ['./src', '--quiet'],
});

// run the command for the user
spawn(eslintCmd.cmd, eslintCmd.cmdArgs);
0.1.2-4d4bfc6

7 months ago

0.1.1-55062d6

8 months ago

0.1.2-4f1e0d2

7 months ago

0.1.0-a59865b

8 months ago

0.1.2-7e6d0b9

7 months ago

0.1.2-35550fa

7 months ago

0.1.3-69fef32

7 months ago

0.1.3-f6ce4f4

7 months ago

0.1.1-1cfd3b9

7 months ago

0.1.3-fff2bad

7 months ago

0.1.2

7 months ago

0.2.0

7 months ago

0.1.3-ff58791

7 months ago

0.1.1

8 months ago

0.2.0-8b500aa

7 months ago

0.1.3

7 months ago

0.1.2-5a72851

7 months ago

0.0.0-cf240f2

8 months ago

0.0.0-1f2f2a2

8 months ago

0.0.0-3729595

8 months ago

0.0.0-e2bad7c

8 months ago

0.0.0-1dd7c4b

8 months ago

0.1.0-97c9c9f

8 months ago

0.1.0

8 months ago

0.0.0-853c0ac

8 months ago

0.0.0-14a4d01

8 months ago

0.0.0-aee8fc2

9 months ago

0.0.0-e5622c1

9 months ago

0.0.0-50f332b

9 months ago

0.0.0-ed506ff

9 months ago

0.0.0-68b2d0f

9 months ago

0.0.0-5141dc4

9 months ago

0.0.0-d6c232c

9 months ago

0.0.0-b6a2638

9 months ago

0.0.0-b8d9033

9 months ago

0.0.0-772acd4

9 months ago

0.0.0-26c5ff0

9 months ago

0.0.0

9 months ago

0.0.0-18af871

9 months ago

0.0.0-9cc89d0

9 months ago