Skip to content

Commit

Permalink
feat: Add custom watcher exclude config
Browse files Browse the repository at this point in the history
  • Loading branch information
romshark committed Aug 6, 2024
1 parent fb37bbb commit 56795fc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
10 changes: 8 additions & 2 deletions example-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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:

Expand All @@ -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
4 changes: 4 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 18 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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),
}
}
Expand Down

0 comments on commit 56795fc

Please sign in to comment.