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 070a1f1
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 44 deletions.
26 changes: 0 additions & 26 deletions packages/docusaurus/src/commands/commandUtils.ts

This file was deleted.

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 '../server/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 '../server/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 @@ -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 = {
Expand Down Expand Up @@ -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<number | null> {
Expand Down Expand Up @@ -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};
}
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 070a1f1

Please sign in to comment.