Categorygithub.com/niradler/go-watch
repositorypackage
0.0.9
Repository: https://github.com/niradler/go-watch.git
Documentation: pkg.go.dev

# Packages

No description provided by the author

# README

go-watch

A lightweight, fast, and configurable file watcher for Go, inspired by nodemon. It automatically watches for changes in source files and restarts processes or executes commands when changes are detected. It also supports live reload for frontend workflows.

Features

  • High Performance: Uses fsnotify for efficient file system event handling.
  • Cross-Platform: Works on Windows, macOS, and Linux.
  • Advanced Watch Capabilities: Supports multiple extensions and ignores specified directories.
  • Live Reload: WebSocket-based live reload for frontend workflows.
  • Configuration File: Supports JSON or YAML configuration files.
  • Debounce Mechanism: Prevents rapid restarts.

Installation

go install github.com/niradler/go-watch@latest

without go, you can use the binaries

curl -fsSL https://raw.githubusercontent.com/niradler/go-watch/master/scripts/install.sh -o install.sh && bash install.sh

or for windows:

Invoke-WebRequest -Uri "https://raw.githubusercontent.com/niradler/go-watch/master/scripts/install.ps1" -OutFile install.ps1; .\install.ps1

Usage

Basic Usage (CLI Arguments)

Watch .go and .js files, ignore node_modules and .git directories, and run go run main.go on file changes:

go-watch --ext go,js --ignore node_modules,.git --cmd "go run main.go"

Live Reload

Enable live reload for frontend workflows on port 35729:

go-watch --live-reload --live-reload-port 35729

Configuration File

Create a go-watch.config.json or go-watch.config.yaml file for more advanced configurations.

JSON Example (go-watch.config.json)

{
  "watch_dirs": [".", "src"],
  "ignore_dirs": ["node_modules", ".git"],
  "rules": [
    {
      "extensions": ["go"],
      "command": "go run main.go"
    },
    {
      "extensions": ["js"],
      "command": "node index.js"
    }
  ],
  "debounce_time": "500ms",
  "live_reload": true,
  "live_reload_port": 35729
}

Run with the config file:

go-watch --config go-watch.config.json

Use Cases

1. Watching a Go Project

Watch .go files and restart the Go application on changes:

go-watch --ext go --cmd "go run main.go"

2. Watching a Node.js Project

Watch .js files and restart the Node.js application on changes:

go-watch --ext js --cmd "node index.js"

3. Watching a Frontend Project

Watch .html, .css, and .js files, and enable live reload:

go-watch --ext html,css,js --live-reload --live-reload-port 35729

4. Ignoring Specific Directories

Watch .go files but ignore the vendor and tmp directories:

go-watch --ext go --ignore vendor,tmp --cmd "go run main.go"

5. Using a Configuration File

Create a go-watch.config.json file:

{
  "watch_dirs": [".", "src"],
  "ignore_dirs": ["node_modules", ".git"],
  "extensions": ["go", "js"],
  "command": "go run main.go",
  "debounce_time": "1s",
  "live_reload": false
}

Run with the config file:

go-watch --config go-watch.config.json

Configuration Options

OptionDescription
--configPath to a JSON or YAML configuration file.
--extComma-separated list of file extensions to watch (e.g., go,js).
--ignoreComma-separated list of directories to ignore (e.g., node_modules,.git).
--cmdCommand to execute when changes are detected (e.g., go run main.go).
--debounceDebounce time for file changes (e.g., 500ms, 1s).
--live-reloadEnable live reload for frontend workflows.
--live-reload-portPort for the live reload server (default: 35729).

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

License

MIT