Skip to content

Commit 87cfaa7

Browse files
refactor(cli): code (#1927)
1 parent e99fa3a commit 87cfaa7

File tree

5 files changed

+63
-51
lines changed

5 files changed

+63
-51
lines changed

bin/webpack-dev-server.js

Lines changed: 10 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,26 @@
22

33
'use strict';
44

5-
/* eslint-disable
6-
import/order,
7-
no-shadow,
8-
no-console
9-
*/
10-
const debug = require('debug')('webpack-dev-server');
5+
/* eslint-disable no-shadow, no-console */
116

127
const fs = require('fs');
138
const net = require('net');
14-
9+
const debug = require('debug')('webpack-dev-server');
1510
const importLocal = require('import-local');
16-
1711
const yargs = require('yargs');
1812
const webpack = require('webpack');
19-
20-
const options = require('./options');
2113
const Server = require('../lib/Server');
22-
14+
const setupExitSignals = require('../lib/utils/setupExitSignals');
2315
const colors = require('../lib/utils/colors');
24-
const createConfig = require('../lib/utils/createConfig');
16+
const processOptions = require('../lib/utils/processOptions');
2517
const createLogger = require('../lib/utils/createLogger');
2618
const findPort = require('../lib/utils/findPort');
2719
const getVersions = require('../lib/utils/getVersions');
20+
const options = require('./options');
2821

2922
let server;
3023

31-
// Taken out of yargs because we must know if
32-
// it wasn't given by the user, in which case
33-
// we should use portfinder.
34-
const DEFAULT_PORT = 8080;
35-
36-
const signals = ['SIGINT', 'SIGTERM'];
37-
38-
signals.forEach((signal) => {
39-
process.on(signal, () => {
40-
if (server) {
41-
server.close(() => {
42-
// eslint-disable-next-line no-process-exit
43-
process.exit();
44-
});
45-
} else {
46-
// eslint-disable-next-line no-process-exit
47-
process.exit();
48-
}
49-
});
50-
});
24+
setupExitSignals(server);
5125

5226
// Prefer the local installation of webpack-dev-server
5327
if (importLocal(__filename)) {
@@ -106,22 +80,6 @@ const config = require(convertArgvPath)(yargs, argv, {
10680
outputFilename: '/bundle.js',
10781
});
10882

109-
function processOptions(config) {
110-
// processOptions {Promise}
111-
if (typeof config.then === 'function') {
112-
config.then(processOptions).catch((err) => {
113-
console.error(err.stack || err);
114-
// eslint-disable-next-line no-process-exit
115-
process.exit();
116-
});
117-
118-
return;
119-
}
120-
121-
const options = createConfig(config, argv, { port: DEFAULT_PORT });
122-
startDevServer(config, options);
123-
}
124-
12583
function startDevServer(config, options) {
12684
const log = createLogger(options);
12785

@@ -185,6 +143,7 @@ function startDevServer(config, options) {
185143
if (err) {
186144
throw err;
187145
}
146+
188147
// chmod 666 (rw rw rw)
189148
const READ_WRITE = 438;
190149

@@ -210,4 +169,6 @@ function startDevServer(config, options) {
210169
}
211170
}
212171

213-
processOptions(config);
172+
processOptions(config, argv, (config, options) => {
173+
startDevServer(config, options);
174+
});

lib/utils/defaultPort.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
module.exports = 8080;

lib/utils/findPort.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
'use strict';
22

33
const { getPortPromise } = require('portfinder');
4+
const defaultPort = require('./defaultPort');
45
const defaultTo = require('./defaultTo');
56
const tryParseInt = require('./tryParseInt');
67

7-
const defaultPort = 8080;
8-
98
function findPort(port) {
109
if (typeof port !== 'undefined') {
1110
return Promise.resolve(port);
1211
}
12+
1313
// Try to find unused port and listen on it for 3 times,
1414
// if port is not specified in options.
1515
// Because NaN == null is false, defaultTo fails if parseInt returns NaN

lib/utils/processOptions.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
const createConfig = require('./createConfig');
4+
const defaultPort = require('./defaultPort');
5+
6+
function processOptions(config, argv, callback) {
7+
// processOptions {Promise}
8+
if (typeof config.then === 'function') {
9+
config.then(processOptions).catch((err) => {
10+
// eslint-disable-next-line no-console
11+
console.error(err.stack || err);
12+
// eslint-disable-next-line no-process-exit
13+
process.exit();
14+
});
15+
16+
return;
17+
}
18+
19+
// Taken out of yargs because we must know if
20+
// it wasn't given by the user, in which case
21+
// we should use portfinder.
22+
const options = createConfig(config, argv, { port: defaultPort });
23+
24+
callback(config, options);
25+
}
26+
27+
module.exports = processOptions;

lib/utils/setupExitSignals.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
const signals = ['SIGINT', 'SIGTERM'];
4+
5+
function setupExitSignals(server) {
6+
signals.forEach((signal) => {
7+
process.on(signal, () => {
8+
if (server) {
9+
server.close(() => {
10+
// eslint-disable-next-line no-process-exit
11+
process.exit();
12+
});
13+
} else {
14+
// eslint-disable-next-line no-process-exit
15+
process.exit();
16+
}
17+
});
18+
});
19+
}
20+
21+
module.exports = setupExitSignals;

0 commit comments

Comments
 (0)