Repository navigation
Expand file tree
/
Copy pathweb.go
More file actions
157 lines (133 loc) · 3.69 KB
/
Copy pathweb.go
File metadata and controls
157 lines (133 loc) · 3.69 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
package cmd
import (
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/onyx-dot-app/onyx/tools/ods/internal/paths"
)
type webPackageJSON struct {
Scripts map[string]string `json:"scripts"`
}
// NewWebCommand creates a command that runs bun scripts from the web directory.
func NewWebCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "web <script> [args...]",
Short: "Run web/package.json bun scripts",
Long: webHelpDescription(),
Args: cobra.MinimumNArgs(1),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) > 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return webScriptNames(), cobra.ShellCompDirectiveNoFileComp
},
Run: func(cmd *cobra.Command, args []string) {
runWebScript(args)
},
}
cmd.Flags().SetInterspersed(false)
return cmd
}
func runWebScript(args []string) {
webDir, err := webDir()
if err != nil {
log.Fatalf("Failed to find web directory: %v", err)
}
nodeModules := filepath.Join(webDir, "node_modules")
if _, err := os.Stat(nodeModules); os.IsNotExist(err) {
log.Info("node_modules not found, running bun install --frozen-lockfile...")
installCmd := exec.Command("bun", "install", "--frozen-lockfile")
installCmd.Dir = webDir
installCmd.Stdout = os.Stdout
installCmd.Stderr = os.Stderr
installCmd.Stdin = os.Stdin
log.Fatalf("Failed to run bun install: %v", err)
}
}
scriptName := args[0]
scriptArgs := args[1:]
if len(scriptArgs) > 0 && scriptArgs[0] == "--" {
scriptArgs = scriptArgs[1:]
}
bunArgs := []string{"run", scriptName}
if len(scriptArgs) > 0 {
// bun requires "--" to forward flags to the underlying script.
bunArgs = append(bunArgs, "--")
bunArgs = append(bunArgs, scriptArgs...)
}
log.Debugf("Running in %s: bun %v", webDir, bunArgs)
webCmd := exec.Command("bun", bunArgs...)
webCmd.Dir = webDir
webCmd.Stdout = os.Stdout
webCmd.Stderr = os.Stderr
webCmd.Stdin = os.Stdin
if err := webCmd.Run(); err != nil {
// For wrapped commands, preserve the child process's exit code and
// avoid duplicating already-printed stderr output.
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
if code := exitErr.ExitCode(); code != -1 {
os.Exit(code)
}
}
log.Fatalf("Failed to run bun: %v", err)
}
}
func webScriptNames() []string {
scripts, err := loadWebScripts()
if err != nil {
return nil
}
names := make([]string, 0, len(scripts))
for name := range scripts {
names = append(names, name)
}
sort.Strings(names)
return names
}
func webHelpDescription() string {
description := `Run bun scripts from web/package.json.
Examples:
ods web dev
ods web lint
ods web test --watch`
scripts := webScriptNames()
if len(scripts) == 0 {
return description + "\n\nAvailable scripts: (unable to load)"
}
return description + "\n\nAvailable scripts:\n " + strings.Join(scripts, "\n ")
}
func loadWebScripts() (map[string]string, error) {
webDir, err := webDir()
if err != nil {
return nil, err
}
packageJSONPath := filepath.Join(webDir, "package.json")
data, err := os.ReadFile(packageJSONPath)
if err != nil {
return nil, fmt.Errorf("failed to read %s: %w", packageJSONPath, err)
}
var pkg webPackageJSON
if err := json.Unmarshal(data, &pkg); err != nil {
return nil, fmt.Errorf("failed to parse %s: %w", packageJSONPath, err)
}
if pkg.Scripts == nil {
return nil, nil
}
return pkg.Scripts, nil
}
func webDir() (string, error) {
root, err := paths.GitRoot()
if err != nil {
return "", err
}
return filepath.Join(root, "web"), nil
}