Skip to content

Commit

Permalink
properly parse and complete workspace names and scripts
Browse files Browse the repository at this point in the history
closes #29
supersedes #30
  • Loading branch information
dsifford committed Apr 11, 2019
1 parent e57bf84 commit 4d40892
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions yarn-completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -892,19 +892,21 @@ _yarn_version() {
_yarn_workspace() {
((depth++))
declare -i args
declare workspaces_info

case "$cur" in
-*) ;;
*)
__yarn_count_args
case "$args" in
[0-2])
declare workspace_path
declare -a workspaces=()
for workspace_path in $(__yarn_get_package_fields -t array workspaces); do
workspaces+=("$(basename "$workspace_path")")
done
COMPREPLY=($(compgen -W "${workspaces[*]}" -- "$cur"))
workspaces_info=$(yarn workspaces info -s 2> /dev/null)
if [[ -n $workspaces_info ]]; then
mapfile -t < <(
sed -n 's/^ \{2\}"\([^"]*\)": {$/\1/p' <<< "$workspaces_info"
)
COMPREPLY=($(compgen -W "${MAPFILE[*]}" -- "$cur"))
fi
return 0
;;
3)
Expand All @@ -913,6 +915,16 @@ _yarn_workspace() {
;;
*)
declare cmd
workspaces_info=$(yarn workspaces info -s 2> /dev/null)

if [[ -n $workspaces_info ]]; then
PWD=$(
sed -n '/^ \{2\}"'"${COMP_WORDS[2]}"'": {$/,/^ \{2\}},\{0,1\}$/{
s/^ \{4\}"location": "\([^"]*\)",$/\1/p
}' <<< "$workspaces_info"
)
fi

__yarn_get_command -d 3
"_yarn_${cmd//-/_}" 2> /dev/null
return $?
Expand Down Expand Up @@ -982,8 +994,12 @@ _yarn_yarn() {
}

_yarn() {
declare prev_shopt
prev_shopt=$(shopt -p extglob)
# shellcheck disable=SC2064
trap "
PWD=$PWD
$(shopt -p extglob)
set +o pipefail
" RETURN

This comment has been minimized.

Copy link
@borekb

borekb Apr 11, 2019

Is RETURN valid here? I'm getting trap:2: undefined signal: RETURN with this. (It's true that I'm trying in Zsh via this workaround but shouldn't the symbol here be SIG... or a number?)

This comment has been minimized.

Copy link
@dsifford

dsifford Apr 11, 2019

Author Owner

This is correct in bash. The we need to trap the function return and not a specific signal or EXIT because it's a shell function and not a shell script.

I'll look into seeing if zsh has a workaround.

This comment has been minimized.

Copy link
@dsifford

dsifford Apr 11, 2019

Author Owner

@borekb can you try replacing these lines with the following and let me know if this works?

	if [[ $ZSH_VERSION ]]; then
		trap "
			PWD=$PWD
			$(shopt -p extglob)
			set +o pipefail
		" EXIT
	else
		trap "
			PWD=$PWD
			$(shopt -p extglob)
			set +o pipefail
		" RETURN
	fi

This comment has been minimized.

Copy link
@borekb

borekb Apr 11, 2019

That indeed fixes the issue with "undefined signal", thanks!

However, I suspect there will be more issues with Zsh compatibility, for example, I get the list of all commands after yarn workspaces [TAB], not just info and run. Also, completion of my packages / workpaces after yarn workspace [TAB] is not working. As you said in #35, Zsh is very different from Bash when it comes to completions and whatever the compatibility layer provided by bashcompinit is, it's probably not reliable enough.

Which is a shame because I really like you package (but I also really like Zsh 😄).

This comment has been minimized.

Copy link
@dsifford

dsifford Apr 12, 2019

Author Owner

Dang.. ☹️

Sorry we couldn't get this working. If you're able to figure out what the incompatibility is this time, let me know. I'm curious.

This comment has been minimized.

Copy link
@borekb

borekb Apr 12, 2019

That's beyond my capabilities, unfortunately. I don't even know where to start or who to ask, sorry that I won't be of much help here...

This comment has been minimized.

Copy link
@borekb

borekb Apr 12, 2019

Continued here: #35 (comment)


shopt -s extglob
set -o pipefail
Expand Down Expand Up @@ -1160,12 +1176,6 @@ _yarn() {
__yarn_get_command -d 1

__yarn_flag_args || "_yarn_${cmd//-/_}" 2> /dev/null || __yarn_fallback

# Resets back to users' settings
$prev_shopt
set +o pipefail

return 0
}

if [[ ${BASH_VERSINFO[0]} -ge 4 && ${BASH_VERSINFO[1]} -ge 4 ]]; then
Expand Down

0 comments on commit 4d40892

Please sign in to comment.