From bb5c8f0012d0f35abd490b5981efafe02115330d Mon Sep 17 00:00:00 2001 From: matsuda Date: Wed, 11 Mar 2026 12:57:14 +0900 Subject: [PATCH] Add completion for npm --- zsh/functions/_npm | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 zsh/functions/_npm diff --git a/zsh/functions/_npm b/zsh/functions/_npm new file mode 100644 index 0000000..60f9f3a --- /dev/null +++ b/zsh/functions/_npm @@ -0,0 +1,30 @@ +#compdef npm + +_npm() { + local si=$IFS + + if [ "${words[-1]}" = 'run' -o "${words[-2]}" = 'run' ]; then + if [ -r package.json ]; then + cat package.json | jq --exit-status -r '.scripts' > /dev/null + if [ $? = 0 ]; then + _values \ + 'scripts' \ + $(cat package.json | jq -r '.scripts | keys[]') + else + _message 'project has no scripts' + fi + else + _message 'not npm project' + fi + else + _values \ + 'subcommands' \ + $(COMP_CWORD=$((CURRENT-1)) \ + COMP_LINE=$BUFFER \ + COMP_POINT=0 \ + npm completion -- "${words[@]}" \ + 2>/dev/null) + fi + + IFS=$si +}