when-changed: Run a command when a file is changed
What is it?
Tired of switching to the shell to test the changes you just made to
your code? Starting to feel like a mindless drone, manually running
pdflatex for the 30th time to see how your resume now looks?
Worry not, when-changed is here to help! Whenever it sees that you have
changed the file, when-changed runs any command you specify.
So to generate your latex resume automatically, you can do this:
$ when-changed CV.tex pdflatex CV.texSweetness!
What do I need?
- Python 3.6+
- watchdog
Installation
pipx install https://github.com/eviweb/when-changed/archive/master.zipUsage
when-changed [OPTION] FILE COMMAND...
when-changed [OPTION] FILE [FILE ...] -c COMMANDFILE can be a directory. Use %f to pass the filename to the command.
Options:
-rWatch recursively-vVerbose output. Multiple -v options increase the verbosity. The maximum is 3:-vvv.-1Don't re-run command if files changed while command was running-sRun command immediately at start-qRun command quietly-kKill the running command before restarting it (useful for long-running processes)-d DELAYDebounce: wait DELAY seconds before running, coalescing rapid changes into a single run-p PATTERNOnly react to files matching PATTERN (glob, e.g.*.py). Can be repeated for multiple patterns.
Environment variables
when-changed provides the following environment variables:
WHEN_CHANGED_EVENT: reflects the current event type that occurs. Could be either:file_createdfile_modifiedfile_movedfile_deleted
WHEN_CHANGED_FILE: provides the full path of the file that has generated the event.
Examples
Restart a server when source files change, killing the previous instance:
$ when-changed -k -r src/ python server.pyDebounce rapid changes (e.g. formatter touching many files at once):
$ when-changed -d 0.5 -r src/ make testWatch only Python and YAML files in a directory:
$ when-changed -r -p '*.py' -p '*.yml' src/ make lint