From 070a1f1c93e87bff729db2981822010230cf5f3c Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Tue, 3 May 2022 15:15:13 +0800 Subject: [PATCH] fixes --- .../docusaurus/src/commands/commandUtils.ts | 26 ------------------- packages/docusaurus/src/commands/serve.ts | 9 ++----- packages/docusaurus/src/commands/start.ts | 9 ++----- .../server/{choosePort.ts => getHostPort.ts} | 18 ++++++++++++- .../src/server/plugins/__tests__/init.test.ts | 4 +-- 5 files changed, 22 insertions(+), 44 deletions(-) delete mode 100644 packages/docusaurus/src/commands/commandUtils.ts rename packages/docusaurus/src/server/{choosePort.ts => getHostPort.ts} (85%) diff --git a/packages/docusaurus/src/commands/commandUtils.ts b/packages/docusaurus/src/commands/commandUtils.ts deleted file mode 100644 index 85467e866b72..000000000000 --- a/packages/docusaurus/src/commands/commandUtils.ts +++ /dev/null @@ -1,26 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -import {choosePort} from '../server/choosePort'; -import {DEFAULT_PORT} from '@docusaurus/utils'; - -export type HostPortOptions = { - host?: string; - 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); -} diff --git a/packages/docusaurus/src/commands/serve.ts b/packages/docusaurus/src/commands/serve.ts index d689b40ad670..044bd04ee684 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 '../server/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 d5e6e42306c0..8dee7788911b 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 '../server/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/choosePort.ts b/packages/docusaurus/src/server/getHostPort.ts similarity index 85% rename from packages/docusaurus/src/server/choosePort.ts rename to packages/docusaurus/src/server/getHostPort.ts index 53a5b5c3cf84..c9792a45ec65 100644 --- a/packages/docusaurus/src/server/choosePort.ts +++ b/packages/docusaurus/src/server/getHostPort.ts @@ -8,6 +8,7 @@ import {execSync, type ExecSyncOptionsWithStringEncoding} from 'child_process'; import detect from 'detect-port'; import logger from '@docusaurus/logger'; +import {DEFAULT_PORT} from '@docusaurus/utils'; import prompts from 'prompts'; const execOptions: ExecSyncOptionsWithStringEncoding = { @@ -48,7 +49,7 @@ function getProcessForPort(port: number): string | null { * port is already being used. This feature was heavily inspired by * create-react-app and uses many of the same utility functions to implement it. */ -export async function choosePort( +async function choosePort( host: string, defaultPort: number, ): Promise { @@ -85,3 +86,18 @@ Would you like to run the app on another port instead?`), throw err; } } + +export type HostPortOptions = { + host?: string; + port?: string; +}; + +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/server/plugins/__tests__/init.test.ts b/packages/docusaurus/src/server/plugins/__tests__/init.test.ts index 3ecf57b0d860..adcd9147dcb6 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(); }); });