From be8f75df019f6b975e77d30a81c1211ac4292be4 Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Sat, 13 Apr 2024 19:54:28 -0400 Subject: [PATCH] [O] Optimize npm query Closes https://github.com/hykilpikonna/hyfetch/issues/246 --- neofetch | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/neofetch b/neofetch index 10e9ad70..ab012efd 100755 --- a/neofetch +++ b/neofetch @@ -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)