Created
June 22, 2022 13:50
-
-
Save JosiasAurel/dcf0187655e939922b91f4d0d60f8233 to your computer and use it in GitHub Desktop.
A script to watch file changes in your project directory and automatically run commands
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
import subprocess | |
import sys | |
import time | |
import json | |
from watchdog.observers import Observer | |
from watchdog.events import FileSystemEventHandler | |
# load config | |
with open("autobuild.json", "r") as config_file: | |
config = json.loads(config_file.read()) | |
path = sys.argv[1] if len(sys.argv) > 1 else "." | |
class FileEventsHandler(FileSystemEventHandler): | |
def __init__(self): | |
super() | |
def on_any_event(self, _): | |
build_cmd = config.get("run-command", None) | |
run_cmd = config.get("buid-cmd", None) | |
if build_cmd != None: | |
subprocess.run(build_cmd.split(" ")) | |
if run_cmd != None: | |
subprocess.run(run_cmd.split(" ")) | |
return | |
observer = Observer() | |
handler = FileEventsHandler() | |
observer.schedule(event_handler=handler, path=path, recursive=True) | |
observer.start() | |
try: | |
while True: | |
time.sleep(1) | |
except KeyboardInterrupt: | |
observer.stop() | |
observer.join() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment