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

https://github.com/tinywasm/devwatch

fsnotify implementation Watches file system changes in a project directory, triggering custom handlers for file and folder events, and supporting browser reloads and selective file ignoring.
https://github.com/tinywasm/devwatch

Last synced: 5 months ago
JSON representation

fsnotify implementation Watches file system changes in a project directory, triggering custom handlers for file and folder events, and supporting browser reloads and selective file ignoring.

Awesome Lists containing this project

README

          

# devwatch

Project Badges

fsnotify implementation Watches file system changes in a project directory, triggering custom handlers for file and folder events, and supporting browser reloads and selective file ignoring.

## Public API

### Main Types

```go
// FilesEventHandlers unifies asset and Go file event handling.
// It allows handlers to specify which file extensions they support and how to process them.
type FilesEventHandlers interface {
MainInputFileRelativePath() string // eg: go => "app/server/main.go" | js =>"app/pwa/public/main.js"
// NewFileEvent handles file events (create, remove, write, rename).
NewFileEvent(fileName, extension, filePath, event string) error
SupportedExtensions() []string // eg: [".go"], [".js",".css"], etc.
UnobservedFiles() []string // eg: main.exe, main.js
}

// Folder event handler interface
// event: create, remove, write, rename
type FolderEvent interface {
NewFolderEvent(folderName, path, event string) error
}

// Main configuration struct
type WatchConfig struct {
AppRootDir string // Project root directory
FilesEventHandlers []FilesEventHandlers // All file event handlers are managed here
FolderEvents FolderEvent // Handler for folder events
BrowserReload func() error // Function to reload the browser
Logger func(message ...any) // Log output
ExitChan chan bool // Channel to signal exit
UnobservedFiles func() []string // Files/folders to ignore (e.g. .git, .vscode)
}

type DevWatch struct {
*WatchConfig
// ... (internal fields)
}
```

### Initialization and Usage

```go
// Create your handlers that implement the FilesEventHandlers interface
// Example handlers:
// assetsHandler handles .css, .js, .html
// goServerHandler handles .go files for the server
// tinyWasmHandler handles .go files for wasm and .js for runtime detection

// Create configuration
cfg := &devwatch.WatchConfig{
AppRootDir: "/path/to/your/app",
FilesEventHandlers: []devwatch.FilesEventHandlers{
assetsHandler,
goServerHandler,
tinyWasmHandler,
},
FolderEvents: yourFolderHandler,
BrowserReload: yourReloadFunc,
Logger: func(message ...any) { fmt.Println(message...) },
ExitChan: make(chan bool),
UnobservedFiles: func() []string { return []string{".git", ".vscode"} },
}

// Create watcher
watcher := devwatch.New(cfg)

// Start the watcher (example with WaitGroup)
var wg sync.WaitGroup
wg.Add(1)
go watcher.FileWatcherStart(&wg)
```

### Notes

- Implement your own handlers for `FilesEventHandlers` and `FolderEvent` according to your application logic.
- Each handler in `FilesEventHandlers` must specify the file extensions it supports via the `SupportedExtensions()` method.
- For `.go` files, the system automatically identifies the correct handler(s) using `godepfind` dependency logic.
- The handlers are processed in the order they are registered in the `FilesEventHandlers` slice.
- Use the `ExitChan` channel to stop the watcher gracefully.

## [Contributing](https://github.com/tinywasm/cdvelop/blob/main/CONTRIBUTING.md)