-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
200 lines (158 loc) · 4.36 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"sort"
"strings"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type PackageJsonFields struct {
Scripts map[string]string `json:"scripts"`
Dependencies map[string]string `json:"dependencies"`
DevDependencies map[string]string `json:"devDependencies"`
}
type Script struct {
name, command string
}
func (s Script) Title() string { return s.name }
func (s Script) Description() string { return s.command }
func (s Script) FilterValue() string { return s.name }
type CommandExecuted struct{}
var lockfilesToPackageManagers = map[string]string{
"pnpm-lock.yaml": "pnpm",
"package-lock.json": "npm",
"bun.lockb": "bun",
"yarn.lock": "yarn",
}
var packageManager string
func detectPackageManager() string {
var lockfiles []string
cwd, err := os.Getwd()
if err != nil {
printErrorFatal("Unable to get current directory", err)
}
files, err := os.ReadDir(cwd)
if err != nil {
printErrorFatal("Unable to read contents of current directory", err)
}
for _, file := range files {
if _, setInMap := lockfilesToPackageManagers[file.Name()]; setInMap {
lockfiles = append(lockfiles, file.Name())
}
if !file.IsDir() {
switch file.Name() {
case "pnpm-lock.yaml":
packageManager = "pnpm"
case "package-lock.json":
packageManager = "npm"
case "bun.lockb":
packageManager = "bun"
case "yarn.lock":
packageManager = "yarn"
}
}
}
if len(lockfiles) > 1 {
multipeLockfilesErr := errors.New("- " + strings.Join(lockfiles, "\n- "))
printErrorFatal("Found multiple lockfiles", multipeLockfilesErr)
}
if packageManager == "" {
packageManager = "npm"
}
return packageManager
}
func installDependencies(packageManager string) {
contents, err := os.ReadDir("node_modules")
if err != nil || len(contents) == 0 {
if os.IsPermission(err) {
printErrorFatal("Unable to read node_modules directory", err)
}
command := exec.Command(packageManager, "install")
command.Stdout = os.Stdout
command.Stderr = os.Stderr
fmt.Printf("Installing packages using %v\n", packageManager)
err := command.Run()
if err != nil {
printErrorFatal("Error running command", err)
}
}
}
func runScript(packageManager, scriptName string) tea.Cmd {
command := exec.Command(packageManager, "run", scriptName)
return tea.ExecProcess(command, func(err error) tea.Msg {
if err != nil {
return tea.Quit()
}
return CommandExecuted{}
})
}
type model struct {
list list.Model
}
func (m model) Init() tea.Cmd {
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
if msg.String() == "ctrl+c" {
return m, tea.Quit
}
if msg.String() == "enter" {
script, _ := m.list.SelectedItem().(Script)
return m, runScript(packageManager, script.name)
}
case tea.WindowSizeMsg:
h, v := lipgloss.NewStyle().GetFrameSize()
m.list.SetSize(msg.Width-h, msg.Height-v)
case CommandExecuted:
return m, tea.Quit
}
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}
func (m model) View() string {
return lipgloss.NewStyle().Margin(1, 2).Render(m.list.View())
}
func main() {
jsonData, err := os.ReadFile("package.json")
if err != nil {
printErrorFatal("package.json not found", err)
}
detectPackageManager()
var parsedJson PackageJsonFields
parseErr := json.Unmarshal(jsonData, &parsedJson)
if parseErr != nil {
printErrorFatal("Unable to parse package.json", parseErr)
}
scriptsList := parsedJson.Scripts
if len(scriptsList) == 0 {
printErrorFatal("No scripts to run", nil)
}
if len(parsedJson.Dependencies) > 0 || len(parsedJson.DevDependencies) > 0 {
installDependencies(packageManager)
}
var items []list.Item
for name, command := range scriptsList {
items = append(items, Script{name, command})
}
sort.SliceStable(items, func(i, j int) bool {
return items[i].(Script).name < items[j].(Script).name
})
m := model{list: list.New(items, list.NewDefaultDelegate(), 0, 0)}
m.list.Title = "Scripts to Run"
m.list.Styles.Title = lipgloss.NewStyle().
Foreground(lipgloss.Color("#fff")).
Background(lipgloss.Color("#bc54c4")).
Padding(0, 1)
p := tea.NewProgram(m, tea.WithAltScreen())
if _, err := p.Run(); err != nil {
printErrorFatal("Error running program", err)
}
}