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
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.
If you want a more verbose output, include the -v
flag. For example:
onchange 'app/**/*.js' 'test/**/*.js' -v -- npm test
To use the event and file that changed, use {{event}}
or {{changed}}
anywhere in the command after --
. For example:
onchange '**/*.js' -- echo '{{event}} to {{changed}}'
To execute the command on the first run, include the -i
flag: For example:
onchange '**/*.js' -i -- npm start
To exclude matches:
onchange '**/*.ts' -e 'dist/**/*.js' -- tslint
To wait for the current process to exit between restarts:
onchange '**/*.js' -w -- npm test
TypeScript
Includes typings for TypeScript users.
Copyright (c) 2013 Stephen Belanger
Licensed under MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.