Skip to content

Instantly share code, notes, and snippets.

@exelotl
Created February 28, 2020 01:00
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 exelotl/2d41ba497a50c29ac54e9c4756be0944 to your computer and use it in GitHub Desktop.
Save exelotl/2d41ba497a50c29ac54e9c4756be0944 to your computer and use it in GitHub Desktop.
NimScript to rebuild JS on save and report errors in the browser.
import strformat, strutils
const main = "dlghelper.nim" # file to watch
const mainHtml = "index.html" # source web page
const outputHtml = "debug.html" # receive the contents of source web page, only if nothing went wrong.
proc sleep(seconds: float) =
exec("sleep {seconds}".fmt)
proc lastModified(file: string): float =
gorge("stat --format=\"%.6Y\" {file}".fmt).strip().parseFloat()
proc errorPage(content: string): string =
fmt"""
<style>body {{ margin: 20px auto; color: #ddd; background: #234 }}</style>
<pre style="white-space: pre-wrap;">{content}</pre>
"""
var modTime: float
while true:
sleep(0.1)
let t = lastModified(main)
if t > modTime:
writeFile(outputHtml, errorPage("BUILDING..."))
modTime = t
let (output, exitCode) = gorgeEx("nim js " & main)
if exitCode == 0:
writeFile(outputHtml, readFile(mainHtml))
else:
writeFile(outputHtml, errorPage(output))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment