Skip to content

Commit a75dd5a

Browse files
committed
Refactor reload events to use standard prop names
1 parent 1e4de8f commit a75dd5a

File tree

10 files changed

+104
-31
lines changed

10 files changed

+104
-31
lines changed

lib/file-utils.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"use strict";
22

33
var _ = require("lodash");
4-
var utils = require("./utils");
54

65
var fileUtils = {
76
/**
@@ -38,27 +37,24 @@ var fileUtils = {
3837
*/
3938
getFileInfo: function (data, options) {
4039

41-
var path = data.path;
42-
var fileName = require("path").basename(path);
43-
44-
var fileExtension = utils.getFileExtension(path);
40+
data.ext = require("path").extname(data.path).slice(1);
41+
data.basename = require("path").basename(data.path);
4542

4643
var obj = {
47-
assetFileName: fileName,
48-
fileExtension: fileExtension
44+
ext: data.ext,
45+
path: data.path,
46+
basename: data.basename,
47+
type: "inject"
4948
};
5049

51-
var type = "inject";
52-
5350
// RELOAD page
54-
if (!_.contains(options.get("injectFileTypes").toJS(), fileExtension)) {
55-
obj.url = path;
56-
type = "reload";
51+
if (!_.contains(options.get("injectFileTypes").toJS(), obj.ext)) {
52+
obj.url = obj.path;
53+
obj.type = "reload";
5754
}
5855

59-
obj.path = path;
60-
obj.type = type;
61-
obj.log = data.log;
56+
obj.path = data.path;
57+
obj.log = data.log;
6258

6359
return obj;
6460
}

lib/logger.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ module.exports.callbacks = {
4545
*/
4646
"file:reload": function (bs, data) {
4747
if (data.log) {
48+
49+
if (data.path[0] === "*") {
50+
return logger.info("{cyan:Reloading files that match: {magenta:%s", data.path);
51+
}
52+
4853
var path = utils.resolveRelativeFilePath(data.path, data.cwd);
4954
logger.info("{cyan:File changed: {magenta:%s", path);
5055
}

lib/public/reload.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module.exports = function (emitter) {
3636
}
3737

3838
/**
39-
* Handle single sting paths such as
39+
* Handle single string paths such as
4040
* reload("core.css")
4141
*/
4242
if (typeof opts === "string" && opts !== "undefined") {

test/specs/api/init.reload.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ describe("API: .reload()", function () {
3838
browserSync.reload("css/core.css");
3939
sinon.assert.calledWithExactly(emitterStub, "file:changed", {
4040
path: "css/core.css",
41+
basename: "core.css",
4142
log: true,
4243
namespace: "core",
43-
event: "change"
44+
event: "change",
45+
ext: "css"
4446
});
4547
});
4648
it("only calls reload once if the array contains a filepath that will cause a reload", function () {
@@ -55,21 +57,36 @@ describe("API: .reload()", function () {
5557
assert.equal(calls.callCount, 2);
5658
sinon.assert.calledWithExactly(emitterStub, "file:changed", {
5759
path: "css/core.css",
60+
basename: "core.css",
5861
log: true,
5962
namespace: "core",
60-
event: "change"
63+
event: "change",
64+
ext: "css"
6165
});
6266
sinon.assert.calledWithExactly(emitterStub, "file:changed", {
6367
path: "ie.css",
68+
basename: "ie.css",
6469
log: true,
6570
namespace: "core",
66-
event: "change"
71+
event: "change",
72+
ext: "css"
6773
});
6874
});
6975
it("should accept an array of file paths as strings", function () {
7076
browserSync.reload(["index.html", "css/core.css"]);
7177
sinon.assert.calledWithExactly(emitterStub, "browser:reload");
7278
});
79+
it("should accept wildcards for files extensions eg: *.css", function () {
80+
browserSync.reload("*.css");
81+
sinon.assert.calledWithExactly(emitterStub, "file:changed", {
82+
path: "*.css",
83+
basename: "*.css",
84+
log: true,
85+
namespace: "core",
86+
event: "change",
87+
ext: "css"
88+
});
89+
});
7390
/**
7491
* BACKWARDS COMPATIBILITY:
7592
* This is an old signature that, whilst we must continue to support,

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ describe("API: .stream()", function () {
3535
stream.end();
3636
sinon.assert.calledWithExactly(emitterStub, "file:changed", {
3737
path: "styles.css",
38+
basename: "styles.css",
3839
log: false,
3940
namespace: "core",
40-
event: "change"
41+
event: "change",
42+
ext: "css"
4143
});
4244
});
4345
it("should accept multiple files in stream", function () {
@@ -47,15 +49,19 @@ describe("API: .stream()", function () {
4749
stream.end();
4850
sinon.assert.calledWithExactly(emitterStub, "file:changed", {
4951
path: "styles.css",
52+
basename: "styles.css",
5053
log: false,
5154
namespace: "core",
52-
event: "change"
55+
event: "change",
56+
ext: "css"
5357
});
5458
sinon.assert.calledWithExactly(emitterStub, "file:changed", {
5559
path: "styles2.css",
60+
basename: "styles2.css",
5661
log: false,
5762
namespace: "core",
58-
event: "change"
63+
event: "change",
64+
ext: "css"
5965
});
6066
sinon.assert.calledWithExactly(emitterStub, "stream:changed", {
6167
changed: ["styles.css", "styles2.css"]
@@ -87,9 +93,11 @@ describe("API: .stream()", function () {
8793
sinon.assert.calledThrice(emitterStub);
8894
sinon.assert.calledWithExactly(emitterStub, "file:changed", {
8995
path: "/users/shane/styles.js",
96+
basename: "styles.js",
9097
log: false,
9198
namespace: "core",
92-
event: "change"
99+
event: "change",
100+
ext: "js"
93101
});
94102
sinon.assert.calledWithExactly(emitterStub, "stream:changed", {
95103
changed: ["styles.js"]

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ describe("API: .stream() noop", function () {
3131

3232
sinon.assert.calledWithExactly(emitterStub, "file:changed", {
3333
path: "styles.css",
34+
basename: "styles.css",
3435
log: false,
3536
namespace: "core",
36-
event: "change"
37+
event: "change",
38+
ext: "css"
3739
});
3840
done();
3941
});

test/specs/commands/reload.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,14 @@ describe("E2E CLI `reload` with no files arg", function () {
5353
}
5454
},
5555
cb: function () {
56-
sinon.assert.calledWithExactly(spy, "file:changed", {path: "core.css", log: true, namespace: "core", event: "change"});
56+
sinon.assert.calledWithExactly(spy, "file:changed", {
57+
path: "core.css",
58+
basename: "core.css",
59+
log: true,
60+
namespace: "core",
61+
event: "change",
62+
ext: "css"
63+
});
5764
bs.cleanup();
5865
done();
5966
}
@@ -80,7 +87,14 @@ describe("E2E CLI `reload` with no files arg", function () {
8087
}
8188
},
8289
cb: function () {
83-
sinon.assert.calledWithExactly(spy, "file:changed", {path: "core.css", log: true, namespace: "core", event: "change"});
90+
sinon.assert.calledWithExactly(spy, "file:changed", {
91+
path: "core.css",
92+
basename: "core.css",
93+
ext: "css",
94+
log: true,
95+
namespace: "core",
96+
event: "change"
97+
});
8498
bs.cleanup();
8599
done();
86100
}

test/specs/e2e/e2e.events.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ describe("E2E Events test", function () {
2727

2828
var spy = sinon.spy(instance.io.sockets, "emit");
2929

30-
instance.events.emit("file:reload", {path: "somepath.css"});
30+
instance.events.emit("file:reload", {path: "somepath.css", fileExtension: "css"});
3131

3232
clock.tick();
3333

34-
sinon.assert.calledWithExactly(spy, "file:reload", {path: "somepath.css"});
34+
sinon.assert.calledWithExactly(spy, "file:reload", {path: "somepath.css", fileExtension: "css"});
3535

3636
spy.restore();
3737
});

test/specs/e2e/files/e2e.file.changed.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,24 @@ describe("E2E Responding to events", function () {
5252
var args = socketsStub.getCall(0).args[1];
5353

5454
assert.equal(eventName, "file:reload"); // check correct event sent to client
55-
assert.equal(args.assetFileName, "styles.css"); // Check the asset name is sent
55+
assert.equal(args.basename, "styles.css"); // Check the asset name is sent
56+
assert.isFalse(instance.paused);
57+
});
58+
59+
it("fires the file:reload event to the browser when wildcard given", function () {
60+
61+
// Emit the event as it comes from the file-watcher
62+
instance.events.emit("file:changed", {path: "*.css", event: "change", log: true, namespace: "core"});
63+
64+
clock.tick();
65+
66+
var eventName = socketsStub.getCall(0).args[0];
67+
var args = socketsStub.getCall(0).args[1];
68+
69+
assert.equal(eventName, "file:reload"); // check correct event sent to client
70+
assert.equal(args.path, "*.css"); // Check the asset name is sent
71+
assert.equal(args.basename, "*.css"); // Check the asset name is sent
72+
assert.equal(args.ext, "css"); // Check the asset name is sent
5673
assert.isFalse(instance.paused);
5774
});
5875

test/specs/http-protocol/http.reload.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,14 @@ describe("HTTP protocol", function () {
5151

5252
request(url, function (e, r, body) {
5353
sinon.assert.calledWith(spy, "file:changed");
54-
sinon.assert.calledWithExactly(spy, "file:changed", {path: "core.min.css", log: true, namespace: "core", event: "change"});
54+
sinon.assert.calledWithExactly(spy, "file:changed", {
55+
path: "core.min.css",
56+
basename: "core.min.css",
57+
ext: "css",
58+
log: true,
59+
namespace: "core",
60+
event: "change"
61+
});
5562
assert.include(body, "Called public API method `.reload()`");
5663
assert.include(body, "With args: [\"core.min.css\",\"core.css\"]");
5764
done();
@@ -63,7 +70,14 @@ describe("HTTP protocol", function () {
6370

6471
request(url, function (e, r, body) {
6572
sinon.assert.calledWith(spy, "file:changed");
66-
sinon.assert.calledWithExactly(spy, "file:changed", {path: "somefile.php", log: true, namespace: "core", event: "change"});
73+
sinon.assert.calledWithExactly(spy, "file:changed", {
74+
path: "somefile.php",
75+
basename: "somefile.php",
76+
ext: "php",
77+
log: true,
78+
namespace: "core",
79+
event: "change"
80+
});
6781
assert.include(body, "Called public API method `.reload()`");
6882
assert.include(body, "With args: \"somefile.php\"");
6983
done();

0 commit comments

Comments
 (0)