From c6bdbc69e168b4dbdd25e6e32e02d049d2a456ef Mon Sep 17 00:00:00 2001 From: Maxim Devoir Date: Wed, 2 Oct 2019 02:47:13 -0700 Subject: [PATCH 1/3] Separate project scripts from binary packages If `jq` utility is installed: - Suggests script names from `package.json` and their associated script Following [this feedback](https://github.com/robbyrussell/oh-my-zsh/pull/8118#issuecomment-537148142), binaries will always be suggested. --- src/_yarn | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/_yarn b/src/_yarn index 34718e0..e99accf 100644 --- a/src/_yarn +++ b/src/_yarn @@ -76,14 +76,34 @@ _global_commands=( _yarn_commands_scripts() { local -a scripts - 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')) + scripts=($(yarn run --json 2>/dev/null | sed -E '/possibleCommands/!d;s/.*Commands available from binary scripts: ([^"]+)".*/\1/;s/.*"items":\[([^]]+).*/\1/;s/[" ]//g' | tr , '\n' | sed -e 's/:/\\:/g')) _describe 'command or script' _commands -- _global_commands -- scripts } _yarn_scripts() { + local -a commands + local -a binaries local -a scripts - 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')) - _describe 'script' scripts + local -a scriptsUnescaped + + binaries=($(yarn run --json 2>/dev/null | sed -E '/Commands available/!d;s/.*Commands available from binary scripts: ([^"]+)".*/\1/;s/.*"items":\[([^]]+).*/\1/;s/[" ]//g' | tr , '\n' | sed -e 's/:/\\:/g')) + scriptsUnescaped=($(yarn run --json 2>/dev/null | sed -E '/possibleCommands/!d;s/.*Commands available from binary scripts: ([^"]+)".*/\1/;s/.*"items":\[([^]]+).*/\1/;s/[" ]//g' | tr , '\n')) + scripts=($(echo "${scriptsUnescaped[@]}" | sed -e 's/:/\\:/g')) + scriptsObject=$(yarn run --json 2>/dev/null | sed -n '/.type.\s\?:\s\?.possibleCommands./p' | head -1) + + if [ "$(command -v jq)" ]; then + for script in "${scriptsUnescaped[@]}"; do + scriptCommand=$(echo -E $scriptsObject | jq ".data.hints.\"$script\"") + commands+=("$(echo "${script//:/\\:}"):$scriptCommand") + done + else + for script in "${scripts[@]}"; do + commands+=("$script:package\.json") + done + fi + + commands=("${commands[@]}" "${binaries[@]}") + _describe 'scripts' commands } _yarn_global_commands() { From a47b58ef97ad489f19fc1ecb9933597eef9a927a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Wed, 2 Oct 2019 07:42:15 -0700 Subject: [PATCH 2/3] Parse script commands without `jq` Source from https://github.com/robbyrussell/oh-my-zsh/pull/8118#issuecomment-537484017 --- src/_yarn | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/src/_yarn b/src/_yarn index e99accf..b50ad7f 100644 --- a/src/_yarn +++ b/src/_yarn @@ -81,29 +81,21 @@ _yarn_commands_scripts() { } _yarn_scripts() { - local -a commands - local -a binaries - local -a scripts - local -a scriptsUnescaped - - binaries=($(yarn run --json 2>/dev/null | sed -E '/Commands available/!d;s/.*Commands available from binary scripts: ([^"]+)".*/\1/;s/.*"items":\[([^]]+).*/\1/;s/[" ]//g' | tr , '\n' | sed -e 's/:/\\:/g')) - scriptsUnescaped=($(yarn run --json 2>/dev/null | sed -E '/possibleCommands/!d;s/.*Commands available from binary scripts: ([^"]+)".*/\1/;s/.*"items":\[([^]]+).*/\1/;s/[" ]//g' | tr , '\n')) - scripts=($(echo "${scriptsUnescaped[@]}" | sed -e 's/:/\\:/g')) - scriptsObject=$(yarn run --json 2>/dev/null | sed -n '/.type.\s\?:\s\?.possibleCommands./p' | head -1) + local -a commands binaries scripts + local -a scriptNames scriptCommands + local i runJSON - if [ "$(command -v jq)" ]; then - for script in "${scriptsUnescaped[@]}"; do - scriptCommand=$(echo -E $scriptsObject | jq ".data.hints.\"$script\"") - commands+=("$(echo "${script//:/\\:}"):$scriptCommand") - done - else - for script in "${scripts[@]}"; do - commands+=("$script:package\.json") - done - fi + runJSON=$(yarn run --json 2>/dev/null) + binaries=($(sed -E '/Commands available/!d;s/.*Commands available from binary scripts: ([^"]+)".*/\1/;s/.*"items":\[([^]]+).*/\1/;s/[" ]//g;s/:/\\:/g;s/,/\n/g' <<< "$runJSON")) + scriptNames=($(sed -E '/possibleCommands/!d;s/.*"items":\[([^]]+).*/\1/;s/[" ]//g;s/:/\\:/g;s/,/\n/g' <<< "$runJSON")) + scriptCommands=("${(@f)$(sed -E '/possibleCommands/!d;s/.*"hints":\{([^}]+)\}.*/\1/;s/"[^"]+"://g;s/:/\\:/g;s/","/\n/g;s/(^"|"$)//g' <<< "$runJSON")}") - commands=("${commands[@]}" "${binaries[@]}") - _describe 'scripts' commands + for (( i=1; i <= $#scriptNames; i++ )); do + scripts+=("${scriptNames[$i]}:${scriptCommands[$i]}") + done + + commands=($scripts $binaries) + _describe 'command' commands } _yarn_global_commands() { From c9b335769c5dfb79c782bc26a7022795eb606f1a Mon Sep 17 00:00:00 2001 From: Maxim Devoir Date: Mon, 7 Oct 2019 12:14:10 -0700 Subject: [PATCH 3/3] revert: suggest binaries on `yarn` This behavior is the same as before c6bdbc69e168b4dbdd25e6e32e02d049d2a456ef --- src/_yarn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_yarn b/src/_yarn index b50ad7f..d448fe1 100644 --- a/src/_yarn +++ b/src/_yarn @@ -76,7 +76,7 @@ _global_commands=( _yarn_commands_scripts() { local -a scripts - scripts=($(yarn run --json 2>/dev/null | sed -E '/possibleCommands/!d;s/.*Commands available from binary scripts: ([^"]+)".*/\1/;s/.*"items":\[([^]]+).*/\1/;s/[" ]//g' | tr , '\n' | sed -e 's/:/\\:/g')) + 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')) _describe 'command or script' _commands -- _global_commands -- scripts }