Improve completion when '--cwd' is specified

This commit is contained in:
Shohei YOSHIDA 2020-05-09 14:28:04 +09:00
parent 4601310ac2
commit 44e1c1ed9c
1 changed files with 11 additions and 2 deletions

View File

@ -85,7 +85,12 @@ _global_commands=(
_yarn_commands_scripts() {
local -a scripts
if [[ -n $opt_args[--cwd] ]]; then
scripts=($(cd $opt_args[--cwd] && yarn run --json 2>/dev/null | sed -E '/Commands available|possibleCommands/!d;s/.*Commands available from binary scripts: ([^"]+)".*/\1/;s/.*"items":\[([^]]+).*/\1/;s/[" ]//g' | tr , '\n' | sed -e 's/:/\\:/g'))
else
scripts=($(yarn run --json 2>/dev/null | sed -E '/Commands available|possibleCommands/!d;s/.*Commands available from binary scripts: ([^"]+)".*/\1/;s/.*"items":\[([^]]+).*/\1/;s/[" ]//g' | tr , '\n' | sed -e 's/:/\\:/g'))
fi
_describe 'command or script' _commands -- _global_commands -- scripts
}
@ -94,7 +99,11 @@ _yarn_scripts() {
local -a scriptNames scriptCommands
local i runJSON
if [[ -n $opt_args[--cwd] ]]; then
runJSON=$(cd $opt_args[--cwd] && yarn run --json 2>/dev/null)
else
runJSON=$(yarn run --json 2>/dev/null)
fi
# Some sed utilities (e.g. Mac OS / BSD) don't interpret `\n` in a replacement
# pattern as a newline. See https://superuser.com/q/307165
binaries=($(sed -E '/Commands available/!d;s/.*Commands available from binary scripts: ([^"]+)".*/\1/;s/.*"items":\[([^]]+).*/\1/;s/[" ]//g;s/:/\\:/g;s/,/\'$'\n/g' <<< "$runJSON"))