What is onchange?
The 'onchange' npm package is a simple tool that allows you to watch files and directories for changes and then execute commands in response to those changes. It is particularly useful for automating tasks in development workflows, such as running tests, building projects, or restarting servers when files are modified.
What are onchange's main functionalities?
Watch Files and Execute Commands
This feature allows you to watch JavaScript files in your project and run the 'npm test' command whenever any of these files change. The '**/*.js' pattern ensures that all JavaScript files in the project are monitored.
onchange '**/*.js' -- npm test
Watch Multiple Patterns
You can watch multiple file patterns simultaneously. In this example, both source and test JavaScript files are being watched, and the 'npm run build' command is executed whenever any of these files change.
onchange 'src/**/*.js' 'test/**/*.js' -- npm run build
Run Multiple Commands
This feature allows you to run multiple commands in sequence when a change is detected. Here, it first echoes 'Files changed' and then runs the 'npm run lint' command.
onchange 'src/**/*.js' -- echo 'Files changed' && npm run lint
Ignore Specific Files or Directories
You can exclude specific files or directories from being watched. In this example, all JavaScript files are watched except those in the 'node_modules' directory.
onchange '**/*.js' --exclude 'node_modules/**' -- npm test
Other packages similar to onchange
chokidar
Chokidar is a highly efficient and flexible file watcher for Node.js. It offers more advanced features compared to 'onchange', such as the ability to watch for specific types of changes (e.g., added, changed, removed) and better performance with large numbers of files.
nodemon
Nodemon is a utility that monitors for any changes in your source and automatically restarts your server. While it is primarily used for restarting Node.js applications, it can also be configured to watch other types of files and run arbitrary commands, similar to 'onchange'.
watch
The 'watch' package is a simple, straightforward file watcher that can execute commands when files change. It is less feature-rich compared to 'onchange' but can be a good choice for simpler use cases.
onchange
Use glob patterns to watch file sets and run a command when anything is added, changed or deleted.
Install
npm install onchange
Usage
onchange 'app/**/*.js' 'test/**/*.js' -- npm test
onchange -i -k '**/*.js' -- node server.js
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:
onchange -a 'config.json' -- microservice-proxy -c {{file}} -p 9000
Initial (-i
, --initial
)
To execute the command once on load without any event:
onchange -i '**/*.js' -- npm start
Exclude (-e
, --exclude
)
To exclude matches:
onchange '**/*.ts' -e 'dist/**/*.js' -- tslint
No Exclude (--no-exclude
)
**/node_modules/**
and **/.git/**
are excluded by default, use --no-exclude
to disable:
onchange 'node_modules/**' --no-exclude -- tslint
Exclude Path (--exclude-path
)
Excludes all paths in a file following the .gitignore
specification.
onchange '**' --exclude-path .gitignore -- prettier
Kill (-k
, --kill
)
To kill current and pending processes between changes:
onchange -k '**/*.js' -- npm test
Jobs (-j <n>
, --jobs <n>
)
Set the maximum concurrent processes to run (default is 1
):
onchange -j2 '**/*.js' -- cp -v -r '{{file}}' 'test/{{file}}'
Delay (-d
, --delay
)
To set the amount of delay (in ms) between process changes:
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
):
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.
onchange -p 100 '**/*.js' -- npm test
Outpipe (-o
, --outpipe
)
Shell command to execute every change:
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. Use
this option to watch only for events you need:
onchange -f add -f change '**/*.js' -- npm start
Verbose (-v
, --verbose
)
Enable if you want verbose logging from onchange
(useful for debugging). For example:
onchange -v 'app/**/*.js' 'test/**/*.js' -- npm test
TypeScript
Includes types for TypeScript users.
Related
- cli-error-notifier - Send native desktop notifications if a command exits with an exit code other than
0
.
License
MIT