-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathomnes.js
executable file
·71 lines (59 loc) · 1.61 KB
/
omnes.js
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
#!/usr/bin/env node
const fs = require("fs")
const { spawn } = require("child_process")
// Define the files to detect for each package manager
const packageManagerFiles = {
bun: "bun.lockb",
pnpm: "pnpm-lock.yaml",
npm: "package-lock.json",
yarn: "yarn.lock",
}
// Define the command formats for each package manager
const packageManagerCommands = {
bun: "bun",
pnpm: "pnpm",
npm: "npm",
yarn: "yarn",
}
// Function to run the command based on the detected package manager
function runCommand(packageManager, command) {
const [packageManagerCommand, ...commandArgs] = command.split(" ")
if (
packageManager === "npm" &&
!["run", "exec"].includes(packageManagerCommand)
) {
command = `${packageManagerCommand} ${commandArgs.join(" ")}`
}
const child = spawn(
packageManagerCommands[packageManager],
command.split(" "),
{
stdio: "inherit",
shell: true,
}
)
child.on("close", (code) => {
process.exit(code)
})
}
// Function to detect the package manager based on the presence of specific files
function detectPackageManager() {
for (const [packageManager, file] of Object.entries(packageManagerFiles)) {
if (fs.existsSync(file)) {
return packageManager
}
}
return "npm" // Default to npm if no lockfile is found
}
// Main function
function main() {
const command = process.argv.slice(2).join(" ")
if (!command) {
console.log("Please provide a command to run")
return
}
const packageManager = detectPackageManager()
console.log(`Detected package manager: ${packageManager}`)
runCommand(packageManager, command)
}
main()