Charles Lyding 5b8e2d5e57 fix(@angular-devkit/build-angular): ensure port 0 uses random port with Vite development server
Vite appears to consider a port value of `0` as a falsy value and use the default Vite port of
`5173` when zero is used as a value for the development server port option. To workaround this
issue, the port checker code now explicitly handles the zero value case and determines a random
port as would be done automatically by the Webpack-based development server.
2023-12-08 09:57:22 -05:00

71 lines
2.0 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import assert from 'node:assert';
import { AddressInfo, createServer } from 'node:net';
import { loadEsmModule } from './load-esm';
import { isTTY } from './tty';
function createInUseError(port: number): Error {
return new Error(`Port ${port} is already in use. Use '--port' to specify a different port.`);
}
export async function checkPort(port: number, host: string): Promise<number> {
// Disabled due to Vite not handling port 0 and instead always using the default value (5173)
// TODO: Enable this again once Vite is fixed
// if (port === 0) {
// return 0;
// }
return new Promise<number>((resolve, reject) => {
const server = createServer();
server
.once('error', (err: NodeJS.ErrnoException) => {
if (err.code !== 'EADDRINUSE') {
reject(err);
return;
}
if (!isTTY) {
reject(createInUseError(port));
return;
}
loadEsmModule<typeof import('inquirer')>('inquirer')
.then(({ default: { prompt } }) =>
prompt({
type: 'confirm',
name: 'useDifferent',
message: `Port ${port} is already in use.\nWould you like to use a different port?`,
default: true,
}),
)
.then(
(answers) =>
answers.useDifferent ? resolve(checkPort(0, host)) : reject(createInUseError(port)),
() => reject(createInUseError(port)),
);
})
.once('listening', () => {
// Get the actual address from the listening server instance
const address = server.address();
assert(
address && typeof address !== 'string',
'Port check server address should always be an object.',
);
server.close();
resolve(address.port);
})
.listen(port, host);
});
}