98a916b2cf67a47f97268dec9d68a15cb2c092ba

Author
Carlo Sala <carlosalag@protonmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

feat(yarn): upgrade completion to latest version (#10516)

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/yarn/_yarn b/plugins/yarn/_yarn
  2index 9173eca205647565d3f5d6d14906efaa3c4a28c8..9db02602ed35a8c11c695cd9945f4c0e6378dcf9 100644
  3--- a/plugins/yarn/_yarn
  4+++ b/plugins/yarn/_yarn
  5@@ -85,36 +85,67 @@ _global_commands=(
  6   'upgrade-interactive:Interactively upgrade packages'
  7 )
  8 
  9+_yarn_find_package_json() {
 10+  local dir=$(cd "$1" && pwd)
 11+
 12+  while true
 13+  do
 14+    if [[ -e "${dir}/package.json" ]]; then
 15+      echo "${dir}/package.json"
 16+      return
 17+    fi
 18+
 19+    if [[ $dir == '/' ]]; then
 20+      break
 21+    fi
 22+
 23+    dir=$(dirname $dir)
 24+  done
 25+}
 26+
 27 _yarn_commands_scripts() {
 28-  local -a scripts
 29+  local -a scripts binaries
 30+  local packageJson
 31+
 32   if [[ -n $opt_args[--cwd] ]]; then
 33-    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'))
 34+    packageJson=$(_yarn_find_package_json $opt_args[--cwd])
 35+    binaries=($(cd $opt_args[--cwd] && echo node_modules/.bin/*(x:t)))
 36   else
 37-    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'))
 38+    packageJson=$(_yarn_find_package_json $pwd)
 39+    binaries=($(echo node_modules/.bin/*(x:t)))
 40   fi
 41 
 42-  _describe 'command or script' _commands -- _global_commands -- scripts
 43+  if [[ -n $packageJson ]]; then
 44+    scripts=($(cat "$packageJson" | perl -0777 -MJSON::PP -n -E '%r=decode_json($_); say for sort keys %{$r->{scripts}}'))
 45+  fi
 46+
 47+  _describe 'command or script' _commands -- _global_commands -- scripts -- binaries
 48 }
 49 
 50 _yarn_scripts() {
 51-  local -a commands binaries scripts
 52-  local -a scriptNames scriptCommands
 53-  local i runJSON
 54+  local -a binaries scripts
 55+  local -a commands
 56+  local packageJson
 57 
 58   if [[ -n $_yarn_run_cwd ]]; then
 59-    runJSON=$(cd $_yarn_run_cwd && yarn run --json 2>/dev/null)
 60+    packageJson=$(_yarn_find_package_json $_yarn_run_cwd)
 61+    if [[ -d "${_yarn_run_cwd}/node_modules" ]]; then
 62+      binaries=($(cd $_yarn_run_cwd && echo node_modules/.bin/*(x:t)))
 63+    else
 64+      binaries=($(cd $_yarn_run_cwd && yarn bin | perl -wln -e 'm{^[^:]+: (\S+)$} and print $1'))
 65+    fi
 66   else
 67-    runJSON=$(yarn run --json 2>/dev/null)
 68+    packageJson=$(_yarn_find_package_json $pwd)
 69+    if [[ -d node_modules ]]; then
 70+      binaries=($(echo node_modules/.bin/*(x:t)))
 71+    else
 72+      binaries=($(yarn bin | perl -wln -e 'm{^[^:]+: (\S+)$} and print $1'))
 73+    fi
 74+  fi
 75+
 76+  if [[ -n $packageJson ]]; then
 77+    scripts=("${(@f)$(cat ${packageJson} | perl -0777 -MJSON::PP -n -E '%r=%{decode_json($_)->{scripts}}; printf "$_:$r{$_}\n" for sort keys %r')}")
 78   fi
 79-  # Some sed utilities (e.g. Mac OS / BSD) don't interpret `\n` in a replacement
 80-  # pattern as a newline. See https://superuser.com/q/307165
 81-  binaries=($(sed -E '/Commands available/!d;s/.*Commands available from binary scripts: ([^"]+)".*/\1/;s/.*"items":\[([^]]+).*/\1/;s/[" ]//g;s/:/\\:/g;s/,/\'$'\n/g' <<< "$runJSON"))
 82-  scriptNames=($(sed -E '/possibleCommands/!d;s/.*"items":\[([^]]+).*/\1/;s/[" ]//g;s/:/\\:/g;s/,/\'$'\n/g' <<< "$runJSON"))
 83-  scriptCommands=("${(@f)$(sed -E '/possibleCommands/!d;s/.*"hints":\{(.+")\}.*/\1/;s/"[^"]+"://g;s/:/\\:/g;s/","/\'$'\n/g;s/(^"|"$)//g' <<< "$runJSON")}")
 84-
 85-  for (( i=1; i <= $#scriptNames; i++ )); do
 86-    scripts+=("${scriptNames[$i]}:${scriptCommands[$i]}")
 87-  done
 88 
 89   commands=('env' $scripts $binaries)
 90   _describe 'command' commands
 91@@ -137,7 +168,13 @@ _yarn_add_files() {
 92 }
 93 
 94 _yarn_workspaces() {
 95-  local -a workspaces=(${(@f)$(yarn workspaces info |sed -n -e 's/^  "\([^"]*\)": {/\1/p')})
 96+  local version=$(yarn --version |sed -n 's|\([0-9]*\).*|\1|p')
 97+  local -a workspaces
 98+  if [[ $version == "1" ]]; then
 99+    workspaces=(${(@f)$(yarn workspaces info |sed -n -e 's/^  "\([^"]*\)": {/\1/p')})
100+  else
101+    workspaces=(${(@f)$(yarn workspaces list --json | sed -n 's|.*"name":"\([^"]*\)"}|\1|p')})
102+  fi
103   _describe 'workspace' workspaces
104 }