@@ -2,9 +2,11 @@ package cmd
22
33import (
44 "encoding/json"
5+ "errors"
56 "fmt"
67 "os"
78 "sort"
9+ "strconv"
810 "strings"
911
1012 "github.com/spf13/cobra"
@@ -18,7 +20,7 @@ var runCommand = cobra.Command{
1820 Use : "run <subcommand>" ,
1921 Aliases : []string {"r" },
2022 Short : "Run commands serially or concurrently (alias: r)" ,
21- RunE : func (cmd * cobra.Command , args []string ) error {
23+ RunE : func (cmd * cobra.Command , _ []string ) error {
2224 _ = cmd .Help ()
2325 os .Exit (1 )
2426 return nil
@@ -51,16 +53,22 @@ func collectCommands(args []string) ([]string, []string, error) {
5153 prefix := strings .TrimSuffix (cmd , "*" )
5254 pkgFile , err := os .ReadFile ("package.json" )
5355 if err != nil {
54- return nil , nil , err
56+ return nil , nil , fmt . Errorf ( "reading package.json: %w" , err )
5557 }
56- var pkg map [string ]interface {}
58+ var pkg map [string ]any
5759 if err := json .Unmarshal (pkgFile , & pkg ); err != nil {
58- return nil , nil , err
60+ return nil , nil , fmt . Errorf ( "unmarshalling package.json: %w" , err )
5961 }
6062
6163 // See if any "scripts" match our prefix
6264 matchingScripts := []string {}
63- for script := range pkg ["scripts" ].(map [string ]interface {}) {
65+
66+ scripts , ok := pkg ["scripts" ].(map [string ]any )
67+ if ! ok {
68+ return nil , nil , errors .New ("invalid scripts in package.json" )
69+ }
70+
71+ for script := range scripts {
6472 if strings .HasPrefix (script , prefix ) {
6573 matchingScripts = append (matchingScripts , script )
6674 }
@@ -70,13 +78,13 @@ func collectCommands(args []string) ([]string, []string, error) {
7078
7179 for _ , script := range matchingScripts {
7280 commandStrings = append (commandStrings , script )
73- commands = append (commands , fmt . Sprintf ( "npm run %s" , script ) )
81+ commands = append (commands , "npm run " + script )
7482 }
7583
7684 continue
7785 }
7886
79- script := fmt . Sprintf ( "npm run %s" , cmd )
87+ script := "npm run " + cmd
8088 commandStrings = append (commandStrings , cmd )
8189 commands = append (commands , script )
8290 }
@@ -88,12 +96,13 @@ func collectLabels(commandStrings []string) []string {
8896 labels := make ([]string , len (commandStrings ))
8997
9098 for i , cmdStr := range commandStrings {
91- if cmdAsLabel {
99+ switch {
100+ case cmdAsLabel :
92101 labels [i ] = cmdStr
93- } else if len (names ) > 0 {
102+ case len (names ) > 0 :
94103 labels [i ] = names [i ]
95- } else {
96- labels [i ] = fmt . Sprintf ( "%d" , i )
104+ default :
105+ labels [i ] = strconv . Itoa ( i )
97106 }
98107 }
99108
0 commit comments