logoDenoby example

Beginner

cli

Watching the filesystem

Edit

When creating frameworks or CLI tools, it is often neccessary to watch the filesystem for changes.

The easiest way to watch a filesystem is to use the Deno builtin watchFs. Deno.watchFs returns an FsWatcher which is an async iterable.
let watcher = Deno.watchFs("./");
The easiest way to interact with async iterables is the javascript for await of syntax.
for await (const event of watcher) {
  console.log(">>>> event", event);
To stop the watcher we can simply call `watcher.close()`
  watcher.close();
}
In real applications, it is quite rare that an application needs to react to every change instantly. Events will be duplicated and multiple events will be dispatched for the same changes. To get around this, we can "debounce" our functions.
import { debounce } from "https://deno.land/std@0.207.0/async/debounce.ts";
In this specific case, we use the standard library to do the work for us. This function will run at most once every two hundred milliseconds
const log = debounce((event: Deno.FsEvent) => {
  console.log("[%s] %s", event.kind, event.paths[0]);
}, 200);

watcher = Deno.watchFs("./");

for await (const event of watcher) {
  log(event);
}

Run this example locally using the Deno CLI:

deno run --allow-read https://examples.deno.land/watching-files.ts