Skip to content
Open
6 changes: 5 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
MIT License

Copyright (c) 2016 Luke Childs
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
yarn-completion are held by Ulysse Buonomo (2017).

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
44 changes: 23 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
# zsh-better-npm-completion
# yarn-extra-completion

> Better completion for `npm`
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 `npm install` recommendations from npm cache
* Makes `npm uninstall` recommendations from `dependencies`/`devDependencies`
* Shows detailed information on script contents for `npm run`
* Falls back to default npm completions if we don't have anything better
* 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 All @@ -16,54 +23,49 @@
Bundle `zsh-better-npm-completion` in your `.zshrc`

```shell
antigen bundle lukechilds/zsh-better-npm-completion
antigen bundle buonomo/yarn-completion
```

### Using [zplug](https://github.com/b4b4r07/zplug)
Load `zsh-better-npm-completion` as a plugin in your `.zshrc`

```shell
zplug "lukechilds/zsh-better-npm-completion", nice:10
zplug "buonomo/yarn-completion", defer:2

```
### Using [zgen](https://github.com/tarjoilija/zgen)

Include the load command in your `.zshrc`

```shell
zgen load lukechilds/zsh-better-npm-completion
zgen load buonomo/yarn-completion
```

### As an [Oh My ZSH!](https://github.com/robbyrussell/oh-my-zsh) custom plugin

Clone `zsh-better-npm-completion` into your custom plugins repo
Clone `yarn-completion` into your custom plugins repo

```shell
git clone https://github.com/lukechilds/zsh-better-npm-completion ~/.oh-my-zsh/custom/plugins/zsh-better-npm-completion
git clone https://github.com/buonomo/yarn-completion ~/.oh-my-zsh/custom/plugins/yarn-completion
```
Then load as a plugin in your `.zshrc`

```shell
plugins+=(zsh-better-npm-completion)
plugins+=(yarn-completion)
```

### Manually
Clone this repository somewhere (`~/.zsh-better-npm-completion` for example)
Clone this repository somewhere (`~/.yarn-completion` for example)

```shell
git clone https://github.com/lukechilds/zsh-better-npm-completion.git ~/.zsh-better-npm-completion
git clone https://github.com/buonomo/yarn-completion.git ~/.yarn-completion
```
Then source it in your `.zshrc`

```shell
source ~/.zsh-better-npm-completion/zsh-better-npm-completion.plugin.zsh
source ~/.yarn-completion/yarn-completion.plugin.zsh
```

## Related

- [`zsh-nvm`](https://github.com/lukechilds/zsh-nvm) - Zsh plugin for installing, updating and loading `nvm`
- [`gifgen`](https://github.com/lukechilds/gifgen) - Simple high quality GIF encoding

## License

MIT © Luke Childs
MIT © Ulysse Buonomo
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 "$@"
143 changes: 0 additions & 143 deletions zsh-better-npm-completion.plugin.zsh

This file was deleted.