Skip to content

Commit

Permalink
doc: Change to yarn-extra-completion
Browse files Browse the repository at this point in the history
  • Loading branch information
BuonOmo committed Nov 7, 2022
1 parent 0788f39 commit 20bb35b
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 108 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2016 Luke Childs, 2017 Ulysse Buonomo
Copyright (c) 2016 Luke Childs, 2017-present Ulysse Buonomo

Copyright for portions of yarn-completion are held by Luke Childs (2016) as
part of project zsh-better-npm-completion. All other copyright for project
Expand Down
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
# yarn-completion
# yarn-extra-completion

This plugin is a fork of [zsh-better-npm-completion](https://github.com/lukechilds/zsh-better-npm-completion). It works the same way, as you can see with `npm` demo:
This plugin is greatly inspired by [zsh-better-npm-completion](https://github.com/lukechilds/zsh-better-npm-completion). It works the same way, as you can see with `npm` demo:

<img src="demo.gif" width="690">

* Makes `yarn add` recommendations from npm cache
* Makes `yarn remove` recommendations from `dependencies`/`devDependencies`
* Shows detailed information on script contents for `npm run`
* Makes `yarn add` recommendations from npm cache,
* Makes `yarn remove` recommendations from `dependencies`/`devDependencies`,
* Shows detailed information on script contents for `npm run`,
* Calls already installed yarn completion for any other command.

# Pre-requisites

You'll need [jq](https://stedolan.github.io/jq/download/).

It is strongly suggested that you also have the default yarn suggestion under
the name of `_yarn`. Or that you set `YARN_EXTRA_COMPLETION_DEFAULT=_your_default_function`.

## Installation

Expand Down
102 changes: 0 additions & 102 deletions yarn-completion.plugin.zsh

This file was deleted.

112 changes: 112 additions & 0 deletions yarn-extra-completion.plugin.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#compdef yarn

_yc_run_default() {
if (( ${+YARN_EXTRA_COMPLETION_DEFAULT} )); then
$YARN_EXTRA_COMPLETION_DEFAULT "$@"
elif (( ${+commands[_yarn]} )); then
_yarn "$@"
fi
}

_yc_yarn_command() {
echo "${words[2]}"
}

_yc_yarn_command_arg() {
echo "${words[3]}"
}

_yc_no_of_yarn_args() {
echo "$#words"
}

_yc_check_jq() {
(( ${+commands[jq]} )) || echo "\nyarn-completion needs jq\n"
}

_yc_recursively_look_for() {
local filename="$1"
local dir=$PWD
while [ ! -e "$dir/$filename" ]; do
dir=${dir%/*}
[[ "$dir" = "" ]] && break
done
[[ ! "$dir" = "" ]] && echo "$dir/$filename"
}

_yc_yarn_add_completion() {
# Only run on `yarn add ?`
[[ ! "$(_yc_no_of_yarn_args)" = "3" ]] && return

local packages=($(yarn cache list --json | jq --raw-output '.data.body[] | .[0]' 2> /dev/null))

# Return if we don't have any cached modules
[[ "$#packages" = 0 ]] && return

# If we do, recommend them
_values 'packages' $packages
}

_yc_yarn_remove_completion() {
# Use default yarn completion to recommend global modules
[[ "$(_yc_yarn_command_arg)" = "-g" ]] || [[ "$(_yc_yarn_command_arg)" = "--global" ]] && return

# Look for a package.json file
local package_json="$(_yc_recursively_look_for package.json)"

# Return if we can't find package.json
[[ "$package_json" = "" ]] && return

local values=($(jq --raw-output '(.devDependencies, .dependencies) | keys[]' $package_json 2> /dev/null))

[[ "$#values" = 0 ]] && return

_values 'installed' $values
}

_yc_yarn_run_completion() {
# Only run on `yarn run ?`
[[ ! "$(_yc_no_of_yarn_args)" = "3" ]] && return

# Look for a package.json file
local package_json="$(_yc_recursively_look_for package.json)"

# Return if we can't find package.json
[[ "$package_json" = "" ]] && return

local -a scripts
scripts=(${(f)"$(
jq --raw-output '
.scripts | to_entries[] | "\(.key):\(.value | gsub("\n";"\\\\n"))"
' $package_json 2> /dev/null
)"})

[[ "$#scripts" = 0 ]] && return

_describe 'scripts' scripts
}

_yc_zsh_yarn_extra_completion() {
# Show yarn commands if not typed yet
[[ $(_yc_no_of_yarn_args) -le 2 ]] && _yc_run_default "$@" && return

# Load custom completion commands
case "$(_yc_yarn_command)" in
add)
_yc_check_jq
_yc_yarn_add_completion
;;
remove)
_yc_check_jq
_yc_yarn_remove_completion
;;
run)
_yc_check_jq
_yc_yarn_run_completion
;;
*)
_yc_run_default "$@"
esac
}

_yc_zsh_yarn_extra_completion "$@"

0 comments on commit 20bb35b

Please sign in to comment.