gulp-watch-dir v0.1.0
gulp-watch-dir
A file watcher that watches the file system relative to the files in the stream.
Installation
Install package with NPM and add it to your development dependencies:
npm install gulp-watch-dir --save-dev
Usage
gulp-watch-dir passes the file it received down the stream, not the file changed.
const watchdir = require("gulp-watch-dir");
const tap = require("gulp-tap");
gulp.src("projects/**/project.xml")
// Watch all JS files beneath the folders contain project.xml.
// When any JS file changes, pass project.xml down the pipe.
.pipe(watchdir("**/*.js", {
ignoreInitial: false,
verbose: true,
awaitWriteFinish: {stabilityThreshold: 500}
}))
.pipe(tap(function(file){
// Output: "c:/test/projects/1/project.xml"
console.log(file.path);
}))
In contrast, the file that has actually changed is being passed down the stream by gulp-watch.
const tap = require("gulp-tap");
const watch = require("gulp-watch");
gulp.src("projects/**/*.js")
.pipe(watch("projects/**/*.js"))
.pipe(tap(file => {
// Output: "c:/test/projects/1/src/code.js"
console.log(file.path);
}));
Why would I use this and not gulp-watch?
Mostly, you wouldn't. gulp-watch-dir creates a new watcher for every file in the stream. In contrast, each call to gulp-watch creates one single watcher only. In most cases, gulp-watch is probably the best choice.
In some cases however, you need to watch files and directories that are determined during the workflow. gulp-watch does not process the files that get piped into it. gulp-watch-dir instead uses the directory of the files piped into it as a base for the supplied globs.
API
watchdir(options, callback)
Creates a watcher that will spy on files that are matched by the input stream.
options
This argument is passed to chokidar;
callback
This function is called when events happen on the file-system.
callback(file, changedFile)
event
: A string representing the file system event. Available events: add, addDir, change, unlink, unlinkDir, ready, raw, error.file
: The Vinyl file in the stream.changedFile
: A path to the file that has actually changed.