-
-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix npm install <tab>
completion
#16
base: master
Are you sure you want to change the base?
Conversation
Oh...but actually just realized npm doesn't appear to have a cache of packages there anymore. Hence #9 ... guess we need to see how to get to npm's newer cache stuff |
The npm cache documentation mentions
But Edit: Which explains why someone created this |
npm install <tab>
completion
zsh-better-npm-completion.plugin.zsh
Outdated
_zbnc_list_cached_modules_no_cache() { | ||
local cache_dir="$(npm config get cache)/_cacache" | ||
export NODE_PATH="${NODE_PATH}:$(npm prefix -g)/lib/node_modules" | ||
node --eval="require('cacache');" &>/dev/null || npm install -g cacache &>/dev/null | ||
if [ -d "${cache_dir}" ]; then | ||
node <<CACHE_LS 2>/dev/null | ||
const cacache = require('cacache'); | ||
cacache.ls('${cache_dir}').then(cache => { | ||
const packages = Object.values(cache).forEach(entry => { | ||
const id = ((entry || {}).metadata || {}).id; | ||
if (id) { | ||
console.log(id.substr(0, id.lastIndexOf('@'))); | ||
} | ||
}); | ||
}); | ||
CACHE_LS | ||
else | ||
# Fallback to older cache location ... i think node < 10 | ||
ls --color=never ~/.npm 2>/dev/null | ||
fi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure the structure of the cache directory before, but at some point i'd like to see if we can actually search via npm (i.e. npm search --parseable @babel | awk '{ print $1 }'
) rather than using the local cache. This way we'd get completion even for packages we've never installed. But i'd like to see how we can incorporate the zsh cache stuff into that.
And looks like we'd only be able to search with 3+ characters. So any tab completion below that would have to come from cache.
Sorry...just pushed another update. It now incorporates an |
Update:
I'm not sure when npm switched their cache storage, but it's not as simple as just loading a file list anymore. And npm doesn't expose a way directly to get the cache list, so I had to put a little script together that uses the
cacache
library that the npm cache uses under the hood to get a package list.It's not the quickest thing in the world. I just finished setting up a new machine from scratch, so my cache is pretty small and it still takes several seconds to get that list. So I also added the zsh caching mechanism to cache the package list.
This should resolve #9.
Update #2:
Perform
npm search
for packages to install.Include local files to resolve #5
Original:
in scenarios where people have
ls
aliased asls --color=auto
or something can cause errors like this when trying to complete annpm install <tab>
:So this just helps cover edge cases to prevent this from breaking.