Skip to content

Commit

Permalink
Refactor reload events to use standard prop names
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Apr 12, 2015
1 parent 1e4de8f commit a75dd5a
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 31 deletions.
26 changes: 11 additions & 15 deletions lib/file-utils.js
@@ -1,7 +1,6 @@
"use strict";

var _ = require("lodash");
var utils = require("./utils");

var fileUtils = {
/**
Expand Down Expand Up @@ -38,27 +37,24 @@ var fileUtils = {
*/
getFileInfo: function (data, options) {

var path = data.path;
var fileName = require("path").basename(path);

var fileExtension = utils.getFileExtension(path);
data.ext = require("path").extname(data.path).slice(1);
data.basename = require("path").basename(data.path);

var obj = {
assetFileName: fileName,
fileExtension: fileExtension
ext: data.ext,
path: data.path,
basename: data.basename,
type: "inject"
};

var type = "inject";

// RELOAD page
if (!_.contains(options.get("injectFileTypes").toJS(), fileExtension)) {
obj.url = path;
type = "reload";
if (!_.contains(options.get("injectFileTypes").toJS(), obj.ext)) {
obj.url = obj.path;
obj.type = "reload";
}

obj.path = path;
obj.type = type;
obj.log = data.log;
obj.path = data.path;
obj.log = data.log;

return obj;
}
Expand Down
5 changes: 5 additions & 0 deletions lib/logger.js
Expand Up @@ -45,6 +45,11 @@ module.exports.callbacks = {
*/
"file:reload": function (bs, data) {
if (data.log) {

if (data.path[0] === "*") {
return logger.info("{cyan:Reloading files that match: {magenta:%s", data.path);
}

var path = utils.resolveRelativeFilePath(data.path, data.cwd);
logger.info("{cyan:File changed: {magenta:%s", path);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/public/reload.js
Expand Up @@ -36,7 +36,7 @@ module.exports = function (emitter) {
}

/**
* Handle single sting paths such as
* Handle single string paths such as
* reload("core.css")
*/
if (typeof opts === "string" && opts !== "undefined") {
Expand Down
23 changes: 20 additions & 3 deletions test/specs/api/init.reload.js
Expand Up @@ -38,9 +38,11 @@ describe("API: .reload()", function () {
browserSync.reload("css/core.css");
sinon.assert.calledWithExactly(emitterStub, "file:changed", {
path: "css/core.css",
basename: "core.css",
log: true,
namespace: "core",
event: "change"
event: "change",
ext: "css"
});
});
it("only calls reload once if the array contains a filepath that will cause a reload", function () {
Expand All @@ -55,21 +57,36 @@ describe("API: .reload()", function () {
assert.equal(calls.callCount, 2);
sinon.assert.calledWithExactly(emitterStub, "file:changed", {
path: "css/core.css",
basename: "core.css",
log: true,
namespace: "core",
event: "change"
event: "change",
ext: "css"
});
sinon.assert.calledWithExactly(emitterStub, "file:changed", {
path: "ie.css",
basename: "ie.css",
log: true,
namespace: "core",
event: "change"
event: "change",
ext: "css"
});
});
it("should accept an array of file paths as strings", function () {
browserSync.reload(["index.html", "css/core.css"]);
sinon.assert.calledWithExactly(emitterStub, "browser:reload");
});
it("should accept wildcards for files extensions eg: *.css", function () {
browserSync.reload("*.css");
sinon.assert.calledWithExactly(emitterStub, "file:changed", {
path: "*.css",
basename: "*.css",
log: true,
namespace: "core",
event: "change",
ext: "css"
});
});
/**
* BACKWARDS COMPATIBILITY:
* This is an old signature that, whilst we must continue to support,
Expand Down
16 changes: 12 additions & 4 deletions test/specs/api/init.reload.stream.js
Expand Up @@ -35,9 +35,11 @@ describe("API: .stream()", function () {
stream.end();
sinon.assert.calledWithExactly(emitterStub, "file:changed", {
path: "styles.css",
basename: "styles.css",
log: false,
namespace: "core",
event: "change"
event: "change",
ext: "css"
});
});
it("should accept multiple files in stream", function () {
Expand All @@ -47,15 +49,19 @@ describe("API: .stream()", function () {
stream.end();
sinon.assert.calledWithExactly(emitterStub, "file:changed", {
path: "styles.css",
basename: "styles.css",
log: false,
namespace: "core",
event: "change"
event: "change",
ext: "css"
});
sinon.assert.calledWithExactly(emitterStub, "file:changed", {
path: "styles2.css",
basename: "styles2.css",
log: false,
namespace: "core",
event: "change"
event: "change",
ext: "css"
});
sinon.assert.calledWithExactly(emitterStub, "stream:changed", {
changed: ["styles.css", "styles2.css"]
Expand Down Expand Up @@ -87,9 +93,11 @@ describe("API: .stream()", function () {
sinon.assert.calledThrice(emitterStub);
sinon.assert.calledWithExactly(emitterStub, "file:changed", {
path: "/users/shane/styles.js",
basename: "styles.js",
log: false,
namespace: "core",
event: "change"
event: "change",
ext: "js"
});
sinon.assert.calledWithExactly(emitterStub, "stream:changed", {
changed: ["styles.js"]
Expand Down
4 changes: 3 additions & 1 deletion test/specs/api/init.reload.stream.noop.js
Expand Up @@ -31,9 +31,11 @@ describe("API: .stream() noop", function () {

sinon.assert.calledWithExactly(emitterStub, "file:changed", {
path: "styles.css",
basename: "styles.css",
log: false,
namespace: "core",
event: "change"
event: "change",
ext: "css"
});
done();
});
Expand Down
18 changes: 16 additions & 2 deletions test/specs/commands/reload.js
Expand Up @@ -53,7 +53,14 @@ describe("E2E CLI `reload` with no files arg", function () {
}
},
cb: function () {
sinon.assert.calledWithExactly(spy, "file:changed", {path: "core.css", log: true, namespace: "core", event: "change"});
sinon.assert.calledWithExactly(spy, "file:changed", {
path: "core.css",
basename: "core.css",
log: true,
namespace: "core",
event: "change",
ext: "css"
});
bs.cleanup();
done();
}
Expand All @@ -80,7 +87,14 @@ describe("E2E CLI `reload` with no files arg", function () {
}
},
cb: function () {
sinon.assert.calledWithExactly(spy, "file:changed", {path: "core.css", log: true, namespace: "core", event: "change"});
sinon.assert.calledWithExactly(spy, "file:changed", {
path: "core.css",
basename: "core.css",
ext: "css",
log: true,
namespace: "core",
event: "change"
});
bs.cleanup();
done();
}
Expand Down
4 changes: 2 additions & 2 deletions test/specs/e2e/e2e.events.js
Expand Up @@ -27,11 +27,11 @@ describe("E2E Events test", function () {

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

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

clock.tick();

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

spy.restore();
});
Expand Down
19 changes: 18 additions & 1 deletion test/specs/e2e/files/e2e.file.changed.js
Expand Up @@ -52,7 +52,24 @@ describe("E2E Responding to events", function () {
var args = socketsStub.getCall(0).args[1];

assert.equal(eventName, "file:reload"); // check correct event sent to client
assert.equal(args.assetFileName, "styles.css"); // Check the asset name is sent
assert.equal(args.basename, "styles.css"); // Check the asset name is sent
assert.isFalse(instance.paused);
});

it("fires the file:reload event to the browser when wildcard given", function () {

// Emit the event as it comes from the file-watcher
instance.events.emit("file:changed", {path: "*.css", event: "change", log: true, namespace: "core"});

clock.tick();

var eventName = socketsStub.getCall(0).args[0];
var args = socketsStub.getCall(0).args[1];

assert.equal(eventName, "file:reload"); // check correct event sent to client
assert.equal(args.path, "*.css"); // Check the asset name is sent
assert.equal(args.basename, "*.css"); // Check the asset name is sent
assert.equal(args.ext, "css"); // Check the asset name is sent
assert.isFalse(instance.paused);
});

Expand Down
18 changes: 16 additions & 2 deletions test/specs/http-protocol/http.reload.js
Expand Up @@ -51,7 +51,14 @@ describe("HTTP protocol", function () {

request(url, function (e, r, body) {
sinon.assert.calledWith(spy, "file:changed");
sinon.assert.calledWithExactly(spy, "file:changed", {path: "core.min.css", log: true, namespace: "core", event: "change"});
sinon.assert.calledWithExactly(spy, "file:changed", {
path: "core.min.css",
basename: "core.min.css",
ext: "css",
log: true,
namespace: "core",
event: "change"
});
assert.include(body, "Called public API method `.reload()`");
assert.include(body, "With args: [\"core.min.css\",\"core.css\"]");
done();
Expand All @@ -63,7 +70,14 @@ describe("HTTP protocol", function () {

request(url, function (e, r, body) {
sinon.assert.calledWith(spy, "file:changed");
sinon.assert.calledWithExactly(spy, "file:changed", {path: "somefile.php", log: true, namespace: "core", event: "change"});
sinon.assert.calledWithExactly(spy, "file:changed", {
path: "somefile.php",
basename: "somefile.php",
ext: "php",
log: true,
namespace: "core",
event: "change"
});
assert.include(body, "Called public API method `.reload()`");
assert.include(body, "With args: \"somefile.php\"");
done();
Expand Down

0 comments on commit a75dd5a

Please sign in to comment.