Last active
November 13, 2021 00:06
-
-
Save gnowland/fbe758bb9730195f138d5259aeeb2d79 to your computer and use it in GitHub Desktop.
Use interactive menu to pass SemVer target to npm-version command
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Portions of this script are Copyright (C) 2017 Ingo Hollmann - All Rights Reserved | |
# Permission to copy and modify is granted under the Creative Commons Attribution 4.0 license | |
# https://www.bughunter2k.de/blog/cursor-controlled-selectmenu-in-bash | |
# Last revised 2020-11-12 by Gifford Nolwland [https://giffordnowland.com] | |
ESC=$(printf "\e") | |
curr=0 | |
declare -a target=("patch" "minor" "major") # mac does not support associative arrays | |
declare -a menu=("Patch (x.x.🁢)" "Minor (x.🁢.x)" "Major (🁢.x.x)") | |
draw_menu() { | |
for i in "${menu[@]}"; do | |
if [[ ${menu[$curr]} == $i ]]; then | |
tput setaf 2; echo " > $i"; tput sgr0 | |
else | |
echo " $i"; | |
fi | |
done | |
} | |
clear_menu() { | |
for i in "${menu[@]}"; do tput cuu1; done | |
tput ed | |
} | |
echo "Choose SemVer target:" | |
# Draw initial menu | |
draw_menu | |
while read -rs -n1 key; do # 1 char (not delimiter), silent | |
# empty echo simulates <enter> which loads the input into the variable (due to lack of automatic echo including new line from read -s) | |
# see: https://www.tutorialkart.com/bash-shell-scripting/bash-read-username-and-password/ | |
# echo | |
# echo $key | |
if [[ "$key" == "" ]]; then break; fi # accept choice on enter or space | |
# match input keys | |
# see: https://stackoverflow.com/questions/10679188/casing-arrow-keys-in-bash/11759139 | |
if [[ "$key" == $ESC ]]; then # an escape sequence was pressed | |
read -rs -n2 -t1 key; # read 2 more characters, silent. Abort if no characters read after 1 second (mac minimum) | |
case "$key" in | |
'[A'|'0A'|'[D'|'0D') # cursor up or left: previous item | |
((curr > 0)) && ((curr--)) | |
;; | |
'[B'|'0B'|'[C'|'0C') # cursor down or right: next item | |
((curr < ${#menu[@]}-1)) && ((curr++)) | |
;; | |
$'[1~'|$'[H'|$'0H') # home: first item | |
((curr=0)) | |
;; | |
$'[4~'|$'[F'|$'0F') # end: last item | |
((curr=${#menu[@]}-1)) | |
;; | |
*) # esc key pressed, abort | |
clear_menu | |
echo "Aborted!" | |
exit 1 | |
;; | |
esac | |
fi | |
# Redraw menu | |
clear_menu | |
draw_menu | |
done | |
# echo "Selected item $curr: [${target[$curr]}] \"${menu[$curr]}\""; | |
echo ${target[$curr]} > /tmp/semver # send out | |
clear_menu | |
tput setaf 2; echo " > ${menu[$curr]}"; tput sgr0 | |
echo # add a new line after final output | |
exit 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
... | |
"scripts": { | |
"deploy": "./bump.sh && npm version $(cat /tmp/semver) && rm /tmp/semver;", | |
"postversion": "git push && git push --tags", | |
... | |
}, | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment