Skip to content

Commit

Permalink
fix(server): set port before instantiating server (webpack#2143)
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldenning authored and knagaitsev committed Jul 31, 2019
1 parent 543bbe1 commit f5791a0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
16 changes: 4 additions & 12 deletions bin/webpack-dev-server.js
Expand Up @@ -15,7 +15,6 @@ const setupExitSignals = require('../lib/utils/setupExitSignals');
const colors = require('../lib/utils/colors');
const processOptions = require('../lib/utils/processOptions');
const createLogger = require('../lib/utils/createLogger');
const findPort = require('../lib/utils/findPort');
const getVersions = require('../lib/utils/getVersions');
const options = require('./options');

Expand Down Expand Up @@ -148,18 +147,11 @@ function startDevServer(config, options) {
});
});
} else {
findPort(options.port)
.then((port) => {
options.port = port;
server.listen(options.port, options.host, (err) => {
if (err) {
throw err;
}
});
})
.catch((err) => {
server.listen(options.port, options.host, (err) => {
if (err) {
throw err;
});
}
});
}
}

Expand Down
17 changes: 16 additions & 1 deletion lib/utils/processOptions.js
Expand Up @@ -2,6 +2,7 @@

const createConfig = require('./createConfig');
const defaultPort = require('./defaultPort');
const findPort = require('./findPort');

function processOptions(config, argv, callback) {
// processOptions {Promise}
Expand All @@ -23,7 +24,21 @@ function processOptions(config, argv, callback) {
// we should use portfinder.
const options = createConfig(config, argv, { port: defaultPort });

callback(config, options);
if (options.socket) {
callback(config, options);
} else {
findPort(options.port)
.then((port) => {
options.port = port;
callback(config, options);
})
.catch((err) => {
// eslint-disable-next-line no-console
console.error(err.stack || err);
// eslint-disable-next-line no-process-exit
process.exit(1);
});
}
}

module.exports = processOptions;
35 changes: 28 additions & 7 deletions test/cli/cli.test.js
Expand Up @@ -62,15 +62,36 @@ describe('CLI', () => {
expect(stdout.includes('Project is running at')).toBe(true);
});

it('--sockPath', async () => {
const { stdout } = await testBin('--sockPath /mysockPath');
expect(stdout.includes('http://localhost&sockPath=/mysockPath')).toBe(true);
it('--sockPath', (done) => {
testBin('--sockPath /mysockPath')
.then((output) => {
expect(
/http:\/\/localhost:[0-9]+&sockPath=\/mysockPath/.test(output.stdout)
).toEqual(true);
done();
})
.catch(done);
});

it('unspecified port', (done) => {
testBin('')
.then((output) => {
expect(/http:\/\/localhost:[0-9]+/.test(output.stdout)).toEqual(true);
done();
})
.catch(done);
});

it('--color', async () => {
const { stdout } = await testBin('--color');
// https://github.com/webpack/webpack-dev-server/blob/master/lib/utils/colors.js
expect(stdout.includes('\u001b[39m \u001b[90m「wds」\u001b[39m:')).toBe(true);
it('--color', (done) => {
testBin('--color')
.then((output) => {
// https://github.com/webpack/webpack-dev-server/blob/master/lib/utils/colors.js
expect(
output.stdout.includes('\u001b[39m \u001b[90m「wds」\u001b[39m:')
).toEqual(true);
done();
})
.catch(done);
});

// The Unix socket to listen to (instead of a host).
Expand Down

0 comments on commit f5791a0

Please sign in to comment.