Skip to content

Commit

Permalink
feat(watchers): Allow per-watcher options hash.
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane authored and Shane Osbourne committed Apr 9, 2015
1 parent e74250f commit 3c069fb
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 26 deletions.
18 changes: 17 additions & 1 deletion lib/default-config.js
Expand Up @@ -40,7 +40,23 @@ module.exports = {
* @since 1.3.0
*/
watchOptions: {
ignoreInitial: true
/**
*
persistent: true,
ignored: '*.txt',
ignoreInitial: false,
followSymlinks: true,
cwd: '.',
usePolling: true,
alwaysStat: false,
depth: undefined,
interval: 100,
ignorePermissionErrors: false,
atomic: true
*/
},

/**
Expand Down
32 changes: 12 additions & 20 deletions lib/file-watcher.js
Expand Up @@ -6,18 +6,15 @@
*/
module.exports.plugin = function (options, emitter) {

// Options to pass along to Gaze
var watchOptions = options.get("watchOptions") || options.get("watchoptions");
var defaultWatchOptions = require("immutable").Map({
ignored: /[\/\\]\./
})
.mergeDeep(
options.get("watchOptions") || options.get("watchoptions")
)
.toJS();

if (watchOptions) {
watchOptions = watchOptions.toJS();
} else {
watchOptions = {};
}

var globs = options.get("files");

return globs.reduce(function (map, glob, namespace) {
return options.get("files").reduce(function (map, glob, namespace) {

/**
* Default CB when not given
Expand All @@ -37,21 +34,21 @@ module.exports.plugin = function (options, emitter) {
if (jsItem.globs.length) {
if (!map[namespace]) {
map[namespace] = {
watchers: [getWatcher(jsItem.globs, watchOptions, fn)]
watchers: [getWatcher(jsItem.globs, defaultWatchOptions, fn)]
};
} else {
map[namespace].watchers.push(getWatcher(jsItem.globs, watchOptions, fn));
map[namespace].watchers.push(getWatcher(jsItem.globs, defaultWatchOptions, fn));
}
}

if (jsItem.objs.length) {
jsItem.objs.forEach(function (item) {
if (!map[namespace]) {
map[namespace] = {
watchers: [getWatcher(item.match, watchOptions, item.fn)]
watchers: [getWatcher(item.match, item.options || defaultWatchOptions, item.fn)]
};
} else {
map[namespace].watchers.push(getWatcher(item.match, watchOptions, item.fn));
map[namespace].watchers.push(getWatcher(item.match, item.options || defaultWatchOptions, item.fn));
}
});
}
Expand All @@ -62,10 +59,5 @@ module.exports.plugin = function (options, emitter) {
};

function getWatcher (patterns, opts, fn) {

if (!opts.ignored) {
opts.ignored = /[\/\\]\./;
}

return require("chokidar").watch(patterns, opts).on("all", fn);
}
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -34,11 +34,11 @@
"pre-release": "npm test && npm run pro-local && npm run pro"
},
"dependencies": {
"anymatch": "^1.1.0",
"anymatch": "^1.2.1",
"async-each-series": "^0.1.1",
"browser-sync-client": "^1.0.1",
"browser-sync-ui": "^0.5.2",
"chokidar": "^1.0.0-rc5",
"browser-sync-ui": "^0.5.3",
"chokidar": "^1.0.0-rc6",
"connect": "^3.3.5",
"dev-ip": "^1.0.1",
"easy-extender": "^2.3.0",
Expand Down
@@ -1,6 +1,6 @@
"use strict";

var browserSync = require("../../../index");
var browserSync = require("../../../../index");

var path = require("path");
var sinon = require("sinon");
Expand Down
109 changes: 109 additions & 0 deletions test/specs/e2e/files/e2e.file.options.js
@@ -0,0 +1,109 @@
"use strict";

var browserSync = require("../../../../");

var path = require("path");
var assert = require("chai").assert;

var outpath = path.join(__dirname, "../../fixtures");

describe("file-watching", function () {

describe("E2E Adding namespaced watchers", function () {

var instance, file;

before(function (done) {

browserSync.reset();

file = path.join(outpath, "watch-func.txt");

var config = {
files: file,
logLevel: "silent"
};

instance = browserSync(config, done).instance;
});

after(function () {
instance.cleanup();
});

it("Watches files with no namespace", function (done) {

assert.ok(instance.watchers.core.watchers);
assert.equal(instance.watchers.core.watchers.length, 1);
done();
});
});

describe("E2E Adding namespaced watchers", function () {

var instance, file;

before(function (done) {

browserSync.reset();

file = path.join(outpath, "watch-func.txt");

var config = {
files: "*.html",
logLevel: "silent"
};

instance = browserSync(config, done).instance;
});

after(function () {
instance.cleanup();
});

it("Watches files when multi given", function (done) {

assert.ok(instance.watchers.core.watchers);
assert.ok(instance.watchers.core.watchers[0]);
done();
});
});

describe("E2E Adding namespaced watchers", function () {

var instance, file;

before(function (done) {

browserSync.reset();

file = path.join(outpath, "watch-func.txt");

var config = {
files: [
"*.html",
{
match: "*.css",
fn: function (event, file) {
console.log(file);
}
}
],
logLevel: "silent"
};

instance = browserSync(config, done).instance;
});

after(function () {
instance.cleanup();
});

it("Watches files when multi given + objs", function (done) {

assert.ok(instance.watchers.core.watchers);
assert.equal(instance.watchers.core.watchers.length, 2);
done();
});
});
});
@@ -1,6 +1,6 @@
"use strict";

var browserSync = require("../../../");
var browserSync = require("../../../../");

var path = require("path");
var assert = require("chai").assert;
Expand Down
29 changes: 29 additions & 0 deletions test/specs/files/files.watching.js
Expand Up @@ -43,6 +43,34 @@ describe("File Watcher Module", function () {
assert.equal(watchers.core.watchers[0].options.debounceDelay, 4000);
done();
});
it("Passes separate options for chokidar when multi given", function (done) {
var imm = merge({
files: [
"css/*.css",
{
match: "*.html",
fn: function (event) {
console.log(event);
},
options: {
interval: 100
}
}
],
watchOptions: {
interval: 200
}
});
imm = imm.set("files", hooks["files:watch"]([], imm.get("files"), {}));

var emitter = new events.EventEmitter();
var watchers = fileWatcher.plugin(imm, emitter);

assert.equal(watchers.core.watchers.length, 2);
assert.equal(watchers.core.watchers[0].options.interval, 200);
assert.equal(watchers.core.watchers[1].options.interval, 100);
done();
});
it("should emit events about changed files in core namespace", function (done) {

var tempFile = path.join(outpath, "watch-func.txt");
Expand All @@ -67,6 +95,7 @@ describe("File Watcher Module", function () {

// act: change file
writeFileWait(tempFile, tempFileContent + " changed");
writeFileWait(tempFile, tempFileContent + " changed");
});
});
});
Expand Down

0 comments on commit 3c069fb

Please sign in to comment.