Skip to content

Commit de2e2fa

Browse files
committed
fix(files): added watchEvents option
- this will allow the user to provide an array of supported file event names to which Browsersync will respond. fixes #1291 #1291
1 parent 5d1e084 commit de2e2fa

File tree

6 files changed

+62
-7
lines changed

6 files changed

+62
-7
lines changed

lib/cli/opts.start.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@
9292
"type": "number",
9393
"desc": "Specify a port for the UI to use"
9494
},
95+
"watchEvents": {
96+
"type": "array",
97+
"desc": "Specify which file events to respond to"
98+
},
9599
"no-notify": {
96100
"desc": "Disable the notify element in browsers"
97101
},

lib/default-config.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ module.exports = {
3131
*/
3232
files: false,
3333

34+
/**
35+
* Specify which file events to respond to.
36+
* Available events: `add`, `change`, `unlink`, `addDir`, `unlinkDir`
37+
* @property watchEvents
38+
* @type Array
39+
* @default ["change"]
40+
* @since 2.18.8
41+
*/
42+
watchEvents: ["change"],
43+
3444
/**
3545
* File watching options that get passed along to [Chokidar](https://github.com/paulmillr/chokidar).
3646
* Check their docs for available options
@@ -40,11 +50,11 @@ module.exports = {
4050
* @since 2.6.0
4151
*/
4252
watchOptions: {
53+
ignoreInitial: true
4354
/*
4455
persistent: true,
4556
4657
ignored: '*.txt',
47-
ignoreInitial: false,
4858
followSymlinks: true,
4959
cwd: '.',
5060
@@ -130,7 +140,7 @@ module.exports = {
130140
* @type string
131141
* @default undefined
132142
* @since 2.18.0
133-
*/
143+
*/
134144

135145
/**
136146
* Clicks, Scrolls & Form inputs on any device will be mirrored to all others.

lib/file-utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var fileUtils = {
2121
* `add` `unlink` etc etc so we need to check for that and only
2222
* respond to 'change', for now.
2323
*/
24-
if (data.event === "change") {
24+
if (bs.options.get("watchEvents").indexOf(data.event) > -1) {
2525
if (!bs.paused && data.namespace === "core") {
2626
bs.events.emit("file:reload", fileUtils.getFileInfo(data, bs.options));
2727
}
@@ -41,6 +41,7 @@ var fileUtils = {
4141
ext: data.ext,
4242
path: data.path,
4343
basename: data.basename,
44+
event: data.event,
4445
type: "inject"
4546
};
4647

lib/logger.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module.exports.callbacks = {
5050
return logger.info("{cyan:Reloading files that match: {magenta:%s", data.path);
5151
}
5252

53-
logger.info("{cyan:File changed: {magenta:%s", data.path);
53+
logger.info("{cyan:File event [" + data.event + "] : {magenta:%s", data.path);
5454
}
5555
},
5656
/**

test/specs/api/init.reload.stream.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,15 @@ describe("API: .stream()", function () {
125125
log: false,
126126
namespace: "core",
127127
event: "change",
128-
ext: "css"
128+
ext: "css",
129129
});
130130
sinon.assert.calledWithExactly(emitterStub, "file:reload", {
131131
ext: "css",
132132
path: "/users/shakyshane/.tmp/css/core.css",
133133
basename: "core.css",
134134
type: "inject",
135-
log: false
135+
log: false,
136+
event: "change"
136137
});
137138
sinon.assert.calledWithExactly(emitterStub, "stream:changed", {
138139
changed: ["core.css"]

test/specs/files/files.watching.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe("File Watcher Module", function () {
4747
],
4848
watchOptions: {
4949
interval: 200
50-
50+
5151
}
5252
}, function (err, bs) {
5353
assert.equal(bs.watchers.core.watchers.length, 2);
@@ -96,6 +96,45 @@ describe("File Watcher Module", function () {
9696
bs.watchers.core.watchers[0]._events.all("change", tempFile);
9797
});
9898
});
99+
it("should emit events about added files when watchEvents added", function (done) {
100+
101+
var tempFile = path.join(outpath, "watch-func.txt");
102+
var called = false;
103+
104+
browserSync.reset();
105+
106+
// assert: it works if it calls done
107+
var bs = browserSync.create();
108+
109+
bs.init({
110+
watchEvents: ["add"],
111+
files: [
112+
{
113+
options: {
114+
ignoreInitial: true
115+
},
116+
match: tempFile,
117+
fn: function (event, file) {
118+
assert.equal(event, "add");
119+
assert.equal(file, tempFile);
120+
assert.isFunction(this.reload);
121+
assert.isFunction(this.notify);
122+
bs.cleanup();
123+
if (!called) {
124+
done();
125+
called = true;
126+
}
127+
}
128+
}
129+
],
130+
ui: false,
131+
online: false,
132+
logSnippet: false,
133+
logLevel: "silent"
134+
}, function (err, bs) {
135+
bs.watchers.core.watchers[0]._events.all("add", tempFile);
136+
});
137+
});
99138
it("should allow obj literal with match & options, but without callback fn", function (done) {
100139

101140
browserSync.reset();

0 commit comments

Comments
 (0)