Browse Source

npmsearch: global installs

DanielFGray 2 years ago
parent
commit
42f752eaac
1 changed files with 10 additions and 13 deletions
  1. 10
    13
      npmsearch

+ 10
- 13
npmsearch View File

@@ -3,6 +3,7 @@
3 3
 declare -a args
4 4
 declare -a packages
5 5
 declare savedev=0
6
+declare global=0
6 7
 
7 8
 declare -A colors
8 9
 colors[red]=$(tput setaf 1)
@@ -38,7 +39,6 @@ has() {
38 39
   done
39 40
 }
40 41
 
41
-has -v npm fzf || die
42 42
 select_from() {
43 43
   local cmd='command -v'
44 44
   for a; do
@@ -58,39 +58,36 @@ select_from() {
58 58
   return 1
59 59
 }
60 60
 
61
+has -v fzf || die
61 62
 
62 63
 npm=$(select_from npm5 npm) || die 'npm not found'
63
-while true; do
64
-  case "$1" in
65
-    -*) args+=( "$1" ) ; shift ;;
66
-    *) break
67
-  esac
68
-done
69 64
 
70
-printf "searching...\r"
65
+printf 'searching...\r'
71 66
 search=$($npm search --json "$*") || exit
72 67
 
73
-printf "formatting...\r"
74 68
 search=$(jq -r '.[] | "\(.name)|\(.version)|\(.description)"' <<< "$search" | column -t -s'|')
75 69
 
76 70
 mapfile -t packages < <(fzf --inline-info --multi --ansi --reverse \
77 71
   --bind='Ctrl-X:toggle-preview' \
78
-  --expect='Ctrl-d,enter' \
72
+  --expect='Ctrl-g,Ctrl-d,enter' \
79 73
   --preview-window='hidden:down' \
80 74
   --preview="$npm -s --json view {1} | jq -C \"del(.users, .time, .versions)\"" \
81
-  --header='enter to save, Ctrl-D saves as dev-dependency, Ctrl-X for extra info, tab to select multiple' \
75
+  --header='Ctrl-D saves as dev-dependency, Ctrl-G installs globally, Ctrl-X for extra info, tab to select multiple' \
82 76
   <<< "$search" || exit 1)
83 77
 
84 78
 key="${packages[0]}"
85 79
 case "${key,,}" in
86 80
   ctrl-d) savedev=1 ;;
81
+  ctrl-g) global=1 ;;
87 82
 esac
88 83
 
89
-packages=( $(printf '%s\n' "${packages[@]:1}" | cut -d' ' -f1) )
84
+mapfile -t packages < <(printf '%s\n' "${packages[@]:1}" | cut -d' ' -f1)
90 85
 
91 86
 printf "installing...\r"
92 87
 if (( ${#packages[@]} > 0 )); then
93
-  if has yarn; then
88
+  if (( global > 0 )); then
89
+    $npm install -g "${args[@]}" "${packages[@]}"
90
+  elif has yarn; then
94 91
     (( savedev > 0 )) && args+=( -D )
95 92
     yarn add "${args[@]}" "${packages[@]}"
96 93
   else

Loading…
Cancel
Save