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.
- 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.
Via go install (Recommended)
With go 1.22 or higher:
go install github.com/liamrlawrence/observer/cmd/observer@latestThe 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.
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.
-
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 thebuild_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"
}
]
}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.jsonfrom 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
This project is licensed under the MIT License.