Categories
Deno

Deno — OS Signals, File System Events, and Module Metadata

Spread the love

Deno is a new server-side runtime environment for running JavaScript and TypeScript apps.

In this article, we’ll take a look at how to get started with developing apps for Deno.

Handle OS Signals

We can handle signals with the Deno.signal method.

For example, we can write:

index.ts

console.log("Press Ctrl-C");
for await (const _ of Deno.signal(Deno.Signal.SIGINT)) {
  console.log("interrupted!");
  Deno.exit();
}

If we press Ctrl+C, we’ll trigger the sigint signal to interrupt the program.

We watch for the signal with the for-await-of loop.

Deno.Signal.SIGINT is the object for the sigint signal.

We call Deno.exit to exit the program.

We run the program by running:

deno run --unstable index.ts

Also, we can write it as a promise.

For instance, we can write:

index.ts

console.log("Press Ctrl-C to end the program");
await Deno.signal(Deno.Signal.SIGINT);
console.log("interrupted!");
Deno.exit();

to watch for the sigint signal.

Stop Watching Signals

We can stop watching signals by calling the sig.dispose method.

For example, we can write:

index.ts

const sig = Deno.signal(Deno.Signal.SIGINT);
setTimeout(() => {
  sig.dispose();
  console.log("No longer watching SIGINT signal");
}, 5000);

console.log("Watching SIGINT signals");
for await (const _ of sig) {
  console.log("interrupted");
}

In the setTimeout callback. we call the sig.dispose method to stop watching the sigint signal.

The for-await-of loop exits after 5 seconds when sig.dispose is called.

File System Events

We can watch for file system events with the Deno.watchFs method.

For example, we can write:

index.ts

const watcher = Deno.watchFs(".");
for await (const event of watcher) {
  console.log(event);
}

We watch the folder the script is in with Deno.watchFs .

We get the event from the event object.

Then we something like:

index.ts

{ kind: "create", paths: [ "/home/runner/IntelligentWorthwhileMice/./lock.json" ] }
{ kind: "modify", paths: [ "/home/runner/IntelligentWorthwhileMice/./lock.json" ] }
{ kind: "access", paths: [ "/home/runner/IntelligentWorthwhileMice/./lock.json" ] }
{
  kind: "create",
  paths: [ "/home/runner/IntelligentWorthwhileMice/./.3s18gah600nwj.foo.txt~" ]
}
{
  kind: "access",
  paths: [ "/home/runner/IntelligentWorthwhileMice/./.3s18gah600nwj.foo.txt~" ]
}
{
  kind: "modify",
  paths: [ "/home/runner/IntelligentWorthwhileMice/./.3s18gah600nwj.foo.txt~" ]
}
{ kind: "modify", paths: [ "/home/runner/IntelligentWorthwhileMice/./foo.txt" ] }
{
  kind: "modify",
  paths: [
    "/home/runner/IntelligentWorthwhileMice/./.3s18gah600nwj.foo.txt~",
    "/home/runner/IntelligentWorthwhileMice/./foo.txt"
  ]
}

displayed.

Then we can run it with:

deno run --allow-read index.ts

to watch for any file system events in the folder.

Module Metadata

We can get module meta with the import.meta property.

For example, we can write:

console.log(import.meta.url);
console.log(Deno.mainModule);
console.log(import.meta.main);

Then we see something like:

file:///home/runner/IntelligentWorthwhileMice/index.ts
file:///home/runner/IntelligentWorthwhileMice/index.ts
true

from the console output when we run:

deno run --allow-read --unstable index.ts

import.meta.url and Deno.mainModule both get the path of the module.

And import.meta.main returns true if it’s the entry point module.

Conclusion

We can handle OS signals, get module metadata, and watch file system events with Deno.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *