> For the complete documentation index, see [llms.txt](https://commandbox.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://commandbox.ortusbooks.com/task-runners/watchers.md).

# Watchers

CommandBox has a powerful utility that can be used to watch a folder of files for changes and fire arbitrary code when changes happen. The utility will block execution of the task until the user stops it with `Ctrl+C`. To use a watcher in your task, there is a method called `watch()` in the base task class that you can call. It has a nice DSL of chainable methods to configure it.

```javascript
watch()
    .paths( '**.cfc' )
    .excludePaths( '/config/Coldbox.cfc,/config/WireBox.cfc' )
    .inDirectory( getCWD() )
    .withDelay( 5000 )
    .onChange( function() {

        print.line( 'Something changed!' );
        command( 'testbox run' )
            .run();

    } )
    .start();
```

Here's a rundown of the methods used above in the DSL.

* **paths( ... )** - Receives one or more globbing patterns to watch for changes. Pass each globbing pattern as a separate argument. (defaults to `**`)
* **inDirectory( ... )** - Set the base directory that the file globs are relative to. (defaults to current working directory)
* **withDelay( ... )** - Set the number of milliseconds between polling the file system. (defaults to 500 ms)
* **onChange( ... )** - Pass a closure to be executed when a change has occurred.
* **start()** - Starts the watcher. Always call this at the end of the DSL chain

## onChange() Closure

If you don't care what the change was then you don't need to define any arguments to your closure. However, each time your closure is called, there is a struct of data passed to it that defines what paths were added removed and changed. The data is the format of:

```javascript
{
  'added':[],
  'removed':[],
  'changed':[] 
}
```

The arrays will contain the corresponding file paths. For example, if the `removed` array is empty, it means no files were removed. There should be at least one file path in at least one of the 3 arrays.

```javascript
watch()
    .onChange( function( paths ) {
        print
            .line( '#paths.added.len()# paths were added!' )
            .line( '#paths.removed.len()# paths were removed!' )
            .line( '#paths.changed.len()# paths were changed!' )            ;
    } )
    .start();
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://commandbox.ortusbooks.com/task-runners/watchers.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
