Browse Source

better quit handling

DanielFGray 1 year ago
parent
commit
e1ec7f877a
1 changed files with 29 additions and 25 deletions
  1. 29
    25
      npmsearch

+ 29
- 25
npmsearch View File

@@ -71,14 +71,15 @@ search=$(jq -r '.[] | "\(.name)|\(.version)|\(.description)"' <<< "$search" | co
71 71
 
72 72
 mapfile -t packages < <(fzf --multi \
73 73
   --bind='ctrl-x:toggle-preview' \
74
-  --expect='ctrl-g,ctrl-d,enter,ctrl-v' \
74
+  --expect='ctrl-g,ctrl-d,enter,ctrl-v,esc' \
75 75
   --preview-window='hidden:down' \
76 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, ' \
78
-  <<< "$search" || exit 1)
77
+  --header='C-d saves as devDependency, C-g installs globally, C-v for specific version, C-x for more info, ' \
78
+  <<< "$search")
79 79
 
80 80
 key="${packages[0]}"
81 81
 case "${key,,}" in
82
+  esc) die 'cancelled' ;;
82 83
   ctrl-d) savedev=1 ;;
83 84
   ctrl-g) global=1 ;;
84 85
   ctrl-v)
@@ -86,31 +87,34 @@ case "${key,,}" in
86 87
     version=$(
87 88
       npm -s --json view "$package" |
88 89
         jq -r '.versions[]' |  # label with .dist-tags?
89
-        fzf --header="choose version for $package" --tac
90
-    )
91
-    packages=( "${package}@${version}" )
90
+        fzf --header="choose version for $package  |  C-d saves as devDependency" --tac --expect='ctrl-d,esc')
91
+    key="${version[0]}"
92
+    case "${key,,}" in
93
+      esc) die 'cancelled' ;;
94
+      ctrl-d) savedev=1 ;;
95
+    esac
96
+    packages=( "${package}@${version[1]}" )
92 97
     ;;
93 98
 esac
94 99
 
95
-if (( ${#packages[@]} > 0 )); then
96
-  if (( ${#packages[@]} > 1 )); then
97
-    packages=( "${packages[@]:1}" )
98
-    packages=( "${packages[@]%% *}" )
99
-  fi
100
-  printf "installing...\r"
101
-  if (( global > 0 )); then
102
-    npm install -g "${args[@]}" "${packages[@]}"
103
-  elif has yarn; then
104
-    (( savedev > 0 )) && args+=( -D )
105
-    yarn add "${args[@]}" "${packages[@]}"
100
+(( ${#packages[@]} > 0 )) || die 'no packages to install'
101
+
102
+if (( ${#packages[@]} > 1 )); then
103
+  packages=( "${packages[@]:1}" )
104
+  packages=( "${packages[@]%% *}" )
105
+fi
106
+
107
+printf "installing...\r"
108
+if (( global > 0 )); then
109
+  npm install -g "${args[@]}" "${packages[@]}"
110
+elif has yarn; then
111
+  (( savedev > 0 )) && args+=( -D )
112
+  yarn add "${args[@]}" "${packages[@]}"
113
+else
114
+  if (( savedev > 0 )); then
115
+    args+=( -D )
106 116
   else
107
-    if (( savedev > 0 )); then
108
-      args+=( -D )
109
-    else
110
-      args+=( -S )
111
-    fi
112
-    npm i "${args[@]}" "${packages[@]}"
117
+    args+=( -S )
113 118
   fi
114
-else
115
-  exit 1
119
+  npm i "${args[@]}" "${packages[@]}"
116 120
 fi

Loading…
Cancel
Save