Skip to content

Commit f8439ae

Browse files
authored
Brought back support for ephemeral port switching (#490)
1 parent 596ae27 commit f8439ae

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

bin/serve.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ const registerShutdown = (fn) => {
154154
process.on('exit', wrapper);
155155
};
156156

157-
const startEndpoint = (endpoint, config, args) => {
157+
const startEndpoint = (endpoint, config, args, previous) => {
158158
const {isTTY} = process.stdout;
159159
const clipboard = args['--no-clipboard'] !== true;
160160
const compress = args['--no-compression'] !== true;
@@ -168,6 +168,11 @@ const startEndpoint = (endpoint, config, args) => {
168168
});
169169

170170
server.on('error', (err) => {
171+
if (err.code === 'EADDRINUSE' && endpoint.length === 1 && !isNaN(endpoint[0])) {
172+
startEndpoint([0], config, args, endpoint[0]);
173+
return;
174+
}
175+
171176
console.error(error(`Failed to serve: ${err.stack}`));
172177
process.exit(1);
173178
});
@@ -207,6 +212,10 @@ const startEndpoint = (endpoint, config, args) => {
207212
message += `\n${chalk.bold('- On Your Network:')} ${networkAddress}`;
208213
}
209214

215+
if (previous) {
216+
message += chalk.red(`\n\nThis port was picked because ${chalk.underline(previous)} is in use.`);
217+
}
218+
210219
if (clipboard) {
211220
try {
212221
await copy(localAddress);

0 commit comments

Comments
 (0)