From 2f845a8a7956c56582e93329eff68f0fe9699fe6 Mon Sep 17 00:00:00 2001 From: Jacob Gillespie Date: Mon, 11 Apr 2022 15:51:08 +0100 Subject: [PATCH] Add node_modules/.bin executables to completion --- utils.go | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/utils.go b/utils.go index d891120..fc1f35c 100644 --- a/utils.go +++ b/utils.go @@ -6,6 +6,7 @@ import ( "os" "os/exec" "path/filepath" + "sort" "strings" "golang.org/x/exp/maps" @@ -62,9 +63,36 @@ func listScripts() []string { return empty } if pkg.Scripts == nil { - return empty + 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()] = "" + } + } } - return maps.Keys(*pkg.Scripts) + + scripts := maps.Keys(*pkg.Scripts) + sort.Strings(scripts) + return scripts } func resolveBinary(name string, binDirs []string) (string, error) {