Skip to content

Commit

Permalink
[O] Optimize npm query
Browse files Browse the repository at this point in the history
Closes #246
  • Loading branch information
hykilpikonna committed Apr 13, 2024
1 parent bd9c0b3 commit be8f75d
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions neofetch
Original file line number Diff line number Diff line change
Expand Up @@ -2069,10 +2069,28 @@ get_packages() {
# OS-independent package managers.
has pipx && tot pipx list --short
has cargo && _cargopkgs="$(cargo install --list | grep -v '^ ')" && tot echo "$_cargopkgs"
npm-list() { npm list -g --depth=0 | grep -v -E '.*(\(none\)|npm@).*'; }
has npm && pkgs_h=1 tot npm-list
has am && pac "$(am -f --less)"

# Special case for npm: If has npm, we should list folders under node_modules instead of using npm list.
# This is because npm list is very slow (2s)
if has npm; then
# Try to apply heuristics to find the global directory.
if [[ -d /usr/lib/node_modules ]]; then
dir /usr/lib/node_modules/*/
elif [[ -d /usr/local/lib/node_modules ]]; then
dir /usr/local/lib/node_modules/*/
else
# If neither exist, use npm root -g to get the global directory.
# (still ~10x faster than npm list)
npm_global=$(npm root -g)
[[ -d $npm_global ]] && dir "$npm_global"/*/

# This may not work in WSL2 (if npm is installed on Windows, not WSL).
# However, if npm is not installed on this WSL subsystem, it doesn't really count
# as a package manager for this subsystem, so let's ignore this case.
fi
fi

# OS-specific package managers.
case $os in
Linux|BSD|"iPhone OS"|Solaris|illumos|Interix)
Expand Down

0 comments on commit be8f75d

Please sign in to comment.