diff --git a/bin/webpack-dev-server.js b/bin/webpack-dev-server.js index 8001c4f57f..898190df52 100755 --- a/bin/webpack-dev-server.js +++ b/bin/webpack-dev-server.js @@ -192,7 +192,19 @@ function startDevServer(config, options) { } }); - runServer(); + server.listen(options.socket, options.host, (err) => { + if (err) { + throw err; + } + // chmod 666 (rw rw rw) + const READ_WRITE = 438; + + fs.chmod(options.socket, READ_WRITE, (err) => { + if (err) { + throw err; + } + }); + }); } else if (options.port) { runServer(); } else { diff --git a/lib/Server.js b/lib/Server.js index 2c5ef5f402..ef4bd2a5f4 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -741,6 +741,21 @@ 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 + ); + } + // delegate listen call and init sockjs listen(port, hostname, fn) { this.hostname = hostname; @@ -816,36 +831,7 @@ class Server { runBonjour(this.options); } - const 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 - ); - }; - - if (this.options.socket) { - // chmod 666 (rw rw rw) - const READ_WRITE = 438; - - fs.chmod(this.options.socket, READ_WRITE, (err) => { - if (err) { - throw err; - } - - showStatus(); - }); - } else { - showStatus(); - } + this.showStatus(); if (fn) { fn.call(this.listeningApp, err); diff --git a/test/cli.test.js b/test/cli.test.js index 69b2128169..5f65afaac4 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -3,25 +3,23 @@ /* eslint-disable array-bracket-spacing, */ -const path = require('path'); +const { unlink } = require('fs'); +const { join, resolve } = require('path'); const execa = require('execa'); const runDevServer = require('./helpers/run-webpack-dev-server'); -const httpsCertificateDirectory = path.join( - __dirname, - 'fixtures/https-certificate' -); -const caPath = path.join(httpsCertificateDirectory, 'ca.pem'); -const pfxPath = path.join(httpsCertificateDirectory, 'server.pfx'); -const keyPath = path.join(httpsCertificateDirectory, 'server.key'); -const certPath = path.join(httpsCertificateDirectory, 'server.crt'); +const httpsCertificateDirectory = join(__dirname, 'fixtures/https-certificate'); +const caPath = join(httpsCertificateDirectory, 'ca.pem'); +const pfxPath = join(httpsCertificateDirectory, 'server.pfx'); +const keyPath = join(httpsCertificateDirectory, 'server.key'); +const certPath = join(httpsCertificateDirectory, 'server.crt'); describe('CLI', () => { it('--progress', (done) => { runDevServer('--progress') .then((output) => { expect(output.code).toEqual(0); - expect(output.stderr.indexOf('0% compiling') >= 0).toBe(true); + expect(output.stderr.includes('0% compiling')).toBe(true); done(); }) .catch(done); @@ -31,7 +29,7 @@ describe('CLI', () => { runDevServer('--bonjour') .then((output) => { expect(output.code).toEqual(0); - expect(output.stdout.indexOf('Bonjour') >= 0).toBe(true); + expect(output.stdout.includes('Bonjour')).toBe(true); done(); }) .catch(done); @@ -41,7 +39,7 @@ describe('CLI', () => { runDevServer('--https') .then((output) => { expect(output.code).toEqual(0); - expect(output.stdout.indexOf('Project is running at') >= 0).toBe(true); + expect(output.stdout.includes('Project is running at')).toBe(true); done(); }) .catch(done); @@ -53,7 +51,7 @@ describe('CLI', () => { ) .then((output) => { expect(output.code).toEqual(0); - expect(output.stdout.indexOf('Project is running at') >= 0).toBe(true); + expect(output.stdout.includes('Project is running at')).toBe(true); done(); }) .catch(done); @@ -82,9 +80,29 @@ describe('CLI', () => { .catch(done); }); + // The Unix socket to listen to (instead of a host). + it('--socket', (done) => { + const socketPath = join('.', 'webpack.sock'); + + runDevServer(`--socket ${socketPath}`) + .then((output) => { + expect(output.code).toEqual(0); + + if (process.platform === 'win32') { + done(); + } else { + expect(output.stdout.includes(socketPath)).toBe(true); + unlink(socketPath, () => { + done(); + }); + } + }) + .catch(done); + }); + it('should exit the process when SIGINT is detected', (done) => { - const cliPath = path.resolve(__dirname, '../bin/webpack-dev-server.js'); - const examplePath = path.resolve(__dirname, '../examples/cli/public'); + const cliPath = resolve(__dirname, '../bin/webpack-dev-server.js'); + const examplePath = resolve(__dirname, '../examples/cli/public'); const cp = execa('node', [cliPath], { cwd: examplePath }); cp.stdout.on('data', (data) => { @@ -101,8 +119,8 @@ describe('CLI', () => { }); it('should exit the process when SIGINT is detected, even before the compilation is done', (done) => { - const cliPath = path.resolve(__dirname, '../bin/webpack-dev-server.js'); - const examplePath = path.resolve(__dirname, '../examples/cli/public'); + const cliPath = resolve(__dirname, '../bin/webpack-dev-server.js'); + const examplePath = resolve(__dirname, '../examples/cli/public'); const cp = execa('node', [cliPath], { cwd: examplePath }); let killed = false; @@ -120,8 +138,8 @@ describe('CLI', () => { }); it('should use different random port when multiple instances are started on different processes', (done) => { - const cliPath = path.resolve(__dirname, '../bin/webpack-dev-server.js'); - const examplePath = path.resolve(__dirname, '../examples/cli/public'); + const cliPath = resolve(__dirname, '../bin/webpack-dev-server.js'); + const examplePath = resolve(__dirname, '../examples/cli/public'); const cp = execa('node', [cliPath], { cwd: examplePath }); const cp2 = execa('node', [cliPath], { cwd: examplePath });