Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: yarn version berry workspace autocompletion #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 33 additions & 14 deletions yarn-completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -921,12 +921,22 @@ _yarn_workspace() {
__yarn_count_args
case "$args" in
[0-2])
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"))
if [ -f .yarnrc.yml ]; then
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will only work properly if you are completing yarn from the root of the project. If you call yarn workspace <Tab> from any child directories, this will fail.

See the following snippet for how the script locates the package.json file.

while [[ -n $cwd ]]; do
if [[ -f "$cwd/package.json" ]]; then
package_dot_json="$cwd/package.json"
break
fi
cwd="${cwd%/*}"
done

workspaces_info=$(yarn workspaces list --json 2> /dev/null)
if [[ -n $workspaces_info ]]; then
mapfile -t < <(
sed -n 's/.*,"name"\:"\([^"]*\)"}/\1/p' <<< "$workspaces_info"
)
COMPREPLY=($(compgen -W "${MAPFILE[*]}" -- "$cur"))
fi
else
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
fi
return 0
;;
Expand All @@ -936,16 +946,25 @@ _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"
)
if [ -f .yarnrc.yml ]; then
workspaces_info=$(yarn workspaces list --json 2> /dev/null)
if [[ -n $workspaces_info ]]; then
PWD=$(
(sed -n 's/.*,"name"\:"\([^"]*\)"}/\1/p' | grep -e "$foo") <<< "$workspaces_info"
)
fi
else
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
fi


__yarn_get_command -d 3
"_yarn_${cmd//-/_}" 2> /dev/null
return $?
Expand Down