callback
whenever a source
file changes.
await watch(source, callback[, watcherId])
source
can be a file, an array of files or a glob pattern.
The callback is an async
function called with filepath
and type
:
filepath
is the absolute path to the file triggering the callbacktype
is either created
, modified
or deleted
based on the event
triggering the callbackawait watch('./src/**/*.css', async (filepath, type) => {
console.info(`File ${filepath} has been ${type}`);
});
You can use unwatch or unwatchAll to cancel watchers when you don't need them anymore.
When writing tests, you might find waitForWatchers useful to wait for the next watch tick, to make sure filesystem events have enough time to be caught.