/
root.go
118 lines (100 loc) · 2.45 KB
/
root.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
package cmd
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"github.com/teamkeel/keel/colors"
"github.com/teamkeel/keel/runtime"
)
var enabledDebugFlags = "true"
var (
flagProjectDir string
flagReset bool
flagPort string
flagNodePackagesPath string
flagPrivateKeyPath string
flagPattern string
flagTracing bool
flagVersion bool
flagVerboseTracing bool
flagEnvironment string
)
var rootCmd = &cobra.Command{
Use: "keel",
Short: "The Keel CLI",
RunE: func(cmd *cobra.Command, args []string) error {
if flagVersion {
fmt.Printf("v%s\n", runtime.GetVersion())
os.Exit(0)
}
return cmd.Help()
},
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
}
func init() {
workingDir, err := os.Getwd()
if err != nil {
panic(err)
}
rootCmd.PersistentFlags().StringVarP(&flagProjectDir, "dir", "d", workingDir, "directory containing a Keel project")
rootCmd.PersistentFlags().BoolVarP(&flagVersion, "version", "v", false, "Print the Keel CLI version")
}
func resolvePackageManager(dir string, isInit bool) (string, error) {
dir = filepath.Clean(dir)
for {
packageLockPath := filepath.Join(dir, "package-lock.json")
pnpmLockPath := filepath.Join(dir, "pnpm-lock.yaml")
_, err := os.Stat(packageLockPath)
if err == nil {
if isInit {
fmt.Println("|", colors.Gray("package-lock.json found at"), packageLockPath)
}
return "npm", nil
}
_, err = os.Stat(pnpmLockPath)
if err == nil {
if isInit {
fmt.Println("|", colors.Gray("pnpm-lock.yaml found at"), packageLockPath)
}
return "pnpm", nil
}
parent := filepath.Dir(dir)
if parent == dir {
break
}
dir = parent
}
_, err := exec.LookPath("pnpm")
if err != nil {
if isInit {
fmt.Println("|", colors.Gray("pnpm not detected"))
}
return "npm", nil
}
if !isInit {
fmt.Println(
colors.Yellow("| We're not sure which package manager you'd like to use as we can't find any lockfiles"),
)
fmt.Println("")
}
s := promptui.Select{
Label: "Which Node.js package manager would you like to use?",
Items: []string{"pnpm", "npm"},
Templates: &promptui.SelectTemplates{
Label: "{{ . }}",
Selected: fmt.Sprintf("%s {{ . | bold }} ", promptui.IconGood),
Active: "{{ . | cyan }}",
Inactive: "{{ . }}",
},
}
_, v, err := s.Run()
return v, err
}