Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

[PATCH] Prevent npm completion from spamming update notifications



Newer versions of npm (and maybe older ones? but i never encountered it until
now) check for updates whenever you run literally any sub-command, including the
one that handles completion, and if it thinks it has something important to tell
you about that it spams the screen with colourful ASCII-art boxes of nonsense

Fortunately the spam only makes its way to the screen once per session, since
every time after that _npm_completion redirects it to /dev/null. But the npm
call in _npm_completion is still wasting time on the update check/notification
even when it's not showing the result. So i offer two possible fixes:

1. A conservative one that suppresses the visible spam but leaves the time-
   wasting checks

2. A more complete but possibly questionable one that monkey-patches their
   _npm_completion to avoid the time-wasting checks

Is the second one too weird to ship with the shell?

(Both patches also change the type call to a $commands check)

dana


### CONSERVATIVE PATCH ###
diff --git a/Completion/Unix/Command/_npm b/Completion/Unix/Command/_npm
index f5493a321..d069fc107 100644
--- a/Completion/Unix/Command/_npm
+++ b/Completion/Unix/Command/_npm
@@ -2,8 +2,8 @@
 
 # Node Package Manager completion, letting npm do all the completion work
 
-if type npm > /dev/null; then
-  eval "$(npm completion)"
+if (( $+commands[npm] )); then
+  eval "$(NPM_CONFIG_UPDATE_NOTIFIER=false npm completion)"
 
   _npm_completion "$@"
 fi


### FUNNY PATCH ####
diff --git a/Completion/Unix/Command/_npm b/Completion/Unix/Command/_npm
index f5493a321..c05f61c51 100644
--- a/Completion/Unix/Command/_npm
+++ b/Completion/Unix/Command/_npm
@@ -2,8 +2,13 @@
 
 # Node Package Manager completion, letting npm do all the completion work
 
-if type npm > /dev/null; then
-  eval "$(npm completion)"
+if (( $+commands[npm] )); then
+  eval "$(NPM_CONFIG_UPDATE_NOTIFIER=false npm completion)"
+  # Monkey-patch their function to prevent update checks
+  functions[_npm_completion]="
+    local -x NPM_CONFIG_UPDATE_NOTIFIER=false;
+    ${functions[_npm_completion]}
+  "
 
   _npm_completion "$@"
 fi




Messages sorted by: Reverse Date, Date, Thread, Author