Skip to content

Commit fb31770

Browse files
refactor: utils and server (#1943)
1 parent 8cf1053 commit fb31770

File tree

3 files changed

+38
-29
lines changed

3 files changed

+38
-29
lines changed

lib/Server.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,21 @@ class Server {
711711
});
712712
}
713713

714+
showStatus() {
715+
const suffix =
716+
this.options.inline !== false || this.options.lazy === true
717+
? '/'
718+
: '/webpack-dev-server/';
719+
const uri = `${createDomain(this.options, this.listeningApp)}${suffix}`;
720+
721+
status(
722+
uri,
723+
this.options,
724+
this.log,
725+
this.options.stats && this.options.stats.colors
726+
);
727+
}
728+
714729
listen(port, hostname, fn) {
715730
this.hostname = hostname;
716731

@@ -878,21 +893,6 @@ class Server {
878893
return false;
879894
}
880895

881-
showStatus() {
882-
const suffix =
883-
this.options.inline !== false || this.options.lazy === true
884-
? '/'
885-
: '/webpack-dev-server/';
886-
const uri = `${createDomain(this.options, this.listeningApp)}${suffix}`;
887-
888-
status(
889-
uri,
890-
this.options,
891-
this.log,
892-
this.options.stats && this.options.stats.colors
893-
);
894-
}
895-
896896
// eslint-disable-next-line
897897
sockWrite(sockets, type, data) {
898898
sockets.forEach((socket) => {

lib/utils/runOpen.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
const open = require('opn');
4+
5+
function runOpen(uri, options, log) {
6+
let openOptions = {};
7+
let openMessage = 'Unable to open browser';
8+
9+
if (typeof options.open === 'string') {
10+
openOptions = { app: options.open };
11+
openMessage += `: ${options.open}`;
12+
}
13+
14+
open(`${uri}${options.openPage || ''}`, openOptions).catch(() => {
15+
log.warn(
16+
`${openMessage}. If you are running in a headless environment, please do not use the --open flag`
17+
);
18+
});
19+
}
20+
21+
module.exports = runOpen;

lib/utils/status.js

+2-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

3-
const open = require('opn');
43
const colors = require('./colors');
4+
const runOpen = require('./runOpen');
55

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

4646
if (options.open) {
47-
let openOptions = {};
48-
let openMessage = 'Unable to open browser';
49-
50-
if (typeof options.open === 'string') {
51-
openOptions = { app: options.open };
52-
openMessage += `: ${options.open}`;
53-
}
54-
55-
open(`${uri}${options.openPage || ''}`, openOptions).catch(() => {
56-
log.warn(
57-
`${openMessage}. If you are running in a headless environment, please do not use the --open flag`
58-
);
59-
});
47+
runOpen(uri, options, log);
6048
}
6149
}
6250

0 commit comments

Comments
 (0)