Effection Logo
@effection-contrib/watchv0.1.1thefrontside/effection-contrib
JSR BadgeNPM Badge with published versionBundle size badgeDependency count badgeTree shaking support badge
import { } from "@effection-contrib/watch/lib"

Watch

Watch is a very simple tool that does one thing: run a command, and every time source files change in a directory, shutdown the current invocation gracefully and restart it.

deno -A jsr:@effection-contrib/watch npm start

Graceful Shutdown

Watch will send SIGINT and SIGTERM to your command, and then wait until its stdout stream is closed, indicating that it has no further output. It will not attempt to start your command again until that has happened. This is important, because your process might be holding onto any number of resources that have to be safely released before exiting.

Git aware

If you are running this command inside a git repository, it will only perform restarts on files that are under source control, or could be candidates for source control (not ignored).

Use it as an Effection library

Most of the time, you will use this an executable. However, if you want to create your own watch from within a library, you can But if you want to write your own

import { each, main } from "effection";
import { watch } from "@effection-contrib/watch";

await main(function* () {
  const changes = watch({
    path: "./src",
    cmd: "npm test",
  });

  for (let start of yield* each(changes)) {
    console.log(start);
    yield* each.next();
  }
});

API Reference

./lib

interface Start

interface Watch extends Stream<Result<Process>, never>

A watch is a strema of process starts that happen in reaction to some source file changing on the file system.

interface WatchOptions

Options available to configure what is watched

Properties

path: string

The directory to watch

cmd: string

The command to run (and re-run every time a change is detected)

function watch(options: WatchOptions): Stream<Start, never>

Create a watch configuration that observes file system changes and executes a command when changes are detected. The watch can be consumed as a stream of process starts.

Examples

Example 1
const watcher = yield* watch({
 path: './src',
 cmd: 'npm test'
});

Parameters

options: WatchOptions

  • Configuration options for the watch

Return Type

Stream<Start, never>

A Stream that emits Result<Process> for each command execution

See

  • WatchOptions for configuration options
  • Process for process execution details