(
preferredPort,
{ bindHost = "127.0.0.1", reservedPorts = [] } = {},
)
| 349 | * @returns {Promise<number>} The port the frontend should bind |
| 350 | */ |
| 351 | export async function resolveFrontendPort( |
| 352 | preferredPort, |
| 353 | { bindHost = "127.0.0.1", reservedPorts = [] } = {}, |
| 354 | ) { |
| 355 | const answersOnLoopback = async (port) => |
| 356 | ( |
| 357 | await Promise.all([ |
| 358 | isPortBusy(port, "127.0.0.1"), |
| 359 | isPortBusy(port, "::1"), |
| 360 | ]) |
| 361 | ).includes(true); |
| 362 | const isUsable = async (port) => |
| 363 | !reservedPorts.includes(port) && |
| 364 | (await tryPort(port, bindHost)) && |
| 365 | !(await answersOnLoopback(port)); |
| 366 | |
| 367 | if (await isUsable(preferredPort)) return preferredPort; |
| 368 | |
| 369 | const maxAttempts = 100; |
| 370 | for (let attempt = 0; attempt < maxAttempts; attempt += 1) { |
| 371 | const port = await findFreePort(0, bindHost); |
| 372 | if (await isUsable(port)) return port; |
| 373 | } |
| 374 | throw new Error( |
| 375 | `Could not allocate a frontend port after ${maxAttempts} attempts`, |
| 376 | ); |
| 377 | } |
| 378 | |
| 379 | export function formatMissingUvxGuidance(cwd = process.cwd()) { |
| 380 | const readmePath = path.join(cwd, "README.md"); |
no test coverage detected