export async function choosePort()

in packages/docusaurus/src/server/choosePort.ts [71:107]


export async function choosePort(
  host: string,
  defaultPort: number,
): Promise<number | null> {
  try {
    const port = await detect({port: defaultPort, hostname: host});
    if (port === defaultPort) {
      return port;
    }
    const isRoot = process.getuid?.() === 0;
    const isInteractive = process.stdout.isTTY;
    const message =
      process.platform !== 'win32' && defaultPort < 1024 && !isRoot
        ? `Admin permissions are required to run a server on a port below 1024.`
        : `Something is already running on port ${defaultPort}.`;
    if (!isInteractive) {
      logger.error(message);
      return null;
    }
    clearConsole();
    const existingProcess = getProcessForPort(defaultPort);
    const {shouldChangePort} = await prompts({
      type: 'confirm',
      name: 'shouldChangePort',
      message: logger.yellow(`${logger.bold('[WARNING]')} ${message}${
        existingProcess ? ` Probably:\n  ${existingProcess}` : ''
      }

Would you like to run the app on another port instead?`),
      initial: true,
    });
    return shouldChangePort ? port : null;
  } catch (err) {
    logger.error`Could not find an open port at ${host}.`;
    throw err;
  }
}