Skip to content

Instantly share code, notes, and snippets.

@theodox
Created July 7, 2017 01:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theodox/be3162138dab150fdf9f04458d6078ca to your computer and use it in GitHub Desktop.
Save theodox/be3162138dab150fdf9f04458d6078ca to your computer and use it in GitHub Desktop.
Simple dev server for transcrypt
import sys, os, time
from collections import defaultdict
import re
import subprocess
SERVE_HTTP = False
if SERVE_HTTP:
import http.server
import socketserver
PORT = 8000
Handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print("serving at port", PORT)
httpd.serve_forever()
JS_FOLDER = re.compile('__javascript__', re.I)
if __name__ == '__main__':
print ("starting")
print (sys.argv)
try:
targetfile = sys.argv[1]
except IndexError:
raise SystemExit("Please supply a root file when running this command")
rootdir = os.path.dirname(os.path.abspath(targetfile))
print ("watching {}".format (rootdir))
call_args = ['transcrypt', targetfile] + sys.argv[2:]
status = defaultdict(int)
delay = 2
while True:
modded = False
for root, dirs, files in os.walk(rootdir):
if JS_FOLDER.search(root):
continue
for f in files:
fullname = "{}/{}".format(root, f)
modtime = os.path.getmtime(fullname)
if status[fullname] != modtime:
status[fullname] = modtime
print (fullname)
modded = True
removals = (filename for filename in status.keys() if not os.path.exists(filename))
for r in removals:
del status[r]
modded = True
if modded:
print (call_args)
result = subprocess.run(call_args, shell=True)
print (result.stdout)
delay = 10
else:
delay = 2
time.sleep(delay)
print ("server shutting down")
if SERVE_HTTP:
httpd.shutdown()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment