From 20c183910dcf73e520caa366528f648434e98b00 Mon Sep 17 00:00:00 2001 From: Florian Schroedl Date: Wed, 4 May 2022 17:00:00 +0200 Subject: [PATCH] Watch --- shell.nix | 1 + src/js/watch.nim | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/js/watch.nim diff --git a/shell.nix b/shell.nix index 7ae19f3..390f427 100644 --- a/shell.nix +++ b/shell.nix @@ -10,5 +10,6 @@ pkgs.mkShell { buildInputs = with pkgs; buildInputs ++ [ nim nimlsp + fswatch ]; } diff --git a/src/js/watch.nim b/src/js/watch.nim new file mode 100644 index 0000000..ac98002 --- /dev/null +++ b/src/js/watch.nim @@ -0,0 +1,54 @@ +import times, posix, strutils, os +import strformat +import times +import libfswatch +import std/[streams, osproc] + +import libfswatch +import libfswatch/fswatch + +let file = "/home/floscr/Code/Projects/org-parser/src/js/example/sandbox.nim" + +var ps: seq[Process] + +proc compile(path: string) = + echo "Compiling..." + let p = startProcess( + "nim", + args= [ + "js", + "--verbosity:0", + "--hint[Processing]:off", + "--hint[XDeclaredButNotUsed]:off", + "--hint[DuplicateModuleImport]:off", + "--excessiveStackTrace:on", + "--warning[UnusedImport]:off", + path, + ], + options = {poUsePath, poStdErrToStdOut} + ) + ps.add(p) + discard p.waitForExit() + + var line: string + while p.running() and p.outputStream.readLine(line): + echo line + + echo "Compilation Finished" + p.close() + ps = @[] + +proc monitorCallback(event: fsw_cevent, event_num: cuint) = + echo event_num + if event_num == 10: + echo &"""[{times.now().format("yyyy-MM-dd HH:MM:ss")}]: {event.path} was modified. Compiling...""" + for p in ps: + p.close() + ps = @[] + + compile(file) + +var mon = newMonitor() +mon.addPath(file) +mon.setCallback(monitorCallback) +mon.start()