File size: 1,532 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { execSync } from "child_process"
import { detect } from "package-manager-detector"

interface PackageManager {
  run: (script: string, args: string) => string
  add: (args: string[], isDev?: boolean) => string
  install: string
}

const commandMap: Record<string, PackageManager> = {
  npm: {
    run: (script: string, args: string) => `npm run ${script} -- ${args}`,
    add: (args: string[], isDev?: boolean) =>
      `npm install ${isDev ? "-D" : ""}${args.join(" ")}`,
    install: "npm install",
  },
  yarn: {
    run: (script: string, args: string) => `yarn ${script} ${args}`,
    add: (args: string[], isDev?: boolean) =>
      `yarn add ${isDev ? "-D" : ""}${args.join(" ")}`,
    install: "yarn",
  },
  pnpm: {
    run: (script: string, args: string) => `pnpm run ${script} -- ${args}`,
    add: (args: string[], isDev?: boolean) =>
      `pnpm add ${isDev ? "-D" : ""}${args.join(" ")}`,
    install: "pnpm install",
  },
  bun: {
    run: (script: string, args: string) => `bun run ${script} -- ${args}`,
    add: (args: string[], isDev?: boolean) =>
      `bun install ${isDev ? "-D" : ""}${args.join(" ")}`,
    install: "bun install",
  },
}

export async function installCommand(args: string[], cwd?: string) {
  const res = await detect({ cwd })
  const agent = res?.agent?.split("@")[0] || "npm"
  try {
    const command = Reflect.get(commandMap, agent)
    const str = command.add(args)
    execSync(str, { cwd, stdio: "inherit", encoding: "utf-8" })
  } catch (error) {
    console.error(error)
  }
}