# onchange

> Use glob patterns to watch file sets and run a command when anything is added, changed or deleted.

Latest version **7.1.0** (published 2020-10-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install onchange
pnpm add onchange
yarn add onchange
bun add onchange
```

Provides the command `onchange`.

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 7.1.0 |
| Published | 2020-10-22 |
| First published | 2014-01-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 7 |
| Unpacked size | 40.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 826 |
| Author | Stephen Belanger |
| Maintainers | blakeembrey, qard |
| Keywords | glob, watch, change |

## Links

- npm: https://www.npmjs.com/package/onchange
- Repository: https://github.com/Qard/onchange
- Issues: https://github.com/Qard/onchange/issues
- npm.io page: https://npm.io/package/onchange

## Dependencies (7)

- [arg](https://npm.io/package/arg.md) ^4.1.3
- [ignore](https://npm.io/package/ignore.md) ^5.1.4
- [chokidar](https://npm.io/package/chokidar.md) ^3.3.1
- [tree-kill](https://npm.io/package/tree-kill.md) ^1.2.2
- [cross-spawn](https://npm.io/package/cross-spawn.md) ^7.0.1
- [@blakeembrey/deque](https://npm.io/package/@blakeembrey/deque.md) ^1.0.5
- [@blakeembrey/template](https://npm.io/package/@blakeembrey/template.md) ^1.0.0

## Alternatives

- [@mce/gif](https://npm.io/package/@mce/gif.md) — 2.6K weekly downloads
- [cleanse](https://npm.io/package/cleanse.md) — 173 weekly downloads
- [str](https://npm.io/package/str.md) — 127 weekly downloads
- [naming](https://npm.io/package/naming.md) — 95 weekly downloads
- [tap-telco-api](https://npm.io/package/tap-telco-api.md) — 19 weekly downloads

## Recent versions

- 7.1.0 (latest) — 2020-10-22
- 7.0.2 — 2020-04-30
- 7.0.1 — 2020-04-28
- 7.0.0 — 2020-04-25
- 6.1.1 — 2020-04-21
- 6.1.0 — 2019-09-10
- 6.0.0 — 2019-05-19
- 5.2.0 — 2018-12-09
- 5.1.3 — 2018-11-26
- 5.1.2 — 2018-11-24
- 5.1.1 — 2018-11-22
- 5.1.0 — 2018-11-03
- 5.0.2 — 2018-10-02
- 5.0.1 — 2018-10-02
- 5.0.0 — 2018-09-30
- … 24 more at https://npm.io/package/onchange/versions

## README

# onchange

Use glob patterns to watch file sets and run a command when anything is added, changed or deleted.

## Install

```sh
npm install onchange
```

## Usage

```sh
# On every change run `npm test`.
onchange 'app/**/*.js' 'test/**/*.js' -- npm test

# On every change restart `server.js` (by killing the running process).
onchange -i -k '**/*.js' -- node server.js

# On every change `echo` file change.
onchange '**/*.js' -- echo '{{event}} to {{file}}'
```

NOTE: Windows users may need to use double quotes rather than single quotes. If used in an npm script, remember to escape the double quotes.

You can match as many glob patterns as you like, just put the command you want to run after the `--` and it will run any time a file matching any of the globs is added changed or deleted.

Other available replacement variables are `fileExt`
(`path.extname(file)`), `fileBase` (`path.basename(file)`),
`fileBaseNoExt` (basename, without extension), and `fileDir`
(`path.dirname(file)`).

## Options

### Add (`-a`, `--add`)

To execute the command for all initially added paths:

```sh
onchange -a 'config.json' -- microservice-proxy -c {{file}} -p 9000
```

### Initial (`-i`, `--initial`)

To execute the command once on load without any event:

```sh
onchange -i '**/*.js' -- npm start
```

### Exclude (`-e`, `--exclude`)

To exclude matches:

```sh
onchange '**/*.ts' -e 'dist/**/*.js' -- tslint
```

### No Exclude (`--no-exclude`)

`**/node_modules/**` and `**/.git/**` are excluded by default, use `--no-exclude` to disable:

```sh
onchange 'node_modules/**' --no-exclude -- tslint
```

### Exclude Path (`--exclude-path`)

Excludes all paths in a file following the [`.gitignore`](https://git-scm.com/docs/gitignore) specification.

```sh
onchange '**' --exclude-path .gitignore -- prettier
```

### Kill (`-k`, `--kill`)

To kill current and pending processes between changes:

```sh
onchange -k '**/*.js' -- npm test
```

### Jobs (`-j <n>`, `--jobs <n>`)

Set the maximum concurrent processes to run (default is `1`):

```sh
onchange -j2 '**/*.js' -- cp -v -r '{{file}}' 'test/{{file}}'
```

### Delay (`-d`, `--delay`)

To set the amount of delay (in ms) between process changes:

```sh
onchange -d 1000 '**/*.js' -- npm start
```

### Await Write Finish (`--await-write-finish <ms>`)

To hold the events until the size does not change for a configurable amount of time (in ms, default is [`2000`](https://www.npmjs.com/package/chokidar#performance)):

```sh
onchange --await-write-finish 1500 '**/*.js' -- npm test
```

### Poll (`-p <ms>`, `--poll <ms>`)

Use polling to monitor for changes. This option is useful if you're watching an NFS volume.

```sh
onchange -p 100 '**/*.js' -- npm test
```

### Outpipe (`-o`, `--outpipe`)

Shell command to execute every change:

```sh
onchange -o '> .changelog' 'src/**/*.js' -- echo '{{event}} to {{file}}'
```

**P.S.** When a command is used with `--outpipe`, the `stdout` from the command will be piped into `outpipe`.

### Filter (`-f`, `--filter`)

By default, onchange watches for all events from [chokidar](https://github.com/paulmillr/chokidar#methods--events). Use
this option to watch only for events you need:

```sh
onchange -f add -f change '**/*.js' -- npm start
```

### Verbose (`-v`, `--verbose`)

Enable if you want verbose logging from `onchange` (useful for debugging). For example:

```sh
onchange -v 'app/**/*.js' 'test/**/*.js' -- npm test
```

## TypeScript

Includes [types](index.d.ts) for TypeScript users.

## Related

- [cli-error-notifier](https://github.com/micromata/cli-error-notifier) - Send native desktop notifications if a command exits with an exit code other than `0`.

## License

MIT

---
_Source: https://npm.io/package/onchange · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
