@addmo/pkg-manager 0.1.1-beta.1

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 addmo
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.
@@ -0,0 +1,164 @@
1
+ "use strict";
2
+ const __rslib_import_meta_url__ = /*#__PURE__*/ function() {
3
+ return "u" < typeof document ? new (require('url'.replace('', ''))).URL('file:' + __filename).href : document.currentScript && document.currentScript.src || new URL('main.js', document.baseURI).href;
4
+ }();
5
+ var __webpack_require__ = {};
6
+ (()=>{
7
+ __webpack_require__.d = (exports1, definition)=>{
8
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
9
+ enumerable: true,
10
+ get: definition[key]
11
+ });
12
+ };
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
16
+ })();
17
+ (()=>{
18
+ __webpack_require__.r = (exports1)=>{
19
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
20
+ value: 'Module'
21
+ });
22
+ Object.defineProperty(exports1, '__esModule', {
23
+ value: true
24
+ });
25
+ };
26
+ })();
27
+ var __webpack_exports__ = {};
28
+ __webpack_require__.r(__webpack_exports__);
29
+ __webpack_require__.d(__webpack_exports__, {
30
+ getAddCommand: ()=>getAddCommand,
31
+ detectFromLockfile: ()=>detectFromLockfile,
32
+ detectPackageManager: ()=>detectPackageManager,
33
+ getInstallCommand: ()=>getInstallCommand,
34
+ getRunCommand: ()=>getRunCommand,
35
+ getExecCommand: ()=>getExecCommand,
36
+ getMissingPackages: ()=>getMissingPackages,
37
+ isPackageInstalled: ()=>isPackageInstalled
38
+ });
39
+ const external_node_fs_namespaceObject = require("node:fs");
40
+ const external_node_path_namespaceObject = require("node:path");
41
+ const external_node_module_namespaceObject = require("node:module");
42
+ const PM_PATTERNS = [
43
+ {
44
+ prefix: "pnpm",
45
+ pm: "pnpm"
46
+ },
47
+ {
48
+ prefix: "yarn",
49
+ pm: "yarn"
50
+ },
51
+ {
52
+ prefix: "bun",
53
+ pm: "bun"
54
+ },
55
+ {
56
+ prefix: "npm",
57
+ pm: "npm"
58
+ }
59
+ ];
60
+ const LOCKFILE_MAP = [
61
+ {
62
+ file: "pnpm-lock.yaml",
63
+ pm: "pnpm"
64
+ },
65
+ {
66
+ file: "bun.lockb",
67
+ pm: "bun"
68
+ },
69
+ {
70
+ file: "yarn.lock",
71
+ pm: "yarn"
72
+ },
73
+ {
74
+ file: "package-lock.json",
75
+ pm: "npm"
76
+ }
77
+ ];
78
+ function detectPackageManager(userAgent = process.env.npm_config_user_agent) {
79
+ if (!userAgent) return "npm";
80
+ const lower = userAgent.toLowerCase();
81
+ for (const { prefix, pm } of PM_PATTERNS)if (lower.startsWith(prefix)) return pm;
82
+ return "npm";
83
+ }
84
+ function detectFromLockfile(root) {
85
+ for (const { file, pm } of LOCKFILE_MAP)if ((0, external_node_fs_namespaceObject.existsSync)((0, external_node_path_namespaceObject.resolve)(root, file))) return pm;
86
+ return "pnpm";
87
+ }
88
+ function isPackageInstalled(root, pkgName) {
89
+ const require1 = (0, external_node_module_namespaceObject.createRequire)(__rslib_import_meta_url__);
90
+ try {
91
+ require1.resolve(`${pkgName}/package.json`, {
92
+ paths: [
93
+ root
94
+ ]
95
+ });
96
+ return true;
97
+ } catch {
98
+ return false;
99
+ }
100
+ }
101
+ function getMissingPackages(root, pkgNames) {
102
+ const missing = [];
103
+ for (const name of pkgNames)if (!isPackageInstalled(root, name)) missing.push(name);
104
+ return missing;
105
+ }
106
+ const INSTALL_MAP = {
107
+ pnpm: "pnpm install",
108
+ npm: "npm install",
109
+ yarn: "yarn",
110
+ bun: "bun install"
111
+ };
112
+ const EXEC_MAP = {
113
+ pnpm: "pnpx",
114
+ npm: "npx",
115
+ yarn: "yarn dlx",
116
+ bun: "bunx"
117
+ };
118
+ function getInstallCommand(pm) {
119
+ return INSTALL_MAP[pm];
120
+ }
121
+ function getRunCommand(pm, script) {
122
+ if ("npm" === pm) return `npm run ${script}`;
123
+ return `${"yarn" === pm ? "yarn" : pm} ${script}`;
124
+ }
125
+ function getExecCommand(pm) {
126
+ return EXEC_MAP[pm];
127
+ }
128
+ function getAddCommand(pm, pkg, dev = false) {
129
+ const flag = dev ? ADD_DEV_FLAG[pm] : "";
130
+ return `${ADD_PREFIX[pm]} ${pkg}${flag ? ` ${flag}` : ""}`;
131
+ }
132
+ const ADD_PREFIX = {
133
+ pnpm: "pnpm add",
134
+ npm: "npm install",
135
+ yarn: "yarn add",
136
+ bun: "bun add"
137
+ };
138
+ const ADD_DEV_FLAG = {
139
+ pnpm: "-D",
140
+ npm: "-D",
141
+ yarn: "-D",
142
+ bun: "-D"
143
+ };
144
+ exports.detectFromLockfile = __webpack_exports__.detectFromLockfile;
145
+ exports.detectPackageManager = __webpack_exports__.detectPackageManager;
146
+ exports.getAddCommand = __webpack_exports__.getAddCommand;
147
+ exports.getExecCommand = __webpack_exports__.getExecCommand;
148
+ exports.getInstallCommand = __webpack_exports__.getInstallCommand;
149
+ exports.getMissingPackages = __webpack_exports__.getMissingPackages;
150
+ exports.getRunCommand = __webpack_exports__.getRunCommand;
151
+ exports.isPackageInstalled = __webpack_exports__.isPackageInstalled;
152
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
153
+ "detectFromLockfile",
154
+ "detectPackageManager",
155
+ "getAddCommand",
156
+ "getExecCommand",
157
+ "getInstallCommand",
158
+ "getMissingPackages",
159
+ "getRunCommand",
160
+ "isPackageInstalled"
161
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
162
+ Object.defineProperty(exports, '__esModule', {
163
+ value: true
164
+ });
@@ -0,0 +1,29 @@
1
+ export type PackageManager = "pnpm" | "npm" | "yarn" | "bun";
2
+ /**
3
+ * Detect the package manager from `npm_config_user_agent`
4
+ * (set automatically by pnpm/npm/yarn/bun when running scripts).
5
+ * Falls back to "npm" when the environment variable is absent.
6
+ */
7
+ export declare function detectPackageManager(userAgent?: string | undefined): PackageManager;
8
+ /**
9
+ * Detect the package manager from lockfile presence in a project directory.
10
+ * Falls back to "pnpm" when no lockfile is found.
11
+ */
12
+ export declare function detectFromLockfile(root: string): PackageManager;
13
+ /**
14
+ * Check whether a package is installed (resolvable from project root).
15
+ * Uses Node module resolution so hoisted (e.g. pnpm) deps are detected.
16
+ */
17
+ export declare function isPackageInstalled(root: string, pkgName: string): boolean;
18
+ /**
19
+ * Return which of the given package names are not installed.
20
+ */
21
+ export declare function getMissingPackages(root: string, pkgNames: readonly string[]): string[];
22
+ export declare function getInstallCommand(pm: PackageManager): string;
23
+ export declare function getRunCommand(pm: PackageManager, script: string): string;
24
+ export declare function getExecCommand(pm: PackageManager): string;
25
+ /**
26
+ * Returns the `add` command for installing a package.
27
+ * @param dev - whether to add as a dev dependency
28
+ */
29
+ export declare function getAddCommand(pm: PackageManager, pkg: string, dev?: boolean): string;
@@ -0,0 +1,106 @@
1
+ import { existsSync } from "node:fs";
2
+ import { resolve } from "node:path";
3
+ import { createRequire } from "node:module";
4
+ const PM_PATTERNS = [
5
+ {
6
+ prefix: "pnpm",
7
+ pm: "pnpm"
8
+ },
9
+ {
10
+ prefix: "yarn",
11
+ pm: "yarn"
12
+ },
13
+ {
14
+ prefix: "bun",
15
+ pm: "bun"
16
+ },
17
+ {
18
+ prefix: "npm",
19
+ pm: "npm"
20
+ }
21
+ ];
22
+ const LOCKFILE_MAP = [
23
+ {
24
+ file: "pnpm-lock.yaml",
25
+ pm: "pnpm"
26
+ },
27
+ {
28
+ file: "bun.lockb",
29
+ pm: "bun"
30
+ },
31
+ {
32
+ file: "yarn.lock",
33
+ pm: "yarn"
34
+ },
35
+ {
36
+ file: "package-lock.json",
37
+ pm: "npm"
38
+ }
39
+ ];
40
+ function detectPackageManager(userAgent = process.env.npm_config_user_agent) {
41
+ if (!userAgent) return "npm";
42
+ const lower = userAgent.toLowerCase();
43
+ for (const { prefix, pm } of PM_PATTERNS)if (lower.startsWith(prefix)) return pm;
44
+ return "npm";
45
+ }
46
+ function detectFromLockfile(root) {
47
+ for (const { file, pm } of LOCKFILE_MAP)if (existsSync(resolve(root, file))) return pm;
48
+ return "pnpm";
49
+ }
50
+ function isPackageInstalled(root, pkgName) {
51
+ const require = createRequire(import.meta.url);
52
+ try {
53
+ require.resolve(`${pkgName}/package.json`, {
54
+ paths: [
55
+ root
56
+ ]
57
+ });
58
+ return true;
59
+ } catch {
60
+ return false;
61
+ }
62
+ }
63
+ function getMissingPackages(root, pkgNames) {
64
+ const missing = [];
65
+ for (const name of pkgNames)if (!isPackageInstalled(root, name)) missing.push(name);
66
+ return missing;
67
+ }
68
+ const INSTALL_MAP = {
69
+ pnpm: "pnpm install",
70
+ npm: "npm install",
71
+ yarn: "yarn",
72
+ bun: "bun install"
73
+ };
74
+ const EXEC_MAP = {
75
+ pnpm: "pnpx",
76
+ npm: "npx",
77
+ yarn: "yarn dlx",
78
+ bun: "bunx"
79
+ };
80
+ function getInstallCommand(pm) {
81
+ return INSTALL_MAP[pm];
82
+ }
83
+ function getRunCommand(pm, script) {
84
+ if ("npm" === pm) return `npm run ${script}`;
85
+ return `${"yarn" === pm ? "yarn" : pm} ${script}`;
86
+ }
87
+ function getExecCommand(pm) {
88
+ return EXEC_MAP[pm];
89
+ }
90
+ function getAddCommand(pm, pkg, dev = false) {
91
+ const flag = dev ? ADD_DEV_FLAG[pm] : "";
92
+ return `${ADD_PREFIX[pm]} ${pkg}${flag ? ` ${flag}` : ""}`;
93
+ }
94
+ const ADD_PREFIX = {
95
+ pnpm: "pnpm add",
96
+ npm: "npm install",
97
+ yarn: "yarn add",
98
+ bun: "bun add"
99
+ };
100
+ const ADD_DEV_FLAG = {
101
+ pnpm: "-D",
102
+ npm: "-D",
103
+ yarn: "-D",
104
+ bun: "-D"
105
+ };
106
+ export { detectFromLockfile, detectPackageManager, getAddCommand, getExecCommand, getInstallCommand, getMissingPackages, getRunCommand, isPackageInstalled };
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@addmo/pkg-manager",
3
+ "version": "0.1.1-beta.1",
4
+ "description": "Detect package manager and generate commands for pnpm/npm/yarn/bun",
5
+ "type": "module",
6
+ "main": "./dist/cjs/index.cjs",
7
+ "module": "./dist/esm/index.js",
8
+ "types": "./dist/esm/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/esm/index.d.ts",
12
+ "import": "./dist/esm/index.js",
13
+ "require": "./dist/cjs/index.cjs",
14
+ "default": "./dist/esm/index.js"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "devDependencies": {
21
+ "@rslib/core": "^0.20.0",
22
+ "@rstest/core": "^0.9.2",
23
+ "@rstest/coverage-istanbul": "^0.3.0",
24
+ "@types/node": "^20.0.0",
25
+ "typescript": "^5.0.0"
26
+ },
27
+ "scripts": {
28
+ "build": "rslib build",
29
+ "test": "rstest run",
30
+ "test:coverage": "rstest run --coverage"
31
+ }
32
+ }