From c6bce7358cdcb979f95018714b8dee81131f240a Mon Sep 17 00:00:00 2001 From: Shohei YOSHIDA Date: Tue, 7 Dec 2021 23:06:33 +0900 Subject: [PATCH 1/4] Update collect workspaces command --- src/_yarn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_yarn b/src/_yarn index 9173eca..b443e53 100644 --- a/src/_yarn +++ b/src/_yarn @@ -137,7 +137,7 @@ _yarn_add_files() { } _yarn_workspaces() { - local -a workspaces=(${(@f)$(yarn workspaces info |sed -n -e 's/^ "\([^"]*\)": {/\1/p')}) + local -a workspaces=(${(@f)$(yarn workspaces list --json | sed -n 's|.*"name":"\([^"]*\)"}|\1|p')}) _describe 'workspace' workspaces } From 2f54f2b21287a5753728feba1ae2abcbf9df2aab Mon Sep 17 00:00:00 2001 From: Shohei YOSHIDA Date: Wed, 8 Dec 2021 00:19:46 +0900 Subject: [PATCH 2/4] Support both older and newer versions --- src/_yarn | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/_yarn b/src/_yarn index b443e53..1e39837 100644 --- a/src/_yarn +++ b/src/_yarn @@ -137,7 +137,13 @@ _yarn_add_files() { } _yarn_workspaces() { - local -a workspaces=(${(@f)$(yarn workspaces list --json | sed -n 's|.*"name":"\([^"]*\)"}|\1|p')}) + local version=$(yarn --version |sed -n 's|\([0-9]*\).*|\1|p') + local -a workspaces + if [[ $version == "1" ]]; then + workspaces=(${(@f)$(yarn workspaces info |sed -n -e 's/^ "\([^"]*\)": {/\1/p')}) + else + workspaces=(${(@f)$(yarn workspaces list --json | sed -n 's|.*"name":"\([^"]*\)"}|\1|p')}) + fi _describe 'workspace' workspaces } From 839df669222b7c25b8ffcacbcbd3f01c9154df24 Mon Sep 17 00:00:00 2001 From: Shohei YOSHIDA Date: Thu, 9 Dec 2021 01:37:29 +0900 Subject: [PATCH 3/4] Don't use yarn run --json for newer yarn --- src/_yarn | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/_yarn b/src/_yarn index 1e39837..87c5a50 100644 --- a/src/_yarn +++ b/src/_yarn @@ -86,31 +86,32 @@ _global_commands=( ) _yarn_commands_scripts() { - local -a scripts + local -a scripts binaries + 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')) + scripts=($(cd $opt_args[--cwd] && cat package.json | perl -0777 -MJSON::PP -n -E '$r=decode_json($_); say for sort keys %{$r->{scripts}}')) + binaries=($(cd $opt_args[--cwd] && echo node_modules/.bin/*(x:t))) 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')) + scripts=($(cat package.json | perl -0777 -MJSON::PP -n -E '%r=decode_json($_); say for sort keys %{$r->{scripts}}')) + binaries=($(echo node_modules/.bin/*(x:t))) fi - _describe 'command or script' _commands -- _global_commands -- scripts + _describe 'command or script' _commands -- _global_commands -- scripts -- binaries } _yarn_scripts() { - local -a commands binaries scripts - local -a scriptNames scriptCommands - local i runJSON + local -a binaries scriptNames scriptCommands scripts + local -a commands if [[ -n $_yarn_run_cwd ]]; then - runJSON=$(cd $_yarn_run_cwd && yarn run --json 2>/dev/null) + scriptNames=($(cd $opt_args[--cwd] && cat package.json | perl -0777 -MJSON::PP -n -E '%r=%{decode_json($_)->{scripts}}; printf qq($_:$r{$_}\n) for sort keys %r')) + scriptCommands=("${(@f)$(cd $opt_args[--cwd] && cat package.json | perl -0777 -MJSON::PP -n -E '%r=%{decode_json($_)->{scripts}}; printf "$r{$_}\n" for sort keys %r')}") + binaries=($(cd $opt_args[--cwd] && echo node_modules/.bin/*(x:t))) else - runJSON=$(yarn run --json 2>/dev/null) + scriptNames=($(cat package.json | perl -0777 -MJSON::PP -n -E '%r=%{decode_json($_)->{scripts}}; printf "$_\n" for sort keys %r')) + scriptCommands=("${(@f)$(cat package.json | perl -0777 -MJSON::PP -n -E '%r=%{decode_json($_)->{scripts}}; printf "$r{$_}\n" for sort keys %r')}") + binaries=($(echo node_modules/.bin/*(x:t))) 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")) - 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")}") for (( i=1; i <= $#scriptNames; i++ )); do scripts+=("${scriptNames[$i]}:${scriptCommands[$i]}") From 790890e25658f9b5c6ba68264d888a2b191b895e Mon Sep 17 00:00:00 2001 From: Shohei YOSHIDA Date: Thu, 9 Dec 2021 19:44:11 +0900 Subject: [PATCH 4/4] Fix no node_modules case --- src/_yarn | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/_yarn b/src/_yarn index 87c5a50..824262a 100644 --- a/src/_yarn +++ b/src/_yarn @@ -100,23 +100,25 @@ _yarn_commands_scripts() { } _yarn_scripts() { - local -a binaries scriptNames scriptCommands scripts + local -a binaries scripts local -a commands if [[ -n $_yarn_run_cwd ]]; then - scriptNames=($(cd $opt_args[--cwd] && cat package.json | perl -0777 -MJSON::PP -n -E '%r=%{decode_json($_)->{scripts}}; printf qq($_:$r{$_}\n) for sort keys %r')) - scriptCommands=("${(@f)$(cd $opt_args[--cwd] && cat package.json | perl -0777 -MJSON::PP -n -E '%r=%{decode_json($_)->{scripts}}; printf "$r{$_}\n" for sort keys %r')}") - binaries=($(cd $opt_args[--cwd] && echo node_modules/.bin/*(x:t))) + scripts=("${(@f)$(cd $_yarn_run_cwd && cat package.json | perl -0777 -MJSON::PP -n -E '%r=%{decode_json($_)->{scripts}}; printf "$_:$r{$_}\n" for sort keys %r')}") + if [[ -d "${_yarn_run_cwd}/node_modules" ]]; then + binaries=($(cd $_yarn_run_cwd && echo node_modules/.bin/*(x:t))) + else + binaries=($(cd $_yarn_run_cwd && yarn bin | perl -wln -e 'm{^[^:]+: (\S+)$} and print $1')) + fi else - scriptNames=($(cat package.json | perl -0777 -MJSON::PP -n -E '%r=%{decode_json($_)->{scripts}}; printf "$_\n" for sort keys %r')) - scriptCommands=("${(@f)$(cat package.json | perl -0777 -MJSON::PP -n -E '%r=%{decode_json($_)->{scripts}}; printf "$r{$_}\n" for sort keys %r')}") - binaries=($(echo node_modules/.bin/*(x:t))) + scripts=("${(@f)$(cat package.json | perl -0777 -MJSON::PP -n -E '%r=%{decode_json($_)->{scripts}}; printf "$_:$r{$_}\n" for sort keys %r')}") + if [[ -d node_modules ]]; then + binaries=($(echo node_modules/.bin/*(x:t))) + else + binaries=($(yarn bin | perl -wln -e 'm{^[^:]+: (\S+)$} and print $1')) + fi fi - for (( i=1; i <= $#scriptNames; i++ )); do - scripts+=("${scriptNames[$i]}:${scriptCommands[$i]}") - done - commands=('env' $scripts $binaries) _describe 'command' commands }