-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecutor.go
59 lines (48 loc) · 1.23 KB
/
executor.go
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
package main
import (
"fmt"
"os"
"os/exec"
)
func PassThrough(args ...string) error {
packageManager, err := DetectPackageManager()
if err != nil {
return err
}
cmd := exec.Command(packageManager, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func Exec(command CommandAlias, args ...string) error {
return ExecWithFlag(command, []flagAlias{}, args...)
}
func ExecWithFlag(command CommandAlias, flags []flagAlias, args ...string) error {
packageManager, err := DetectPackageManager()
packageCommands := map[string][]string{
"npm": command.NPM,
"yarn": command.Yarn,
"pnpm": command.Pnpm,
}
if err != nil {
return err
}
flagArgs := make([]string, len(flags))
for i, flag := range flags {
if packageManager == "npm" {
flagArgs[i] = flag.NPM[0]
} else if packageManager == "yarn" {
flagArgs[i] = flag.Yarn[0]
} else if packageManager == "pnpm" {
flagArgs[i] = flag.Pnpm[0]
} else {
return fmt.Errorf("unknown package manager: %s", packageManager)
}
}
cmd := exec.Command(packageManager, append(packageCommands[packageManager], append(flagArgs, args...)...)...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}