@@ -2,7 +2,6 @@ package cmd
2
2
3
3
import (
4
4
"encoding/json"
5
- "errors"
6
5
"fmt"
7
6
"os"
8
7
"sort"
@@ -29,8 +28,10 @@ var runCommand = cobra.Command{
29
28
}
30
29
31
30
func init () {
32
- runCommand .PersistentFlags ().StringVarP (& workingDirectory , "working-directory" , "w" , "" , "set the working directory for all commands" )
33
- runCommand .PersistentFlags ().BoolVarP (& continueOnError , "continue-on-error" , "c" , false , "continue running commands after a failure" )
31
+ runCommand .PersistentFlags ().StringVarP (& workingDirectory ,
32
+ "working-directory" , "w" , "" , "set the working directory for all commands" )
33
+ runCommand .PersistentFlags ().BoolVarP (& continueOnError ,
34
+ "continue-on-error" , "c" , false , "continue running commands after a failure" )
34
35
runCommand .PersistentFlags ().BoolVarP (& noShell , "no-subshell" , "S" , false , "do not run commands in a subshell" )
35
36
runCommand .PersistentFlags ().BoolVarP (& noColor , "no-color" , "C" , false , "do not colorize label output" )
36
37
@@ -43,60 +44,87 @@ func init() {
43
44
}
44
45
45
46
func collectCommands (args []string ) ([]string , []string , error ) {
46
- commandStrings := []string {}
47
- commands := []string {}
47
+ // The commands as provided by the user
48
+ providedCommands := []string {}
49
+
50
+ // "Resolved" commands (e.g. prefixed with "bun run", etc.)
51
+ runnableCommands := []string {}
48
52
49
53
for _ , cmd := range args {
50
- commandStrings = append (commandStrings , cmd )
51
- commands = append (commands , cmd )
54
+ providedCommands = append (providedCommands , cmd )
55
+ runnableCommands = append (runnableCommands , cmd )
56
+ }
57
+
58
+ var scripts []string
59
+
60
+ if len (npmCmds ) > 0 {
61
+ var err error
62
+ scripts , err = getPackageJSONScripts ()
63
+ if err != nil {
64
+ return nil , nil , err
65
+ }
52
66
}
53
67
54
68
for _ , cmd := range npmCmds {
55
69
if strings .HasSuffix (cmd , "*" ) {
56
70
prefix := strings .TrimSuffix (cmd , "*" )
57
- pkgFile , err := os .ReadFile ("package.json" )
58
- if err != nil {
59
- return nil , nil , fmt .Errorf ("reading package.json: %w" , err )
60
- }
61
- var pkg map [string ]any
62
- if err := json .Unmarshal (pkgFile , & pkg ); err != nil {
63
- return nil , nil , fmt .Errorf ("unmarshalling package.json: %w" , err )
64
- }
65
71
66
72
// See if any "scripts" match our prefix
67
73
matchingScripts := []string {}
68
74
69
- scripts , ok := pkg ["scripts" ].(map [string ]any )
70
- if ! ok {
71
- return nil , nil , errors .New ("invalid scripts in package.json" )
72
- }
73
-
74
- for script := range scripts {
75
+ for _ , script := range scripts {
75
76
if strings .HasPrefix (script , prefix ) {
76
77
matchingScripts = append (matchingScripts , script )
77
78
}
78
79
}
79
80
80
- sort .Strings (matchingScripts )
81
-
82
81
for _ , script := range matchingScripts {
83
- commandStrings = append (commandStrings , script )
82
+ providedCommands = append (providedCommands , script )
83
+
84
84
if runWithBun {
85
- commands = append (commands , "bun run " + script )
85
+ runnableCommands = append (runnableCommands , "bun run " + script )
86
86
} else {
87
- commands = append (commands , "npm run " + script )
87
+ runnableCommands = append (runnableCommands , "npm run " + script )
88
88
}
89
89
}
90
90
91
91
continue
92
92
}
93
93
94
- script := "npm run " + cmd
95
- commandStrings = append (commandStrings , cmd )
96
- commands = append (commands , script )
94
+ providedCommands = append (providedCommands , cmd )
95
+
96
+ if runWithBun {
97
+ runnableCommands = append (runnableCommands , "bun run " + cmd )
98
+ } else {
99
+ runnableCommands = append (runnableCommands , "npm run " + cmd )
100
+ }
101
+ }
102
+
103
+ return providedCommands , runnableCommands , nil
104
+ }
105
+
106
+ func getPackageJSONScripts () ([]string , error ) {
107
+ pkgFile , err := os .ReadFile ("package.json" )
108
+ if err != nil {
109
+ return nil , fmt .Errorf ("reading package.json: %w" , err )
110
+ }
111
+
112
+ var pkgJSON struct {
113
+ Scripts map [string ]string `json:"scripts"`
114
+ }
115
+
116
+ if err := json .Unmarshal (pkgFile , & pkgJSON ); err != nil {
117
+ return nil , fmt .Errorf ("unmarshalling package.json: %w" , err )
97
118
}
98
119
99
- return commandStrings , commands , nil
120
+ scripts := make ([]string , 0 , len (pkgJSON .Scripts ))
121
+ for script := range pkgJSON .Scripts {
122
+ scripts = append (scripts , script )
123
+ }
124
+
125
+ sort .Strings (scripts )
126
+
127
+ return scripts , nil
100
128
}
101
129
102
130
func collectLabels (commandStrings []string ) []string {
0 commit comments