Skip to content

Commit 0538e61

Browse files
hiroppyevilebottnawi
authored andcommitted
refactor: put socket processing back to /bin (#1869)
1 parent 1dd0e3c commit 0538e61

File tree

3 files changed

+66
-50
lines changed

3 files changed

+66
-50
lines changed

bin/webpack-dev-server.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,19 @@ function startDevServer(config, options) {
192192
}
193193
});
194194

195-
runServer();
195+
server.listen(options.socket, options.host, (err) => {
196+
if (err) {
197+
throw err;
198+
}
199+
// chmod 666 (rw rw rw)
200+
const READ_WRITE = 438;
201+
202+
fs.chmod(options.socket, READ_WRITE, (err) => {
203+
if (err) {
204+
throw err;
205+
}
206+
});
207+
});
196208
} else if (options.port) {
197209
runServer();
198210
} else {

lib/Server.js

+16-30
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,21 @@ class Server {
741741
return false;
742742
}
743743

744+
showStatus() {
745+
const suffix =
746+
this.options.inline !== false || this.options.lazy === true
747+
? '/'
748+
: '/webpack-dev-server/';
749+
const uri = `${createDomain(this.options, this.listeningApp)}${suffix}`;
750+
751+
status(
752+
uri,
753+
this.options,
754+
this.log,
755+
this.options.stats && this.options.stats.colors
756+
);
757+
}
758+
744759
// delegate listen call and init sockjs
745760
listen(port, hostname, fn) {
746761
this.hostname = hostname;
@@ -816,36 +831,7 @@ class Server {
816831
runBonjour(this.options);
817832
}
818833

819-
const showStatus = () => {
820-
const suffix =
821-
this.options.inline !== false || this.options.lazy === true
822-
? '/'
823-
: '/webpack-dev-server/';
824-
825-
const uri = `${createDomain(this.options, this.listeningApp)}${suffix}`;
826-
827-
status(
828-
uri,
829-
this.options,
830-
this.log,
831-
this.options.stats && this.options.stats.colors
832-
);
833-
};
834-
835-
if (this.options.socket) {
836-
// chmod 666 (rw rw rw)
837-
const READ_WRITE = 438;
838-
839-
fs.chmod(this.options.socket, READ_WRITE, (err) => {
840-
if (err) {
841-
throw err;
842-
}
843-
844-
showStatus();
845-
});
846-
} else {
847-
showStatus();
848-
}
834+
this.showStatus();
849835

850836
if (fn) {
851837
fn.call(this.listeningApp, err);

test/cli.test.js

+37-19
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,23 @@
33
/* eslint-disable
44
array-bracket-spacing,
55
*/
6-
const path = require('path');
6+
const { unlink } = require('fs');
7+
const { join, resolve } = require('path');
78
const execa = require('execa');
89
const runDevServer = require('./helpers/run-webpack-dev-server');
910

10-
const httpsCertificateDirectory = path.join(
11-
__dirname,
12-
'fixtures/https-certificate'
13-
);
14-
const caPath = path.join(httpsCertificateDirectory, 'ca.pem');
15-
const pfxPath = path.join(httpsCertificateDirectory, 'server.pfx');
16-
const keyPath = path.join(httpsCertificateDirectory, 'server.key');
17-
const certPath = path.join(httpsCertificateDirectory, 'server.crt');
11+
const httpsCertificateDirectory = join(__dirname, 'fixtures/https-certificate');
12+
const caPath = join(httpsCertificateDirectory, 'ca.pem');
13+
const pfxPath = join(httpsCertificateDirectory, 'server.pfx');
14+
const keyPath = join(httpsCertificateDirectory, 'server.key');
15+
const certPath = join(httpsCertificateDirectory, 'server.crt');
1816

1917
describe('CLI', () => {
2018
it('--progress', (done) => {
2119
runDevServer('--progress')
2220
.then((output) => {
2321
expect(output.code).toEqual(0);
24-
expect(output.stderr.indexOf('0% compiling') >= 0).toBe(true);
22+
expect(output.stderr.includes('0% compiling')).toBe(true);
2523
done();
2624
})
2725
.catch(done);
@@ -31,7 +29,7 @@ describe('CLI', () => {
3129
runDevServer('--bonjour')
3230
.then((output) => {
3331
expect(output.code).toEqual(0);
34-
expect(output.stdout.indexOf('Bonjour') >= 0).toBe(true);
32+
expect(output.stdout.includes('Bonjour')).toBe(true);
3533
done();
3634
})
3735
.catch(done);
@@ -41,7 +39,7 @@ describe('CLI', () => {
4139
runDevServer('--https')
4240
.then((output) => {
4341
expect(output.code).toEqual(0);
44-
expect(output.stdout.indexOf('Project is running at') >= 0).toBe(true);
42+
expect(output.stdout.includes('Project is running at')).toBe(true);
4543
done();
4644
})
4745
.catch(done);
@@ -53,7 +51,7 @@ describe('CLI', () => {
5351
)
5452
.then((output) => {
5553
expect(output.code).toEqual(0);
56-
expect(output.stdout.indexOf('Project is running at') >= 0).toBe(true);
54+
expect(output.stdout.includes('Project is running at')).toBe(true);
5755
done();
5856
})
5957
.catch(done);
@@ -82,9 +80,29 @@ describe('CLI', () => {
8280
.catch(done);
8381
});
8482

83+
// The Unix socket to listen to (instead of a host).
84+
it('--socket', (done) => {
85+
const socketPath = join('.', 'webpack.sock');
86+
87+
runDevServer(`--socket ${socketPath}`)
88+
.then((output) => {
89+
expect(output.code).toEqual(0);
90+
91+
if (process.platform === 'win32') {
92+
done();
93+
} else {
94+
expect(output.stdout.includes(socketPath)).toBe(true);
95+
unlink(socketPath, () => {
96+
done();
97+
});
98+
}
99+
})
100+
.catch(done);
101+
});
102+
85103
it('should exit the process when SIGINT is detected', (done) => {
86-
const cliPath = path.resolve(__dirname, '../bin/webpack-dev-server.js');
87-
const examplePath = path.resolve(__dirname, '../examples/cli/public');
104+
const cliPath = resolve(__dirname, '../bin/webpack-dev-server.js');
105+
const examplePath = resolve(__dirname, '../examples/cli/public');
88106
const cp = execa('node', [cliPath], { cwd: examplePath });
89107

90108
cp.stdout.on('data', (data) => {
@@ -101,8 +119,8 @@ describe('CLI', () => {
101119
});
102120

103121
it('should exit the process when SIGINT is detected, even before the compilation is done', (done) => {
104-
const cliPath = path.resolve(__dirname, '../bin/webpack-dev-server.js');
105-
const examplePath = path.resolve(__dirname, '../examples/cli/public');
122+
const cliPath = resolve(__dirname, '../bin/webpack-dev-server.js');
123+
const examplePath = resolve(__dirname, '../examples/cli/public');
106124
const cp = execa('node', [cliPath], { cwd: examplePath });
107125
let killed = false;
108126

@@ -120,8 +138,8 @@ describe('CLI', () => {
120138
});
121139

122140
it('should use different random port when multiple instances are started on different processes', (done) => {
123-
const cliPath = path.resolve(__dirname, '../bin/webpack-dev-server.js');
124-
const examplePath = path.resolve(__dirname, '../examples/cli/public');
141+
const cliPath = resolve(__dirname, '../bin/webpack-dev-server.js');
142+
const examplePath = resolve(__dirname, '../examples/cli/public');
125143

126144
const cp = execa('node', [cliPath], { cwd: examplePath });
127145
const cp2 = execa('node', [cliPath], { cwd: examplePath });

0 commit comments

Comments
 (0)