diff --git a/lib/cli/command.reload.js b/lib/cli/command.reload.js index bfa49f95e..50d8a8ddc 100644 --- a/lib/cli/command.reload.js +++ b/lib/cli/command.reload.js @@ -1,6 +1,6 @@ "use strict"; -var error = "Could not contact BrowserSync server."; +var error = "Could not contact BrowserSync server."; /** * $ browser-sync reload @@ -14,16 +14,25 @@ var error = "Could not contact BrowserSync server."; module.exports = function (opts) { var flags = opts.cli.flags; - var proto = require("../http-protocol"); + if (!flags.url) { + flags.url = "http://localhost:" + (flags.port || 3000); + } + var proto = require("../http-protocol"); + var scheme = flags.url.match(/^https/) ? "https" : "http"; - var url = proto.getUrl({method: "reload", args: flags.files}, "http://localhost:" + (flags.port || 3000)); + var url = proto.getUrl({method: "reload", args: flags.files}, flags.url); - require("http").get(url, function (res) { + require(scheme).get(url, function (res) { if (res.statusCode !== 200) { require("logger").logger.error(error); return opts.cb(new Error(error)); } else { opts.cb(null, res); } + }).on("error", function (err) { + if (err.code === "ECONNREFUSED") { + err.message = "BrowserSync not running at " + flags.url; + } + return opts.cb(err); }); }; diff --git a/lib/cli/opts.reload.json b/lib/cli/opts.reload.json index e9411cc45..359288b48 100644 --- a/lib/cli/opts.reload.json +++ b/lib/cli/opts.reload.json @@ -1,4 +1,5 @@ { "files": "File paths to reload", - "port": "Target a running instance by port number" + "port": "Target a running instance by port number", + "url": "Provide the full the url to the running BrowserSync instance" } diff --git a/test/specs/commands/reload.js b/test/specs/commands/reload.js index 9b5ec1553..38e32b7cd 100644 --- a/test/specs/commands/reload.js +++ b/test/specs/commands/reload.js @@ -5,6 +5,7 @@ var browserSync = require(path.resolve("./")); var pkg = require(path.resolve("package.json")); var sinon = require("sinon"); +var assert = require("chai").assert; var cli = require(path.resolve(pkg.bin)); describe("E2E CLI `reload` with no files arg", function () { @@ -59,4 +60,44 @@ describe("E2E CLI `reload` with no files arg", function () { }); }); }); + it("should make a http request with files arg over HTTPS", function (done) { + + browserSync.reset(); + process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0; + + browserSync + .create() + .init({server: "test/fixtures", open: false, https: true}, function (err, bs) { + + var spy = sinon.spy(bs.events, "emit"); + + cli({ + cli: { + input: ["reload"], + flags: { + url: bs.options.getIn(["urls", "local"]), + files: "core.css" + } + }, + cb: function () { + sinon.assert.calledWithExactly(spy, "file:changed", {path: "core.css", log: true, namespace: "core"}); + bs.cleanup(); + done(); + } + }); + }); + }); + it("should handle ECONNREFUSED errors nicely", function (done) { + cli({ + cli: { + input: ["reload"], + flags: {} + }, + cb: function (err) { + assert.equal(err.code, "ECONNREFUSED"); + assert.equal(err.message, "BrowserSync not running at http://localhost:3000"); + done(); + } + }); + }); });