Skip to content

Instantly share code, notes, and snippets.

@DrJume
Last active October 29, 2021 07:53
Show Gist options
  • Save DrJume/93010bebda4eaffc24abd644a0086040 to your computer and use it in GitHub Desktop.
Save DrJume/93010bebda4eaffc24abd644a0086040 to your computer and use it in GitHub Desktop.
Interactive update script
#!/bin/bash
last_check=''
skip_next_confirm="false"
function skipNextConfirm {
skip_next_confirm="true"
return 0
}
# check if command outputs something
function check {
echo "-> $*"
local output="$(script -q /dev/null $@ 2>&1 | tee /dev/tty)"
last_check="$output"
if [ -z "$output" ]
then return 1 # output is empty, return false
else echo && return 0 # output is of non-zero length, return true
fi
}
# prompt the user to run the command
function confirm {
local command="-> $*"
if [ "$skip_next_confirm" = "true" ]; then
skip_next_confirm="false"
echo "$command (skipped)"
return 1
fi
read -r -n 1 -p "$command [Y/n] " response
[[ ! "$response" = "" ]] && echo
[[ ! "$response" =~ ^[yY]?$ ]] && return 1
$@
}
if check npm -g outdated; then
! (echo "$last_check" | grep '\<npm\>' > /dev/null) && skipNextConfirm
confirm npm -g i npm
confirm npm -g update
fi
echo
if check pnpm -g outdated --long --no-table; then
! (echo "$last_check" | grep '\<pnpm\>' > /dev/null) && skipNextConfirm
confirm pnpm -g i pnpm
confirm pnpm -g update --interactive
fi
echo
if confirm brew update; then
if check brew outdated; then
confirm brew upgrade
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment