git や docker 等、サブコマンドを多用するコマンドには補完のための設定方法が公式で用意されています。
しかし、 yarn は公式からサブコマンドを補完するための方法が提供されていないため、どうにかして yarn でもサブコマンドを補完する方法がないか探してみました。
方法1 dsifford/yarn-completion
補完する方法を探している際に、最初に発見した方法がこちらです。
READMEに記載されているインストールのコマンドを実行します。
mkdir -p "${BASH_COMPLETION_USER_DIR:-${XDG_DATA_HOME:-$HOME/.local/share}/bash-completion}/completions/"
curl -o "${BASH_COMPLETION_USER_DIR:-${XDG_DATA_HOME:-$HOME/.local/share}/bash-completion}/completions/yarn" https://raw.githubusercontent.com/dsifford/yarn-completion/master/yarn-completion.bash
yarn
まで入力してtabキーを入力すると、以下のように yarn のサブコマンド一覧が表示されました。
$ yarn
access info remove
add init run
audit install tag
autoclean licenses team
bin link unlink
cache list unplug
check login upgrade
config logout upgrade-interactive
create node version
exec outdated versions
generate-lock-entry owner why
global pack workspace
help policies workspaces
import publish
$ yarn a
access add audit autoclean
方法2: fish shell による補完
yarn のサブコマンドを補完するもう一つの方法は fish shell による補完です。
fish shell はわかりやすさを重視したユーザーにやさしい shell で、デフォルトで yarn の補完機能が備わっていました。
以下のように各コマンドと説明が表示されました。
> yarn
access (Not implemented)
add (Add packages)
audit (Perform vulnerability audit)
autoclean (Clean unnecessary files from dependencies)
bin (Show location of Yarn `bin` folder)
cache (Manage Yarn cache)
check (Check dependencies integrity)
config (Manage Yarn configuration)
create (Create new project)
exec (Run binaries)
generate-lock-entry (Generate lockfile entry)
global (Manage global packages)
help (Show available commands and flags)
rows 1 to 13 of 41
おまけ
npm は公式からサブコマンドを補完するためのサブコマンドが用意されています。
npm completion
の出力を ~/.bashrc
等に追加することで補完できるようになります。