Skip to content

Instantly share code, notes, and snippets.

@vrde
Last active July 21, 2020 13:15
Show Gist options
  • Save vrde/e35f70ff8901b1852f4b95d3ed2f4fe0 to your computer and use it in GitHub Desktop.
Save vrde/e35f70ff8901b1852f4b95d3ed2f4fe0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# inotifydo <path> <command>
# Run a command when a file in the path changes.
#
# Examples:
#
# Run my python tests when I change a file in the current path
# inotifydo . "python manage.py test"
#
# Copy my css file from one dir to the other when it changes
# inotifydo public/styles.css "cp public/styles.css build/styles.css"
red=`tput setaf 1`
green=`tput setaf 2`
yellow=`tput setaf 3`
reset=`tput sgr0`
debounce=${DEBOUNCE:-1}
currpid=
hasgit=$(git rev-parse 2> /dev/null ; echo $?)
run() {
echo "Run command ${green}${1}${reset}"
$1
echo
echo "${green}Waiting for changes...${reset}"
}
debounce_and_run() {
kill ${currpid} &> /dev/null
sleep ${debounce}
run "$1"
}
run "$2"
while true
do
path=$(inotifywait -q --exclude .git -e CREATE,MODIFY,DELETE --format %w%f -r $1)
if [ ${hasgit} -eq 0 ]
then
ignore=$(git check-ignore -q ${path} ; echo $?)
if [ ${ignore} -eq 0 ]
then
echo "${yellow}Ignore file ${path}${reset}"
else
echo "${green}Change in file ${path}${reset}"
debounce_and_run "$2" &
currpid=$!
fi
else
echo "${green}Change in file ${path}${reset}"
debounce_and_run "$2" &
currpid=$!
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment