Skip to content

Commit

Permalink
refactor: utils and server (#1943)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Jun 1, 2019
1 parent 8cf1053 commit fb31770
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 29 deletions.
30 changes: 15 additions & 15 deletions lib/Server.js
Expand Up @@ -711,6 +711,21 @@ class Server {
});
}

showStatus() {
const suffix =
this.options.inline !== false || this.options.lazy === true
? '/'
: '/webpack-dev-server/';
const uri = `${createDomain(this.options, this.listeningApp)}${suffix}`;

status(
uri,
this.options,
this.log,
this.options.stats && this.options.stats.colors
);
}

listen(port, hostname, fn) {
this.hostname = hostname;

Expand Down Expand Up @@ -878,21 +893,6 @@ class Server {
return false;
}

showStatus() {
const suffix =
this.options.inline !== false || this.options.lazy === true
? '/'
: '/webpack-dev-server/';
const uri = `${createDomain(this.options, this.listeningApp)}${suffix}`;

status(
uri,
this.options,
this.log,
this.options.stats && this.options.stats.colors
);
}

// eslint-disable-next-line
sockWrite(sockets, type, data) {
sockets.forEach((socket) => {
Expand Down
21 changes: 21 additions & 0 deletions lib/utils/runOpen.js
@@ -0,0 +1,21 @@
'use strict';

const open = require('opn');

function runOpen(uri, options, log) {
let openOptions = {};
let openMessage = 'Unable to open browser';

if (typeof options.open === 'string') {
openOptions = { app: options.open };
openMessage += `: ${options.open}`;
}

open(`${uri}${options.openPage || ''}`, openOptions).catch(() => {
log.warn(
`${openMessage}. If you are running in a headless environment, please do not use the --open flag`
);
});
}

module.exports = runOpen;
16 changes: 2 additions & 14 deletions lib/utils/status.js
@@ -1,7 +1,7 @@
'use strict';

const open = require('opn');
const colors = require('./colors');
const runOpen = require('./runOpen');

// TODO: don't emit logs when webpack-dev-server is used via Node.js API
function status(uri, options, log, useColor) {
Expand Down Expand Up @@ -44,19 +44,7 @@ function status(uri, options, log, useColor) {
}

if (options.open) {
let openOptions = {};
let openMessage = 'Unable to open browser';

if (typeof options.open === 'string') {
openOptions = { app: options.open };
openMessage += `: ${options.open}`;
}

open(`${uri}${options.openPage || ''}`, openOptions).catch(() => {
log.warn(
`${openMessage}. If you are running in a headless environment, please do not use the --open flag`
);
});
runOpen(uri, options, log);
}
}

Expand Down

0 comments on commit fb31770

Please sign in to comment.