Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ammmze
Copy link

@ammmze ammmze commented Jan 28, 2020

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 as ls --color=auto or something can cause errors like this when trying to complete an npm install <tab>:

_values:compvalues:11: invalid value definition: ^[[01;34m_locks^[[0m

So this just helps cover edge cases to prevent this from breaking.

@ammmze
Copy link
Author

ammmze commented Jan 28, 2020

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

@ammmze
Copy link
Author

ammmze commented Jan 28, 2020

The npm cache documentation mentions list...

Used to add, list, or clean the npm cache folder.

But npm cache list is not a valid command :\

Edit: Which explains why someone created this

@ammmze ammmze changed the title ensure ls is not colorizing output fix npm install <tab> completion Jan 29, 2020
Comment on lines 44 to 63
_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
Copy link
Author

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.

@ammmze
Copy link
Author

ammmze commented Jan 30, 2020

Sorry...just pushed another update. It now incorporates an npm search to find packages. And it also includes local files in npm install which should resolve #5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants