pkgmgr 1.0.0 → 1.1.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/README.md CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  A minimal CLI that forwards commands to whichever package manager you're already using.
4
4
 
5
+ <a href="https://github.com/epicweb-dev/full-stack-foundations/commit/fdb0644909a6d5e847b9bf3fb12e8efd120aa036">
6
+ <img src="./demo.png" alt="Diff showing package.json scripts being updated to use pkgmgr instead of npm. Scripts like postinstall, start, dev, build, and test are changed from using npm and npx to pkgmgr and pkgmgrx. The result: these scripts now work seamlessly whether contributors use npm, pnpm, yarn, or bun." style="max-width: 691px; max-height: 868px;" />
7
+ </a>
8
+
9
+ **What's happening here:** A project replaces hardcoded `npm` and `npx` calls with `pkgmgr` and `pkgmgrx` in their package.json scripts. Now when a contributor runs `pnpm run dev`, the script uses `pnpm`. When another runs `bun run dev`, it uses `bun`. No configuration, no conditional logic—just automatic package manager detection.
10
+
5
11
  ## The Problem
6
12
 
7
13
  You're writing a script, a tool, or documentation that needs to run package manager commands. But different users use different package managers (npm, pnpm, yarn, bun), and you don't want to:
@@ -24,6 +30,10 @@ pkgmgr exec vitest # runs: <detected-pm> exec vitest
24
30
 
25
31
  ## Available Binaries
26
32
 
33
+ ### Package Manager Binaries (`pkgmgr`)
34
+
35
+ Forward commands directly to the detected package manager.
36
+
27
37
  | Binary | Fallback |
28
38
  |--------|----------|
29
39
  | `pkgmgr` | npm |
@@ -31,6 +41,22 @@ pkgmgr exec vitest # runs: <detected-pm> exec vitest
31
41
  | `pkgmgr-pnpm` | pnpm |
32
42
  | `pkgmgr-yarn` | yarn |
33
43
 
44
+ ### Exec Binaries (`pkgmgrx`)
45
+
46
+ Forward commands to the detected package manager's "exec" equivalent.
47
+
48
+ | Binary | Fallback | Executes |
49
+ |--------|----------|----------|
50
+ | `pkgmgrx` | npm | `npx <args>` |
51
+ | `pkgmgrx-bun` | bun | `bunx <args>` |
52
+ | `pkgmgrx-pnpm` | pnpm | `pnpm dlx <args>` |
53
+ | `pkgmgrx-yarn` | yarn | `yarn dlx <args>` |
54
+
55
+ ```bash
56
+ pkgmgrx cowsay "Hello" # runs: npx cowsay "Hello" (or bunx, pnpm dlx, yarn dlx)
57
+ pkgmgrx tsc --version # runs: npx tsc --version
58
+ ```
59
+
34
60
  All binaries use the same detection logic. The only difference is which package manager is used when detection fails (no `PKGMGR` env var and no `npm_config_user_agent`).
35
61
 
36
62
  ## Detection Logic
package/bin/lib.js CHANGED
@@ -1,5 +1,13 @@
1
1
  const SUPPORTED = new Set(['npm', 'pnpm', 'yarn', 'bun'])
2
2
 
3
+ // Maps package manager to its "exec" command equivalent
4
+ const EXEC_COMMANDS = {
5
+ npm: { bin: 'npx', args: [] },
6
+ pnpm: { bin: 'pnpm', args: ['dlx'] },
7
+ yarn: { bin: 'yarn', args: ['dlx'] },
8
+ bun: { bin: 'bunx', args: [] },
9
+ }
10
+
3
11
  export function detectPackageManager(fallback = 'npm') {
4
12
  // 1. Explicit override via environment variable
5
13
  const envOverride = process.env.PKGMGR
@@ -45,3 +53,32 @@ export async function run(fallback = 'npm') {
45
53
  process.exit(code ?? 0)
46
54
  })
47
55
  }
56
+
57
+ export async function runExec(fallback = 'npm') {
58
+ const pm = detectPackageManager(fallback)
59
+ const args = process.argv.slice(2)
60
+
61
+ if (args.length === 0) {
62
+ console.error('pkgmgrx: no command specified')
63
+ process.exit(1)
64
+ }
65
+
66
+ const { bin, args: prefixArgs } = EXEC_COMMANDS[pm]
67
+ const command = [...prefixArgs, ...args]
68
+
69
+ const { spawn } = await import('node:child_process')
70
+
71
+ const child = spawn(bin, command, {
72
+ stdio: 'inherit',
73
+ shell: process.platform === 'win32',
74
+ })
75
+
76
+ child.on('error', (err) => {
77
+ console.error(`pkgmgrx: failed to execute ${bin}: ${err.message}`)
78
+ process.exit(1)
79
+ })
80
+
81
+ child.on('close', (code) => {
82
+ process.exit(code ?? 0)
83
+ })
84
+ }
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ import { runExec } from './lib.js'
3
+ runExec('bun')
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ import { runExec } from './lib.js'
3
+ runExec('pnpm')
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ import { runExec } from './lib.js'
3
+ runExec('yarn')
package/bin/pkgmgrx.js ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ import { runExec } from './lib.js'
3
+ runExec('npm')
package/package.json CHANGED
@@ -1,12 +1,16 @@
1
1
  {
2
2
  "name": "pkgmgr",
3
- "version": "1.0.0",
3
+ "version": "1.1.1",
4
4
  "description": "Forward commands to the package manager you're already using",
5
5
  "bin": {
6
6
  "pkgmgr": "./bin/pkgmgr.js",
7
7
  "pkgmgr-bun": "./bin/pkgmgr-bun.js",
8
8
  "pkgmgr-pnpm": "./bin/pkgmgr-pnpm.js",
9
- "pkgmgr-yarn": "./bin/pkgmgr-yarn.js"
9
+ "pkgmgr-yarn": "./bin/pkgmgr-yarn.js",
10
+ "pkgmgrx": "./bin/pkgmgrx.js",
11
+ "pkgmgrx-bun": "./bin/pkgmgrx-bun.js",
12
+ "pkgmgrx-pnpm": "./bin/pkgmgrx-pnpm.js",
13
+ "pkgmgrx-yarn": "./bin/pkgmgrx-yarn.js"
10
14
  },
11
15
  "files": [
12
16
  "bin"