Created
June 23, 2020 14:11
-
-
Save ikouchiha47/f1875785dd6f4ab87e037805531bf46b to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python3 | |
import os | |
import sys | |
import time | |
import subprocess | |
## usage | |
## ./hot-reload.sh [directories] <command> | |
## Example: ./hot-reload.sh dir1 dir2 dir3 'make' | |
sources = sys.argv[1:] | |
command = sources.pop() | |
proc = None | |
flatten = lambda lists: [item for sublist in lists for item in sublist] | |
retrive_files = lambda source: [os.path.join(root, file) for root, _, files in os.walk(source) for file in files] | |
filelists = lambda sources: [retrive_files(source) for source in sources] | |
transform_to_map = lambda _files: {file: os.path.getctime(file) for file in _files} | |
_files = flatten(filelists(sources)) | |
_files_stamps = transform_to_map(_files) | |
def run_command(command): | |
if proc: | |
proc.kill() | |
time.sleep(2) | |
return subprocess.Popen(command, shell=True) | |
proc = run_command(command) | |
while True: | |
time.sleep(2) | |
# see if there are new files added | |
changed = 0 | |
for file in _files_stamps: | |
mtime = os.path.getctime(file) | |
prv_mtime = _files_stamps[file] | |
if prv_mtime != mtime: | |
_files_stamps[file] = mtime | |
changed = 1 | |
break | |
if changed == 1: | |
print("file change detected") | |
proc = run_command(command) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment