-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpm.js
More file actions
executable file
·90 lines (74 loc) · 2.36 KB
/
pm.js
File metadata and controls
executable file
·90 lines (74 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env node
const { execSync } = require("child_process");
const { detect } = require("detect-package-manager");
const { version } = require("./package.json");
const args = process.argv.slice(2);
const HELP_TEXT = `
pm - Universal package manager CLI
Usage: pm [options] [command] [args...]
Options:
-v, --version Show version number
-h, --help Show this help message
--verbose Show which package manager is being used
Commands:
Any command supported by your package manager (install, add, remove, etc.)
Examples:
pm install Install dependencies
pm add lodash Add a package
pm remove lodash Remove a package
pm run build Run a script
pm --verbose install Show detected package manager and install
Detection Priority:
1. yarn.lock -> yarn
2. package-lock.json -> npm
3. pnpm-lock.yaml -> pnpm
4. bun.lockb -> bun
5. Fallback: checks if yarn/pnpm/bun exists, otherwise npm
`;
async function main() {
// Handle --version / -v (exits immediately)
if (args.includes("--version") || args.includes("-v")) {
console.log(`pm version ${version}`);
process.exit(0);
}
// Check for --verbose flag early
const verbose = args.includes("--verbose");
const showHelp = args.includes("--help") || args.includes("-h");
// Filter out our flags from args passed to package manager
const filteredArgs = args.filter(
(arg) => !["--verbose", "--help", "-h"].includes(arg)
);
const argsString = filteredArgs.join(" ");
// If verbose is set, detect and show PM first (even if --help is also set)
let pm;
if (verbose || !showHelp) {
try {
pm = await detect();
} catch (error) {
if (!showHelp) {
console.error(
"Could not detect package manager. Make sure you are in a Node.js project directory."
);
console.error(
"Hint: Create a package.json or lockfile (package-lock.json, yarn.lock, pnpm-lock.yaml, or bun.lockb)"
);
process.exit(1);
}
}
if (verbose && pm) {
console.log(`Detected package manager: ${pm}`);
}
}
// Handle --help / -h
if (showHelp) {
console.log(HELP_TEXT);
process.exit(0);
}
try {
execSync(`${pm} ${argsString}`, { stdio: "inherit" });
} catch (error) {
console.error(`Failed to execute command: ${pm} ${argsString}`);
process.exit(1);
}
}
main();