home ~ projects ~ socials

Use watchexec To Run Commands When Files Change

This top one is my working sample as of May 2024 (but it still seems flaky a little)

#!/bin/bash

watchexec\
  --project-origin "../cli"\
  -w "../cli"\
  --workdir "../cli"\
  -e rs\
  "bash -c 'cargo test -- --nocapture && cargo run'"

Sometimes you can't go all the way into a directory? which is weird, I had to use this apprahc instead of setting the origin for example-site in the following

#!/bin/bash

watchexec\
  --project-origin "../cli"\
  -w "../cli"\
  --workdir "../cli/example-site"\
  -e neoj\
  "rsync -av ./ ~/Documents/Neopoligen/v0.1.0-dev/"
cd /Users/alan/Downloads
watchexec -p --filter "ascii-shop.json" --fs-events create mv -- ascii-shop.json "/Users/alan/workshop/asciibear/html/bears.json"

TODO: Add osascript for display to notification center

watchexec -w /some/path --emit-events-to stdin echo
watchexec -w /some/path --emit-events-to json-stdin jq

Notes

  • --debounce is an option. Defaults to 50m
  • --project-origin need to use when you're not in the current dir
  • -w what to watch. . will watch the entire dir. TBD if that's recursive
  • --no-meta which prevents events for firing for metadata changes and only fires them for: 'create', 'remove', 'rename', 'modify',
  • --emit-events-to see docs for now
-- end of line --

References