-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck.go
More file actions
222 lines (192 loc) · 5.94 KB
/
Copy pathcheck.go
File metadata and controls
222 lines (192 loc) · 5.94 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package internal
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
// HandleCheckCommand handles the special 'check' command that runs lint, typecheck, and test
func HandleCheckCommand(r *CommandRunner) error {
dirs := []string{r.CurrentDir}
if r.ProjectRoot != r.CurrentDir {
dirs = append(dirs, r.ProjectRoot)
}
// Try to find a native check command first
for _, dir := range dirs {
if cmd := r.findNativeCheckCommand(dir); cmd != nil {
return r.ExecuteCommand(cmd)
}
}
// If no native check command, synthesize by running lint, typecheck, and test separately
return r.synthesizeCheckCommand()
}
// synthesizeCheckCommand runs lint, typecheck, and test as separate commands
func (r *CommandRunner) synthesizeCheckCommand() error {
commands := []string{"lint", "typecheck", "test"}
var foundAny bool
var failedCommands []string
var hasErrors bool
// First check which commands are available
for _, cmdName := range commands {
if r.hasCommand(cmdName) {
foundAny = true
}
}
if !foundAny {
return fmt.Errorf("no check, lint, typecheck, or test commands found")
}
fmt.Fprintf(os.Stderr, "Running check (synthesizing from available commands)...\n")
for _, cmdName := range commands {
// Skip typecheck if it doesn't exist for this project type
if cmdName == "typecheck" && !r.hasTypecheckCapability() {
continue
}
if !r.hasCommand(cmdName) {
continue
}
fmt.Fprintf(os.Stderr, "\n→ Running %s...\n", cmdName)
subRunner := &CommandRunner{
Command: cmdName,
Args: r.Args,
CurrentDir: r.CurrentDir,
ProjectRoot: r.ProjectRoot,
}
if err := subRunner.Run(); err != nil {
hasErrors = true
failedCommands = append(failedCommands, cmdName)
fmt.Fprintf(os.Stderr, " ✗ %s failed: %v\n", cmdName, err)
}
}
if hasErrors {
return fmt.Errorf("check failed: %s", strings.Join(failedCommands, ", "))
}
return nil
}
func (r *CommandRunner) findNativeCheckCommand(dir string) *exec.Cmd {
// Check for mise
if FileExists(filepath.Join(dir, ".mise.toml")) {
project := ResolveProject(dir)
if miseSource := findSourceByName(project.CommandSources, "mise"); miseSource != nil {
commands := miseSource.ListCommands()
if _, exists := commands["check"]; exists {
cmd := exec.Command("mise", append([]string{"run", "check"}, r.Args...)...)
cmd.Dir = dir
return cmd
}
}
}
// Check for just
if FileExists(filepath.Join(dir, "justfile")) || FileExists(filepath.Join(dir, "Justfile")) {
project := ResolveProject(dir)
if justSource := findSourceByName(project.CommandSources, "just"); justSource != nil {
commands := justSource.ListCommands()
if _, exists := commands["check"]; exists {
cmd := exec.Command("just", append([]string{"check"}, r.Args...)...)
cmd.Dir = dir
return cmd
}
}
}
// Check for make
if FileExists(filepath.Join(dir, "Makefile")) || FileExists(filepath.Join(dir, "makefile")) {
project := ResolveProject(dir)
if makeSource := findSourceByName(project.CommandSources, "make"); makeSource != nil {
commands := makeSource.ListCommands()
if _, exists := commands["check"]; exists {
cmd := exec.Command("make", append([]string{"check"}, r.Args...)...)
cmd.Dir = dir
return cmd
}
}
}
// Check for npm/yarn/bun scripts
if FileExists(filepath.Join(dir, "package.json")) {
data, err := os.ReadFile(filepath.Join(dir, "package.json"))
if err == nil {
var pkg struct {
Scripts map[string]string `json:"scripts"`
}
if json.Unmarshal(data, &pkg) == nil {
if _, ok := pkg.Scripts["check"]; ok {
packageManager := detectPackageManager(dir)
if packageManager != "" {
cmd := exec.Command(packageManager, append([]string{"run", "check"}, r.Args...)...)
cmd.Dir = dir
return cmd
}
}
}
}
}
return nil
}
// hasCommand checks if a command exists in any runner
func (r *CommandRunner) hasCommand(command string) bool {
// Create a temporary runner to check for the specific command
// Build projects and check if command exists
projects := []*Project{}
projects = append(projects, ResolveProject(r.CurrentDir))
if r.ProjectRoot != r.CurrentDir && r.ProjectRoot != "" {
projects = append(projects, ResolveProject(r.ProjectRoot))
}
for _, project := range projects {
for _, source := range project.CommandSources {
if cmd := source.FindCommand(command, []string{}); cmd != nil {
return true
}
}
}
return false
}
// hasListedCommand reports whether any source explicitly lists one of the
// provided command names. This ignores synthesized fallbacks that don't appear
// in the source listings.
func (r *CommandRunner) hasListedCommand(names ...string) bool {
projects := []*Project{ResolveProject(r.CurrentDir)}
if r.ProjectRoot != r.CurrentDir && r.ProjectRoot != "" {
projects = append(projects, ResolveProject(r.ProjectRoot))
}
for _, project := range projects {
for _, source := range project.CommandSources {
commands := source.ListCommands()
for _, name := range names {
if _, ok := commands[name]; ok {
return true
}
}
}
}
return false
}
// hasTypecheckCapability checks if the project supports typechecking
func (r *CommandRunner) hasTypecheckCapability() bool {
dirs := []string{r.CurrentDir}
if r.ProjectRoot != r.CurrentDir {
dirs = append(dirs, r.ProjectRoot)
}
for _, dir := range dirs {
// TypeScript projects
if FileExists(filepath.Join(dir, "tsconfig.json")) {
return true
}
// Python projects with pyright or mypy
if FileExists(filepath.Join(dir, "pyproject.toml")) {
data, _ := os.ReadFile(filepath.Join(dir, "pyproject.toml"))
content := string(data)
if strings.Contains(content, "pyright") || strings.Contains(content, "mypy") {
return true
}
}
// Rust always has cargo check
if FileExists(filepath.Join(dir, "Cargo.toml")) {
return true
}
// Go can use go build for type checking
if FileExists(filepath.Join(dir, "go.mod")) {
return true
}
}
return false
}