Skip to content

liamrlawrence/observer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Observer

Observer is a Golang project designed to enhance development workflows by automating the process of watching file changes within a directory, and then executing build and run commands for hot reloading.

Features

  • File Monitoring: Automatically detects changes in specified files based on extensions, directories, and regex patterns.
  • Configurable: Allows for detailed configuration to specify which files to watch and what commands to trigger.
  • Hot Reloading: Executes build and run commands automatically on file change, facilitating hot reloading during development.

Installation

Via go install (Recommended)

With go 1.22 or higher:

go install github.com/liamrlawrence/observer/cmd/observer@latest

Configuration

The Observer tool uses a JSON configuration file to define how it should monitor and respond to file changes in directories. Below is a detailed description of each configuration parameter available in observer.config.json.

Top-Level Configuration

  • rebuild_delay (optional): The delay (in milliseconds) after a file is changed before rebuilding. If omitted, the default is 500ms.
  • init_commands (optional): An array that contains a list of commands that should be run once on startup. This can be especially useful when working with Docker containers.
  • watchers (required): An array that contains configurations for different sets of files to watch. Each watcher can be customized with its own set of rules and commands.

Watcher Configuration

  • label (optional, recommended): A label used when printing information to the console. If omitted, a default label will be generated, however it is not the most user friendly.

  • extensions (required): An array of file extensions to watch.

  • include_dirs (optional): An array of directories to watch. If omitted, the tool defaults to the current working directory (".").

  • ignore_dirs (optional): An array of directories to ignore.

  • include_patterns (optional): An array of regular expression patterns. Paths matching these patterns will be watched.

  • ignore_patterns (optional): An array of regular expression patterns. Paths matching these patterns will be ignored.

  • build_command (required): The command to execute when changes are detected that meet the criteria. This command is typically a build or compile command.

  • run_command (optional): An additional command that can be run after the build_command. This could be used to execute a program or script.

  • debug (optional): A boolean to toggle additional diagnostic information.

Here is an example of a configuration file:

{
  "init_commands": [
    "cp -r node_modules_cache/* node_modules/"
  ],
  "watchers": [
    {
      "label": "[Go]",
      "extensions": [".go", ".templ"],
      "include_dirs": ["cmd", "internal"],
      "ignore_patterns": ["_templ\\.go"],
      "build_command": "make go",
      "run_command": "./build/main"
    },
    {
      "label": "[Web]",
      "extensions": [".ts"],
      "include_dirs": ["static"],
      "build_command": "npm run build"
    }
  ]
}

Optional Configuration Flags

You can specify custom flags at runtime to configure the behavior of the application. Here are the available flags:

  • -config: This flag allows you to specify a custom configuration file.

    observer -config path/to/your/config.json

    If omitted, Observer defaults to using observer.config.json from the current working directory.

  • -debug: Setting this flag to true activates debug mode for all watchers, regardless of what is specified in their configuration. In debug mode, additional diagnostic information is logged.

    observer -debug=true

License

This project is licensed under the MIT License.

About

A file watcher that triggers commands when a change is detected, used for hot reloading development environments.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages