From a2398fc0454a641b020ee2d96489524bead8f3f9 Mon Sep 17 00:00:00 2001 From: Indrajit Raychaudhuri Date: Fri, 7 Dec 2018 17:08:12 -0600 Subject: [PATCH] node: Cache completion for additional helpers Add support for npm and additional well-known helper commands --- modules/node/init.zsh | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/modules/node/init.zsh b/modules/node/init.zsh index 0a516f0..75a7ebb 100644 --- a/modules/node/init.zsh +++ b/modules/node/init.zsh @@ -28,18 +28,28 @@ elif (( ! $+commands[node] )); then return 1 fi -# Load NPM completion. -if (( $+commands[npm] )); then - cache_file="${TMPDIR:-/tmp}/prezto-node-cache.$UID.zsh" - - if [[ "$commands[npm]" -nt "$cache_file" \ - || "${ZDOTDIR:-$HOME}/.zpreztorc" -nt "$cache_file" \ - || ! -s "$cache_file" ]]; then - # npm is slow; cache its output. - npm completion >! "$cache_file" 2> /dev/null +# Load NPM and known helper completions. +typeset -A compl_commands=( + npm 'npm completion' + grunt 'grunt --completion=zsh' + gupl 'gulp --completion=zsh' +) + +for compl_command in "${(k)compl_commands[@]}"; do + if (( $+commands[$compl_command] )); then + cache_file="${TMPDIR:-/tmp}/prezto-$compl_command-cache.$UID.zsh" + + # Completion commands are slow; cache their output if old or missing. + if [[ "$commands[$compl_command]" -nt "$cache_file" \ + || "${ZDOTDIR:-$HOME}/.zpreztorc" -nt "$cache_file" \ + || ! -s "$cache_file" ]]; then + command ${=compl_commands[$compl_command]} >! "$cache_file" 2> /dev/null + fi + + source "$cache_file" + + unset cache_file fi +done - source "$cache_file" - - unset cache_file -fi +unset compl_command{s,}