@js-utils-kit/pm 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Sriman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.cjs ADDED
@@ -0,0 +1 @@
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require(`node:fs/promises`),t=require(`node:path`),n=require(`@js-utils-kit/fs`);const r=[`npm`,`pnpm`,`yarn`,`bun`];async function i({cwd:i=process.cwd(),lockfile:a=!0,packageJson:o=!0}={}){let s,c=process.env.npm_config_user_agent;if(c?.startsWith(`pnpm`)?s=`pnpm`:c?.startsWith(`yarn`)?s=`yarn`:c?.startsWith(`bun`)?s=`bun`:c?.startsWith(`npm`)&&(s=`npm`),a){let[e,r,a,o,c]=await Promise.all([(0,n.exists)((0,t.resolve)(i,`package-lock.json`)),(0,n.exists)((0,t.resolve)(i,`pnpm-lock.yaml`)),(0,n.exists)((0,t.resolve)(i,`yarn.lock`)),(0,n.exists)((0,t.resolve)(i,`bun.lock`)),(0,n.exists)((0,t.resolve)(i,`bun.lockb`))]);e?s=`npm`:a?s=`yarn`:r?s=`pnpm`:(o||c)&&(s=`bun`)}if(o)try{let n=JSON.parse(await(0,e.readFile)((0,t.resolve)(i,`package.json`),`utf-8`));if(n.packageManager){let e=n.packageManager.split(`@`)[0];r.includes(e)&&(s=e)}}catch{}return{name:s,isPackageManager:s!=null,isNpm:s===`npm`,isPnpm:s===`pnpm`,isYarn:s===`yarn`,isBun:s===`bun`}}exports.PACKAGE_MANAGERS=r,exports.detectPM=i;
@@ -0,0 +1,110 @@
1
+ //#region src/types.d.ts
2
+ type PackageManager = (typeof PACKAGE_MANAGERS)[number];
3
+ /**
4
+ * Options for {@link detectPM}
5
+ */
6
+ interface DetectPMOptions {
7
+ /**
8
+ * Working directory where package manager detection should occur.
9
+ *
10
+ * @default `process.cwd()`.
11
+ */
12
+ cwd?: string;
13
+ /**
14
+ * Whether to detect the package manager using lockfiles.
15
+ *
16
+ * Supported lockfiles:
17
+ * - `package-lock.json` → npm
18
+ * - `pnpm-lock.yaml` → pnpm
19
+ * - `yarn.lock` → yarn
20
+ * - `bun.lock` or `bun.lockb` → bun
21
+ *
22
+ * @default true
23
+ */
24
+ lockfile?: boolean;
25
+ /**
26
+ * Whether to inspect `package.json` for the `packageManager` field.
27
+ *
28
+ * Example:
29
+ * ```json
30
+ * {
31
+ * "packageManager": "pnpm@10.30.3"
32
+ * }
33
+ * ```
34
+ *
35
+ * @default true
36
+ */
37
+ packageJson?: boolean;
38
+ }
39
+ /**
40
+ * Result returned by {@link detectPM}
41
+ */
42
+ interface DetectPMResult {
43
+ /**
44
+ * Detected package manager name.
45
+ *
46
+ * `undefined` if no package manager could be determined.
47
+ */
48
+ name?: PackageManager;
49
+ /**
50
+ * Indicates whether any package manager was detected.
51
+ */
52
+ isPackageManager: boolean;
53
+ /** True if npm is detected */
54
+ isNpm: boolean;
55
+ /** True if pnpm is detected */
56
+ isPnpm: boolean;
57
+ /** True if yarn is detected */
58
+ isYarn: boolean;
59
+ /** True if bun is detected */
60
+ isBun: boolean;
61
+ }
62
+ //#endregion
63
+ //#region src/pm.d.ts
64
+ /** List of JavaScript package managers */
65
+ declare const PACKAGE_MANAGERS: readonly ["npm", "pnpm", "yarn", "bun"];
66
+ /**
67
+ * Detect the package manager used in a project directory.
68
+ *
69
+ * @remarks
70
+ *
71
+ * Detection strategy:
72
+ *
73
+ * 1. `npm_config_user_agent` environment variable
74
+ * 2. Lockfiles (`package-lock.json`, `pnpm-lock.yaml`, etc.)
75
+ * 3. `package.json` → `packageManager` field
76
+ *
77
+ * Each later step can override earlier detection.
78
+ *
79
+ *
80
+ * @example
81
+ * ```ts
82
+ * const pm = await detectPM();
83
+ *
84
+ * console.log(pm.name);
85
+ * // "pnpm"
86
+ *
87
+ * if (pm.isPnpm) {
88
+ * console.log('Using pnpm');
89
+ * }
90
+ * ```
91
+ *
92
+ * @example Disable lockfile detection
93
+ *
94
+ * ```ts
95
+ * await detectPM({ lockfile: false });
96
+ * ```
97
+ *
98
+ * @example Disable packageJson detection
99
+ *
100
+ * ```ts
101
+ * await detectPM({ packageJson: false });
102
+ * ```
103
+ */
104
+ declare function detectPM({
105
+ cwd,
106
+ lockfile,
107
+ packageJson
108
+ }?: DetectPMOptions): Promise<DetectPMResult>;
109
+ //#endregion
110
+ export { DetectPMOptions, DetectPMResult, PACKAGE_MANAGERS, PackageManager, detectPM };
@@ -0,0 +1,110 @@
1
+ //#region src/types.d.ts
2
+ type PackageManager = (typeof PACKAGE_MANAGERS)[number];
3
+ /**
4
+ * Options for {@link detectPM}
5
+ */
6
+ interface DetectPMOptions {
7
+ /**
8
+ * Working directory where package manager detection should occur.
9
+ *
10
+ * @default `process.cwd()`.
11
+ */
12
+ cwd?: string;
13
+ /**
14
+ * Whether to detect the package manager using lockfiles.
15
+ *
16
+ * Supported lockfiles:
17
+ * - `package-lock.json` → npm
18
+ * - `pnpm-lock.yaml` → pnpm
19
+ * - `yarn.lock` → yarn
20
+ * - `bun.lock` or `bun.lockb` → bun
21
+ *
22
+ * @default true
23
+ */
24
+ lockfile?: boolean;
25
+ /**
26
+ * Whether to inspect `package.json` for the `packageManager` field.
27
+ *
28
+ * Example:
29
+ * ```json
30
+ * {
31
+ * "packageManager": "pnpm@10.30.3"
32
+ * }
33
+ * ```
34
+ *
35
+ * @default true
36
+ */
37
+ packageJson?: boolean;
38
+ }
39
+ /**
40
+ * Result returned by {@link detectPM}
41
+ */
42
+ interface DetectPMResult {
43
+ /**
44
+ * Detected package manager name.
45
+ *
46
+ * `undefined` if no package manager could be determined.
47
+ */
48
+ name?: PackageManager;
49
+ /**
50
+ * Indicates whether any package manager was detected.
51
+ */
52
+ isPackageManager: boolean;
53
+ /** True if npm is detected */
54
+ isNpm: boolean;
55
+ /** True if pnpm is detected */
56
+ isPnpm: boolean;
57
+ /** True if yarn is detected */
58
+ isYarn: boolean;
59
+ /** True if bun is detected */
60
+ isBun: boolean;
61
+ }
62
+ //#endregion
63
+ //#region src/pm.d.ts
64
+ /** List of JavaScript package managers */
65
+ declare const PACKAGE_MANAGERS: readonly ["npm", "pnpm", "yarn", "bun"];
66
+ /**
67
+ * Detect the package manager used in a project directory.
68
+ *
69
+ * @remarks
70
+ *
71
+ * Detection strategy:
72
+ *
73
+ * 1. `npm_config_user_agent` environment variable
74
+ * 2. Lockfiles (`package-lock.json`, `pnpm-lock.yaml`, etc.)
75
+ * 3. `package.json` → `packageManager` field
76
+ *
77
+ * Each later step can override earlier detection.
78
+ *
79
+ *
80
+ * @example
81
+ * ```ts
82
+ * const pm = await detectPM();
83
+ *
84
+ * console.log(pm.name);
85
+ * // "pnpm"
86
+ *
87
+ * if (pm.isPnpm) {
88
+ * console.log('Using pnpm');
89
+ * }
90
+ * ```
91
+ *
92
+ * @example Disable lockfile detection
93
+ *
94
+ * ```ts
95
+ * await detectPM({ lockfile: false });
96
+ * ```
97
+ *
98
+ * @example Disable packageJson detection
99
+ *
100
+ * ```ts
101
+ * await detectPM({ packageJson: false });
102
+ * ```
103
+ */
104
+ declare function detectPM({
105
+ cwd,
106
+ lockfile,
107
+ packageJson
108
+ }?: DetectPMOptions): Promise<DetectPMResult>;
109
+ //#endregion
110
+ export { DetectPMOptions, DetectPMResult, PACKAGE_MANAGERS, PackageManager, detectPM };
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ import{readFile as e}from"node:fs/promises";import{resolve as t}from"node:path";import{exists as n}from"@js-utils-kit/fs";const r=[`npm`,`pnpm`,`yarn`,`bun`];async function i({cwd:i=process.cwd(),lockfile:a=!0,packageJson:o=!0}={}){let s,c=process.env.npm_config_user_agent;if(c?.startsWith(`pnpm`)?s=`pnpm`:c?.startsWith(`yarn`)?s=`yarn`:c?.startsWith(`bun`)?s=`bun`:c?.startsWith(`npm`)&&(s=`npm`),a){let[e,r,a,o,c]=await Promise.all([n(t(i,`package-lock.json`)),n(t(i,`pnpm-lock.yaml`)),n(t(i,`yarn.lock`)),n(t(i,`bun.lock`)),n(t(i,`bun.lockb`))]);e?s=`npm`:a?s=`yarn`:r?s=`pnpm`:(o||c)&&(s=`bun`)}if(o)try{let n=JSON.parse(await e(t(i,`package.json`),`utf-8`));if(n.packageManager){let e=n.packageManager.split(`@`)[0];r.includes(e)&&(s=e)}}catch{}return{name:s,isPackageManager:s!=null,isNpm:s===`npm`,isPnpm:s===`pnpm`,isYarn:s===`yarn`,isBun:s===`bun`}}export{r as PACKAGE_MANAGERS,i as detectPM};
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@js-utils-kit/pm",
3
+ "version": "0.1.0",
4
+ "description": "Utilities for detecting and interacting with JavaScript package managers",
5
+ "homepage": "https://js-utils.js.org",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/teneplaysofficial/js-utils-kit",
9
+ "directory": "packages/@js-utils-kit/pm"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/teneplaysofficial/js-utils-kit/issues"
13
+ },
14
+ "license": "MIT",
15
+ "author": {
16
+ "name": "Sriman",
17
+ "email": "136729116+TenEplaysOfficial@users.noreply.github.com",
18
+ "url": "https://tene.vercel.app"
19
+ },
20
+ "private": false,
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "type": "module",
25
+ "main": "./dist/index.cjs",
26
+ "module": "./dist/index.mjs",
27
+ "exports": {
28
+ ".": {
29
+ "import": "./dist/index.mjs",
30
+ "require": "./dist/index.cjs"
31
+ }
32
+ },
33
+ "types": "./dist/index.d.cts",
34
+ "engines": {
35
+ "node": ">=22"
36
+ },
37
+ "dependencies": {
38
+ "@js-utils-kit/fs": "1.5.0",
39
+ "@js-utils-kit/types": "1.4.0"
40
+ },
41
+ "scripts": {
42
+ "build": "tsdown",
43
+ "test": "vitest run"
44
+ }
45
+ }