From 56795fcd8c662392cb969aefa98adfef723df788 Mon Sep 17 00:00:00 2001 From: Roman Sharkov Date: Tue, 6 Aug 2024 18:31:03 +0200 Subject: [PATCH] feat: Add custom watcher exclude config --- example-config.yml | 10 ++++++++-- internal/config/config.go | 4 ++++ main.go | 23 ++++++++++++++++++----- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/example-config.yml b/example-config.yml index 36cea69..a1c4742 100644 --- a/example-config.yml +++ b/example-config.yml @@ -74,6 +74,8 @@ custom-watchers: # include defines that this watcher will watch all JavaScript and JSX files. include: ["*.js", "*.jsx"] + exclude: # exclude is optional. + # fail-on-error specifies that in case cmd returns error code 1 the output # of the execution should be displayed in the browser, just like # for example if the Go compiler fails to compile. @@ -92,7 +94,7 @@ custom-watchers: # - "reload" = reload all browser tabs. # - "restart" = restart the server but don't rebuild it. # - "rebuild" = re-lint, rebuild and restart the server. - requires: "reload" + requires: reload - name: "Restart on config change" # cmd specifies that no special command needs to be executed since this watcher @@ -102,6 +104,10 @@ custom-watchers: # include specifies what kind of configuration files need to be watched. include: ["*.yaml", "*.yml", "*.toml"] + # exclude specifies what kind of configuration files, that would otherwise + # match `include` to explicitly exclude. + exclude: ["ignore-this.yaml"] + # fail-on-error doesn't need to be specified when cmd is empty. Default is false. fail-on-error: @@ -110,4 +116,4 @@ custom-watchers: # requires specifies that when a config file changes the server needs # to be restarted, but doesn't need to be rebuilt. - requires: "restart" + requires: restart diff --git a/internal/config/config.go b/internal/config/config.go index af45ef8..ea13183 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -104,6 +104,10 @@ type ConfigCustomWatcher struct { // Include specifies glob expressions for what files to watch. Include GlobList `yaml:"include"` + // Exclude specifies glob expressions for what files to ignore + // that would otherwise match `include`. + Exclude GlobList `yaml:"exclude"` + // Cmd specifies the command to run when an included file changed. // Cmd will be executed in app.dir-work. This is optional and can be left empty // since sometimes all you want to do is rebuild & restart or just restart diff --git a/main.go b/main.go index 02aad57..dcda303 100644 --- a/main.go +++ b/main.go @@ -51,6 +51,7 @@ type customWatcher struct { name string cmd config.CmdStr include []glob.Glob + exclude []glob.Glob debounced func(func()) failOnErr bool requires action.Type @@ -59,6 +60,11 @@ type customWatcher struct { func (c customWatcher) isFilePathIncluded(s string) bool { for _, glob := range c.include { if glob.Match(s) { + for _, glob := range c.exclude { + if glob.Match(s) { + return false + } + } return true } } @@ -106,18 +112,25 @@ func main() { for i, w := range conf.CustomWatchers { debouncer, debounced := debounce.NewSync(w.Debounce) go debouncer(ctx) - globs := make([]glob.Glob, len(w.Include)) + + // The following globs have already been validated during config parsing. + // It's safe to assume compilation succeeds. + include := make([]glob.Glob, len(w.Include)) for i, pattern := range w.Include { - // These globs have already been validated during config parsing. - // It's safe to assume compilation succeeds. - globs[i] = glob.MustCompile(pattern) + include[i] = glob.MustCompile(pattern) } + exclude := make([]glob.Glob, len(w.Exclude)) + for i, pattern := range w.Exclude { + exclude[i] = glob.MustCompile(pattern) + } + customWatchers[i] = customWatcher{ name: w.Name, debounced: debounced, cmd: w.Cmd, failOnErr: w.FailOnError, - include: globs, + include: include, + exclude: exclude, requires: action.Type(w.Requires), } }