-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
173 lines (153 loc) · 3.54 KB
/
utils.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
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"golang.org/x/exp/maps"
)
func runScript(name string, args []string) error {
cwd, err := os.Getwd()
if err != nil {
return err
}
pkg, err := findPackageJSON(cwd)
if err != nil {
return err
}
if pkg.Scripts == nil {
pkg.Scripts = &map[string]string{}
}
binDirs := findBinDirs(cwd)
script, ok := (*pkg.Scripts)[name]
if !ok {
script, err = resolveBinary(name, binDirs)
if err != nil {
return fmt.Errorf("No script or binary found for %q", name)
}
}
env := os.Environ()
env = append(env, fmt.Sprintf("PATH=%s:%s", strings.Join(binDirs, ":"), os.Getenv("PATH")))
if ok {
env = append(env, fmt.Sprintf("npm_lifecycle_event=%s", name))
env = append(env, fmt.Sprintf("npm_lifecycle_script=%s", name))
}
commandArgs := strings.Join(append([]string{script}, args...), " ")
fmt.Printf("%s %s\n", green.Render("$"), gray.Render(name))
fmt.Printf("%s %s\n", green.Render("$"), gray.Render(commandArgs))
command := exec.Command("sh", "-c", commandArgs)
command.Env = env
command.Stdin = os.Stdin
command.Stdout = os.Stdout
command.Stderr = os.Stderr
return command.Run()
}
func listScripts() []string {
empty := []string{}
cwd, err := os.Getwd()
if err != nil {
return empty
}
pkg, err := findPackageJSON(cwd)
if err != nil {
return empty
}
if pkg.Scripts == nil {
pkg.Scripts = &map[string]string{}
}
// Add all executibles to the script list
binDirs := findBinDirs(cwd)
for _, dir := range binDirs {
files, err := os.ReadDir(dir)
if err != nil {
continue
}
for _, file := range files {
if file.IsDir() {
continue
}
if _, ok := (*pkg.Scripts)[file.Name()]; ok {
continue
}
info, err := os.Stat(filepath.Join(dir, file.Name()))
if err != nil {
continue
}
if info.Mode()&0111 != 0 {
(*pkg.Scripts)[file.Name()] = ""
}
}
}
scripts := maps.Keys(*pkg.Scripts)
sort.Strings(scripts)
return scripts
}
func resolveBinary(name string, binDirs []string) (string, error) {
for _, binDir := range binDirs {
path := filepath.Join(binDir, name)
if _, err := os.Stat(path); err == nil {
return path, nil
}
}
return "", fmt.Errorf("No binary found for %q", name)
}
type PackageJSON struct {
Scripts *map[string]string `json:"scripts"`
}
func findPackageJSON(current string) (*PackageJSON, error) {
filename, err := findUp(current, "package.json")
if err != nil {
return nil, err
}
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
var pkg PackageJSON
if err := json.NewDecoder(file).Decode(&pkg); err != nil {
return nil, err
}
return &pkg, nil
}
func findBinDirs(current string) []string {
moduleDirs := findAllUp(current, "node_modules")
binDirs := make([]string, 0, len(moduleDirs))
for _, moduleDir := range moduleDirs {
binDir := filepath.Join(moduleDir, ".bin")
binDirs = append(binDirs, binDir)
}
return binDirs
}
func findUp(current, name string) (string, error) {
for {
path := filepath.Join(current, name)
if _, err := os.Stat(path); err == nil {
return path, nil
}
next := filepath.Dir(current)
if next == current {
break
}
current = next
}
return "", fmt.Errorf("No %s found", name)
}
func findAllUp(current, name string) []string {
filenames := []string{}
for {
path := filepath.Join(current, name)
if _, err := os.Stat(path); err == nil {
filenames = append(filenames, path)
}
next := filepath.Dir(current)
if next == current {
break
}
current = next
}
return filenames
}