Skip to content

Instantly share code, notes, and snippets.

@subfuzion
Last active January 20, 2016 22:22
Show Gist options
  • Save subfuzion/c2753bcf5fc12b8b2759 to your computer and use it in GitHub Desktop.
Save subfuzion/c2753bcf5fc12b8b2759 to your computer and use it in GitHub Desktop.
Using watch and glob - package.json example
var glob = require('glob');
var path = require('path');
// ===== files to watch =====
// this is just a simple glob example that could be done far more
// easily by using the following run script in package.json:
// "watch": "watch '<cmd>' ./src"
var pattern = './src/**/*';
var options = {};
// ==========================
var files = glob.sync(pattern, options);
var set = new Set();
// Add all the files we want to watch to the set.
// We need to explicitly add the directory name
// to the set as well, otherwise the watch
// filter function won't watch files in it.
files.forEach(function(f) {
var p = path.relative(__dirname, f);
set.add(p);
var dir = path.parse(p).dir;
set.add(dir);
});
// Exports a filter function that returns true or false for each file and
// directory to decide whether or not that file or directory is included by
// watch. The argument to the function (f) is a fully qualified string pathname.
module.exports = function(f) {
// The original naive approach attempted to use minimatch, but minimatch
// rejects 'src' when the glob pattern is 'src/**', for example, and if
// the 'src' dir is rejected, watch won't call this filter function with
// any files inside the 'src' directory.
// This is an artificial example, but reflects my desire to handle
// simple glob patterns that might be supplied by a user that should
// work as expected.
var p = path.relative(__dirname, f);
var watch = set.has(p);
if (watch) console.log(p);
return set.has(p);
};
{
"name": "node-app",
"version": "1.0.0",
"scripts": {
"watch": "watch 'npm run build' --wait=1 --filter=./.watch.js",
"build": "echo building..."
},
"devDependencies": {
"glob": "^6.0.4",
"watch": "^0.17.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment