devwatch

package module
v0.0.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 13, 2025 License: MIT Imports: 11 Imported by: 0

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
// File event handler interface
// event: create, remove, write, rename
 type FileEvent interface {
     NewFileEvent(fileName, extension, filePath, event string) error
 }

// Go file handler: must implement both DepHandler and FileEvent
 type GoFileHandler interface {
     godepfind.DepHandler
     FileEvent
 }

// 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
     FileEventAssets FileEvent         // Handler for static assets (css, js, html, etc)
     FilesEventGO    []GoFileHandler   // Handlers for .go files (backend, wasm, etc)
     FolderEvents    FolderEvent       // Handler for folder events
     BrowserReload   func() error      // Function to reload the browser
     Writer          io.Writer         // Log output
     ExitChan        chan bool         // Channel to signal exit
     UnobservedFiles func() []string   // Files/folders to ignore (e.g. .git, .vscode)
 }

 type DevWatch struct {
     *WatchConfig
     watcher         *fsnotify.Watcher
     depFinder       *godepfind.GoDepFind // Dependency finder for Go projects
     no_add_to_watch map[string]bool
 }
Initialization and Usage
// Create configuration
cfg := &devwatch.WatchConfig{
    AppRootDir:      "/path/to/your/app",
    FileEventAssets: yourAssetsHandler,
    FilesEventGO:    []devwatch.GoFileHandler{yourBackendHandler, yourWasmHandler},
    FolderEvents:    yourFolderHandler,
    BrowserReload:   yourReloadFunc,
    Writer:          os.Stdout,
    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 FileEvent, GoFileHandler, and FolderEvent according to your application logic.
  • For .go files, the system automatically identifies the correct handler(s) using godepfind dependency logic.
  • Use the ExitChan channel to stop the watcher gracefully.
AddSupportedAssetsExtensions
// AddSupportedAssetsExtensions adds one or more file extensions to the supported assets list.
// By default, the following extensions are included: .html, .css, .js, .svg
// It ensures no duplicates are added.
// Example:
//   watcher.AddSupportedAssetsExtensions(".png", ".jpg", ".webp")
func (dw *DevWatch) AddSupportedAssetsExtensions(exts ...string)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetFileName

func GetFileName(path string) (string, error)

GetFileName returns the filename from a path Example: "theme/index.html" -> "index.html"

Types

type DevWatch

type DevWatch struct {
	*WatchConfig
	// contains filtered or unexported fields
}

func New

func New(c *WatchConfig) *DevWatch

func (*DevWatch) AddSupportedAssetsExtensions added in v0.0.3

func (dw *DevWatch) AddSupportedAssetsExtensions(exts ...string)

AddSupportedAssetsExtensions adds one or more file extensions to the supported assets list. By default, the following extensions are included: .html, .css, .js, .svg It ensures no duplicates are added. Example: dw.AddSupportedAssetsExtensions(".png", ".jpg")

func (*DevWatch) Contain

func (h *DevWatch) Contain(path string) bool

func (*DevWatch) FileWatcherStart

func (h *DevWatch) FileWatcherStart(wg *sync.WaitGroup)

func (*DevWatch) InitialRegistration

func (h *DevWatch) InitialRegistration()

type FileEvent

type FileEvent interface {
	NewFileEvent(fileName, extension, filePath, event string) error
}

event: create, remove, write, rename

type FolderEvent

type FolderEvent interface {
	NewFolderEvent(folderName, path, event string) error
}

event: create, remove, write, rename

type GoFileHandler added in v0.0.3

type GoFileHandler interface {
	godepfind.DepHandler
	FileEvent
}

type WatchConfig

type WatchConfig struct {
	AppRootDir      string          // eg: "home/user/myNewApp"
	FileEventAssets FileEvent       // when change assets files eg: css, js, html, png, jpg, svg, etc event: create, remove, write, rename
	FilesEventGO    []GoFileHandler // handlers for go file events (backend, wasm, etc)
	FolderEvents    FolderEvent     // when directories are created/removed for architecture detection

	BrowserReload func() error // when change frontend files reload browser

	Writer          io.Writer       // For logging output
	ExitChan        chan bool       // global channel to signal the exit
	UnobservedFiles func() []string // files that are not observed by the watcher eg: ".git", ".gitignore", ".vscode",  "examples",
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL