Browse Source

npmsearch can install specific versions

DanielFGray 1 year ago
parent
commit
4d339118a1
1 changed files with 26 additions and 13 deletions
  1. 26
    13
      npmsearch

+ 26
- 13
npmsearch View File

@@ -58,35 +58,48 @@ select_from() {
58 58
   return 1
59 59
 }
60 60
 
61
-has -v fzf || die
61
+has -v fzf npm || die
62 62
 
63
-npm=$(select_from npm5 npm) || die 'npm not found'
63
+fzf() {
64
+  command fzf --inline-info --ansi --reverse +s --no-hscroll "$@"
65
+}
64 66
 
65 67
 printf 'searching...\r'
66
-search=$($npm search --json "$*") || exit
68
+search=$(npm search --json "$*") || exit
67 69
 
68 70
 search=$(jq -r '.[] | "\(.name)|\(.version)|\(.description)"' <<< "$search" | column -t -s'|')
69 71
 
70
-mapfile -t packages < <(fzf --inline-info --multi --ansi --reverse \
71
-  --bind='Ctrl-X:toggle-preview' \
72
-  --expect='Ctrl-g,Ctrl-d,enter' \
72
+mapfile -t packages < <(fzf --multi \
73
+  --bind='ctrl-x:toggle-preview' \
74
+  --expect='ctrl-g,ctrl-d,enter,ctrl-v' \
73 75
   --preview-window='hidden:down' \
74
-  --preview="$npm -s --json view {1} | jq -C \"del(.users, .time, .versions)\"" \
75
-  --header='Ctrl-D saves as dev-dependency, Ctrl-G installs globally, Ctrl-X for extra info, tab to select multiple' \
76
+  --preview="npm -s --json view {1} | jq -C \"del(.users, .time, .versions)\"" \
77
+  --header='C-d saves as devdependency, C-g installs globally, C-v for specific version, C-x for more info, ' \
76 78
   <<< "$search" || exit 1)
77 79
 
78 80
 key="${packages[0]}"
79 81
 case "${key,,}" in
80 82
   ctrl-d) savedev=1 ;;
81 83
   ctrl-g) global=1 ;;
84
+  ctrl-v)
85
+    package="${packages[1]%% *}"
86
+    version=$(
87
+      npm -s --json view "$package" |
88
+        jq -r '.versions[]' |  # label with .dist-tags?
89
+        fzf --header="choose version for $package" --tac
90
+    )
91
+    packages=( "${package}@${version}" )
92
+    ;;
82 93
 esac
83 94
 
84
-mapfile -t packages < <(printf '%s\n' "${packages[@]:1}" | cut -d' ' -f1)
85
-
86
-printf "installing...\r"
87 95
 if (( ${#packages[@]} > 0 )); then
96
+  if (( ${#packages[@]} > 1 )); then
97
+    packages=( "${packages[@]:1}" )
98
+    packages=( "${packages[@]%% *}" )
99
+  fi
100
+  printf "installing...\r"
88 101
   if (( global > 0 )); then
89
-    $npm install -g "${args[@]}" "${packages[@]}"
102
+    npm install -g "${args[@]}" "${packages[@]}"
90 103
   elif has yarn; then
91 104
     (( savedev > 0 )) && args+=( -D )
92 105
     yarn add "${args[@]}" "${packages[@]}"
@@ -96,7 +109,7 @@ if (( ${#packages[@]} > 0 )); then
96 109
     else
97 110
       args+=( -S )
98 111
     fi
99
-    $npm i "${args[@]}" "${packages[@]}"
112
+    npm i "${args[@]}" "${packages[@]}"
100 113
   fi
101 114
 else
102 115
   exit 1

Loading…
Cancel
Save