An open API service indexing awesome lists of open source software.

https://github.com/tmc/watcher

watcher is a simple filesystem event watcher
https://github.com/tmc/watcher

Last synced: 11 months ago
JSON representation

watcher is a simple filesystem event watcher

Awesome Lists containing this project

README

          

# watcher

[![Go Reference](https://pkg.go.dev/badge/github.com/tmc/watcher.svg)](https://pkg.go.dev/github.com/tmc/watcher)

Command watcher is a file watcher that executes a command when files change.

It can be used to automatically run tests, build code, or any other command when files in a directory change.
## Installation

Prerequisites: Go Installation

You'll need Go 1.23 or later. [Install Go](https://go.dev/doc/install) if you haven't already.

Setting up your PATH

After installing Go, ensure that `$HOME/go/bin` is in your PATH:

For bash users

Add to `~/.bashrc` or `~/.bash_profile`:
```bash
export PATH="$PATH:$HOME/go/bin"
```

Then reload your configuration:
```bash
source ~/.bashrc
```

For zsh users

Add to `~/.zshrc`:
```bash
export PATH="$PATH:$HOME/go/bin"
```

Then reload your configuration:
```bash
source ~/.zshrc
```

### Install

```console
go install github.com/tmc/watcher@latest
```

### Run directly

```console
go run github.com/tmc/watcher@latest [arguments]
```

## Usage:

watcher [flags] [command to execute and args]

-c clear terminal before each run
-depth int
recursion depth (default 1)
-dir string
directory root to use for watching (default ".")
-ignore string
comma-separated list of glob patterns to ignore
-quiet duration
quiet period after command execution (default 800ms)
-v verbose
-wait duration
time to wait between change detection and exec (default 10ms)

## Features

- Watch directories recursively with configurable depth
- Ignore files based on glob patterns (e.g., \`-ignore "\*.tmp,\*.log"\`)
- Automatically detects and watches newly created directories
- Graceful termination on SIGINT/SIGTERM
- Configurable quiet period to avoid rapid execution
- Configurable wait time between file change and command execution

## Common Use Cases

- Run tests when source files change
- Trigger build steps in development
- Refresh browsers or servers
- Auto-compile code during development

## Example Use:

In Shell A:

```console

$ go install github.com/tmc/watcher@latest
$ mkdir /tmp/foo; cd /tmp/foo
$ watcher -v echo "triggered"
running echo triggered
triggered

```

Now, In Shell B:

```console

$ touch /tmp/foo/oi

```

Every time /tmp/foo changes the echo will be re-executed.

License: ISC