From 331a08620efc235506262d4f13b5407e566c73ed Mon Sep 17 00:00:00 2001 From: Sebastian Seedorf Date: Mon, 20 Apr 2020 18:19:42 +0200 Subject: [PATCH] Works, easy :D --- README.md | 12 ++++++++++++ mod.ts | 23 +++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 README.md create mode 100644 mod.ts diff --git a/README.md b/README.md new file mode 100644 index 0000000..e920fc9 --- /dev/null +++ b/README.md @@ -0,0 +1,12 @@ +# Rhinoder + +This program reloads a deno program automatically when a file in the current working directory is created, deleted or modified. + +## Usage + +the following code runs `deno --allow-net example.ts` which is located in `/some/work/dir/example.ts` on start an whenever a file in the folder `/some/work/dir` changes. + + +```bash +/some/work/dir$ deno --allow-run --allow-read https://raw.githubusercontent.com/Caesar2011/rhinoder/master/mod.ts --allow-net example.ts +``` \ No newline at end of file diff --git a/mod.ts b/mod.ts new file mode 100644 index 0000000..3f479fb --- /dev/null +++ b/mod.ts @@ -0,0 +1,23 @@ +function startProcess(args: string[] = []): Deno.Process { + return Deno.run({ cmd: ['deno', ...args] }); +} + +const throttle = 500; +let app: Deno.Process = startProcess(Deno.args); +let appInitTime: number = Date.now(); +let timeout: number|null = null; + +function runApp() { + appInitTime = Date.now(); + app && app.close(); + app = startProcess(Deno.args); +} + +runApp(); + +for await (const event of Deno.fsEvents('.')) { + if (event.kind !== "access") { + if (timeout) clearTimeout(timeout); + timeout = setTimeout(runApp, throttle); + } +} \ No newline at end of file