Skip to content

Commit 3aced70

Browse files
committed
Merge branch 'alexturpin'
2 parents 42bab46 + 8dd9f01 commit 3aced70

5 files changed

Lines changed: 27 additions & 12 deletions

File tree

lib/internal-events.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ module.exports = function (bs) {
4242
var open = bs.options.get("open");
4343

4444
if (mode === "proxy" || mode === "server" || open === "ui" || open === "ui-external") {
45-
utils.openBrowser(data.url, bs.options);
45+
utils.openBrowser(data.url, bs.options, bs);
4646
}
4747

4848
// log about any file watching

lib/logger.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ module.exports.callbacks = {
6767
logger.info("{cyan:Reloading Browsers...");
6868
}
6969
},
70+
/**
71+
*
72+
*/
73+
"browser:error": function () {
74+
logger.error("Couldn't open browser (if you are using BrowserSync in a headless environment, you might want to set the {cyan:open} option to {cyan:false})");
75+
},
7076
/**
7177
* @param {BrowserSync} bs
7278
* @param data

lib/utils.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,9 @@ var utils = {
139139
* Open the page in browser
140140
* @param {String} url
141141
* @param {Object} options
142+
* @param {BrowserSync} bs
142143
*/
143-
openBrowser: function (url, options) {
144+
openBrowser: function (url, options, bs) {
144145

145146
var open = options.get("open");
146147
var browser = options.get("browser");
@@ -155,22 +156,23 @@ var utils = {
155156
if (browser !== "default") {
156157
if (utils.isList(browser)) {
157158
browser.forEach(function (browser) {
158-
utils.open(url, browser);
159+
utils.open(url, browser, bs);
159160
});
160161
} else {
161-
utils.open(url, browser); // single
162+
utils.open(url, browser, bs); // single
162163
}
163164
} else {
164-
utils.open(url);
165+
utils.open(url, null, bs);
165166
}
166167
}
167168
},
168169
/**
169-
* Wrapper for open module - for easier stubbin'
170+
* Wrapper for opn module
170171
* @param url
171172
* @param name
173+
* @param bs
172174
*/
173-
open: function (url, name) {
175+
open: function (url, name, bs) {
174176
var options = (function () {
175177
if (_.isString(name)) {
176178
return {app: name};
@@ -180,7 +182,10 @@ var utils = {
180182
}
181183
return {};
182184
})();
183-
require("opn")(url, options);
185+
var opn = require("opn");
186+
opn(url, options).catch(function() {
187+
bs.events.emit("browser:error");
188+
});
184189
},
185190
/**
186191
* @param {Boolean} kill

test/specs/e2e/e2e.options.open.browsers.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ describe("E2E browser option with app args", function () {
9494
var stub = sinon.spy(utils, "open");
9595
var opnPath = require.resolve("opn");
9696
require(opnPath);
97-
var opnStub = require("sinon").stub(require.cache[opnPath], "exports");
97+
var opnStub = require("sinon").stub(require.cache[opnPath], "exports")
98+
.returns({catch: function(){}});
9899

99100
browserSync(config, function (err, bs) {
100101
bs.cleanup();
@@ -126,7 +127,8 @@ describe("E2E browser option with app args", function () {
126127
var stub = sinon.spy(utils, "open");
127128
var opnPath = require.resolve("opn");
128129
require(opnPath);
129-
var opnStub = require("sinon").stub(require.cache[opnPath], "exports");
130+
var opnStub = require("sinon").stub(require.cache[opnPath], "exports")
131+
.returns({catch: function(){}});
130132

131133
browserSync(config, function (err, bs) {
132134
bs.cleanup();

test/specs/e2e/e2e.options.open.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ describe("E2E OPEN options with external", function () {
5050
stub = sinon.spy(utils, "open");
5151
opnPath = require.resolve("opn");
5252
require(opnPath);
53-
opnStub = require("sinon").stub(require.cache[opnPath], "exports");
53+
opnStub = require("sinon").stub(require.cache[opnPath], "exports")
54+
.returns({catch: function(){}});
5455
bs = browserSync(config, done).instance;
5556
});
5657

@@ -81,7 +82,8 @@ describe("E2E OPEN options with UI + snippet", function () {
8182
stub = sinon.spy(utils, "open");
8283
opnPath = require.resolve("opn");
8384
require(opnPath);
84-
opnStub = require("sinon").stub(require.cache[opnPath], "exports");
85+
opnStub = require("sinon").stub(require.cache[opnPath], "exports")
86+
.returns({catch: function(){}});
8587
bs = browserSync(config, done).instance;
8688
});
8789

0 commit comments

Comments
 (0)