Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed May 3, 2022
1 parent 2d904eb commit 4045ce3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<number | null> {
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};
}
9 changes: 2 additions & 7 deletions packages/docusaurus/src/commands/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 &
Expand All @@ -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();
Expand Down
9 changes: 2 additions & 7 deletions packages/docusaurus/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 &
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

0 comments on commit 4045ce3

Please sign in to comment.