Browse Source

npmsearch: npm5 support

DanielFGray 2 years ago
parent
commit
23d62b2deb
1 changed files with 23 additions and 3 deletions
  1. 23
    3
      npmsearch

+ 23
- 3
npmsearch View File

@@ -39,7 +39,27 @@ has() {
39 39
 }
40 40
 
41 41
 has -v npm fzf || die
42
+select_from() {
43
+  local cmd='command -v'
44
+  for a; do
45
+    case "$a" in
46
+      -c)
47
+        cmd="$2"
48
+        shift 2
49
+        ;;
50
+    esac
51
+  done
52
+  for c; do
53
+    if $cmd "${c%% *}" &> /dev/null; then
54
+      echo "$c"
55
+      return 0
56
+    fi
57
+  done
58
+  return 1
59
+}
60
+
42 61
 
62
+npm=$(select_from npm5 npm) || die 'npm not found'
43 63
 while true; do
44 64
   case "$1" in
45 65
     -*) args+=( "$1" ) ; shift ;;
@@ -48,7 +68,7 @@ while true; do
48 68
 done
49 69
 
50 70
 printf "searching...\r"
51
-search=$(npm search --json "$*") || exit
71
+search=$($npm search --json "$*") || exit
52 72
 
53 73
 printf "formatting...\r"
54 74
 search=$(jq -r '.[] | "\(.name)|\(.version)|\(.description)"' <<< "$search" | column -t -s'|')
@@ -57,7 +77,7 @@ mapfile -t packages < <(fzf --inline-info --multi --ansi --reverse \
57 77
   --bind='Ctrl-X:toggle-preview' \
58 78
   --expect='Ctrl-d,enter' \
59 79
   --preview-window='hidden:down' \
60
-  --preview='npm view --json {1} | jq -C "del(.users, .time, .versions)"' \
80
+  --preview="$npm -s --json view {1} | jq -C \"del(.users, .time, .versions)\"" \
61 81
   --header='enter to save, Ctrl-D saves as dev-dependency, Ctrl-X for extra info, tab to select multiple' \
62 82
   <<< "$search" || exit 1)
63 83
 
@@ -79,7 +99,7 @@ if (( ${#packages[@]} > 0 )); then
79 99
     else
80 100
       args+=( -S )
81 101
     fi
82
-    npm i "${args[@]}" "${packages[@]}"
102
+    $npm i "${args[@]}" "${packages[@]}"
83 103
   fi
84 104
 else
85 105
   exit 1

Loading…
Cancel
Save