Skip to content

Commit

Permalink
Brought back support for ephemeral port switching (#490)
Browse files Browse the repository at this point in the history
  • Loading branch information
leo authored Nov 12, 2018
1 parent 596ae27 commit f8439ae
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion bin/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ const registerShutdown = (fn) => {
process.on('exit', wrapper);
};

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

server.on('error', (err) => {
if (err.code === 'EADDRINUSE' && endpoint.length === 1 && !isNaN(endpoint[0])) {
startEndpoint([0], config, args, endpoint[0]);
return;
}

console.error(error(`Failed to serve: ${err.stack}`));
process.exit(1);
});
Expand Down Expand Up @@ -207,6 +212,10 @@ const startEndpoint = (endpoint, config, args) => {
message += `\n${chalk.bold('- On Your Network:')} ${networkAddress}`;
}

if (previous) {
message += chalk.red(`\n\nThis port was picked because ${chalk.underline(previous)} is in use.`);
}

if (clipboard) {
try {
await copy(localAddress);
Expand Down

0 comments on commit f8439ae

Please sign in to comment.