From 4045ce3a36e4a02881c6628ed20dc70e7cb0c8fa Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Tue, 3 May 2022 15:15:13 +0800 Subject: [PATCH] fixes --- .../{commandUtils.ts => getHostPort.ts} | 18 ++++++++---------- packages/docusaurus/src/commands/serve.ts | 9 ++------- packages/docusaurus/src/commands/start.ts | 9 ++------- .../src/server/plugins/__tests__/init.test.ts | 4 +--- 4 files changed, 13 insertions(+), 27 deletions(-) rename packages/docusaurus/src/commands/{commandUtils.ts => getHostPort.ts} (50%) diff --git a/packages/docusaurus/src/commands/commandUtils.ts b/packages/docusaurus/src/commands/getHostPort.ts similarity index 50% rename from packages/docusaurus/src/commands/commandUtils.ts rename to packages/docusaurus/src/commands/getHostPort.ts index 85467e866b72b..771688a2b3d04 100644 --- a/packages/docusaurus/src/commands/commandUtils.ts +++ b/packages/docusaurus/src/commands/getHostPort.ts @@ -13,14 +13,12 @@ export type HostPortOptions = { port?: string; }; -export function getCLIOptionHost(hostOption: HostPortOptions['host']): string { - return hostOption ?? 'localhost'; -} - -export async function getCLIOptionPort( - portOption: HostPortOptions['port'], - host: string, -): Promise { - const basePort = portOption ? parseInt(portOption, 10) : DEFAULT_PORT; - return choosePort(host, basePort); +export async function getHostPort(options: HostPortOptions): Promise<{ + host: string; + port: number | null; +}> { + const host = options.host ?? 'localhost'; + const basePort = options.port ? parseInt(options.port, 10) : DEFAULT_PORT; + const port = await choosePort(host, basePort); + return {host, port}; } diff --git a/packages/docusaurus/src/commands/serve.ts b/packages/docusaurus/src/commands/serve.ts index d689b40ad6708..21e6846c66303 100644 --- a/packages/docusaurus/src/commands/serve.ts +++ b/packages/docusaurus/src/commands/serve.ts @@ -12,11 +12,7 @@ import path from 'path'; import type {LoadContextOptions} from '../server'; import {loadSiteConfig} from '../server/config'; import {build} from './build'; -import { - getCLIOptionHost, - getCLIOptionPort, - type HostPortOptions, -} from './commandUtils'; +import {getHostPort, type HostPortOptions} from './getHostPort'; import {DEFAULT_BUILD_DIR_NAME} from '@docusaurus/utils'; export type ServeCLIOptions = HostPortOptions & @@ -43,8 +39,7 @@ export async function serve( ); } - const host: string = getCLIOptionHost(cliOptions.host); - const port: number | null = await getCLIOptionPort(cliOptions.port, host); + const {host, port} = await getHostPort(cliOptions); if (port === null) { process.exit(); diff --git a/packages/docusaurus/src/commands/start.ts b/packages/docusaurus/src/commands/start.ts index d5e6e42306c06..8f19f3e3e3982 100644 --- a/packages/docusaurus/src/commands/start.ts +++ b/packages/docusaurus/src/commands/start.ts @@ -24,11 +24,7 @@ import { applyConfigurePostCss, getHttpsConfig, } from '../webpack/utils'; -import { - getCLIOptionHost, - getCLIOptionPort, - type HostPortOptions, -} from './commandUtils'; +import {getHostPort, type HostPortOptions} from './getHostPort'; import {getTranslationsLocaleDirPath} from '../server/translations/translations'; export type StartCLIOptions = HostPortOptions & @@ -60,8 +56,7 @@ export async function start( const protocol: string = process.env.HTTPS === 'true' ? 'https' : 'http'; - const host: string = getCLIOptionHost(cliOptions.host); - const port: number | null = await getCLIOptionPort(cliOptions.port, host); + const {host, port} = await getHostPort(cliOptions); if (port === null) { process.exit(); diff --git a/packages/docusaurus/src/server/plugins/__tests__/init.test.ts b/packages/docusaurus/src/server/plugins/__tests__/init.test.ts index 3ecf57b0d8609..adcd9147dcb66 100644 --- a/packages/docusaurus/src/server/plugins/__tests__/init.test.ts +++ b/packages/docusaurus/src/server/plugins/__tests__/init.test.ts @@ -37,9 +37,7 @@ describe('initPlugins', () => { it('throws user-friendly error message for plugins with bad values', async () => { await expect(() => - loadSite({ - customConfigFilePath: 'badPlugins.docusaurus.config.js', - }), + loadSite({config: 'badPlugins.docusaurus.config.js'}), ).rejects.toThrowErrorMatchingSnapshot(); }); });