-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
167 lines (147 loc) · 4.55 KB
/
main.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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"fmt"
"github.com/spf13/cobra"
"log"
"os"
"strings"
)
func main() {
var rootCmd = &cobra.Command{
Use: "pm",
Short: "A universal package manager wrapper",
DisableFlagParsing: true,
Run: func(cmd *cobra.Command, args []string) {
if err := PassThrough(args...); err != nil {
fmt.Fprintln(os.Stderr, err)
}
},
Args: cobra.ArbitraryArgs,
}
addCmd := &cobra.Command{
Use: "add <package name>",
Aliases: []string{"i", "install"},
Short: "Add dependency if package name is given.",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
log.Fatalf("No package name given")
}
cmd.Flags().Parse(args)
dev := GetBoolFlag(cmd, "save-dev", "dev")
peer := GetBoolFlag(cmd, "save-peer", "peer")
optional := GetBoolFlag(cmd, "save-optional", "optional")
global := GetBoolFlag(cmd, "global")
exact := GetBoolFlag(cmd, "exact")
frozenLockfile := GetBoolFlag(cmd, "frozen-lockfile")
if frozenLockfile {
Exec(Commands.CI)
return
}
var flags []flagAlias
if dev {
flags = append(flags, Flags.Dev)
}
if peer {
flags = append(flags, Flags.Peer)
}
if optional {
flags = append(flags, Flags.Optional)
}
if global {
flags = append(flags, Flags.Global)
}
if exact {
flags = append(flags, Flags.Exact)
}
args = filterFlags(args, cmd.Flags())
ExecWithFlag(Commands.Add, flags, args...)
nonTypedPackages := []string{}
for _, arg := range args {
if !strings.HasPrefix(arg, "@types/") {
ok, _ := CheckPackageExists("@types/" + arg)
if !ok {
continue
}
nonTypedPackages = append(nonTypedPackages, "@types/"+arg)
}
}
if !dev && !peer && !optional && !global {
for _, packageName := range nonTypedPackages {
ExecWithFlag(Commands.Add, []flagAlias{Flags.Dev}, packageName)
}
}
},
DisableFlagParsing: true,
}
addCmd.Flags().BoolP("save-dev", "D", false, "install package as dev dependency")
addCmd.Flags().Bool("dev", false, "install package as dev dependency")
addCmd.Flags().MarkHidden("dev")
addCmd.Flags().BoolP("save-peer", "P", false, "install package as peer dependency")
addCmd.Flags().Bool("peer", false, "install package as peer dependency")
addCmd.Flags().MarkHidden("peer")
addCmd.Flags().BoolP("save-optional", "O", false, "install package as optional dependency")
addCmd.Flags().Bool("optional", false, "install package as optional dependency")
addCmd.Flags().MarkHidden("optional")
addCmd.Flags().BoolP("global", "g", false, "install package globally")
addCmd.Flags().BoolP("exact", "E", false, "install exact version")
addCmd.Flags().Bool("frozen-lockfile", false, "don't generate a lockfile and fail if an update is needed")
addCmd.FParseErrWhitelist = cobra.FParseErrWhitelist{UnknownFlags: true}
installCmd := &cobra.Command{
Use: "install [package name]",
Aliases: []string{"i"},
Short: "Add dependency if package name is given. Otherwise, install the package",
Run: func(cmd *cobra.Command, args []string) {
frozenLockfile, _ := cmd.Flags().GetBool("frozen-lockfile")
if frozenLockfile {
Exec(Commands.CI)
return
}
if len(args) > 0 {
addCmd.Run(cmd, args)
} else {
Exec(Commands.Install)
}
},
DisableFlagParsing: true,
}
installCmd.Flags().Bool("frozen-lockfile", false, "don't generate a lockfile and fail if an update is needed")
installCmd.FParseErrWhitelist = cobra.FParseErrWhitelist{UnknownFlags: true}
uninstallCmd := &cobra.Command{
Use: "uninstall <package name>",
Aliases: []string{"rm", "remove", "un"},
Short: "Uninstall a package",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
log.Fatalf("No package name given")
}
Exec(Commands.Uninstall, args...)
},
DisableFlagParsing: true,
}
uninstallCmd.FParseErrWhitelist = cobra.FParseErrWhitelist{UnknownFlags: true}
ciCmd := &cobra.Command{
Use: "ci",
Short: "CI command",
Run: func(cmd *cobra.Command, args []string) {
Exec(Commands.CI)
},
}
ciCmd.FParseErrWhitelist = cobra.FParseErrWhitelist{UnknownFlags: true}
runCmd := &cobra.Command{
Use: "run",
Short: "Run command",
Run: func(cmd *cobra.Command, args []string) {
Exec(Commands.Run, args...)
},
DisableFlagParsing: true,
}
runCmd.FParseErrWhitelist = cobra.FParseErrWhitelist{UnknownFlags: true}
rootCmd.AddCommand(installCmd)
rootCmd.AddCommand(addCmd)
rootCmd.AddCommand(uninstallCmd)
rootCmd.AddCommand(ciCmd)
rootCmd.AddCommand(runCmd)
if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
}
}