Skip to content

Commit

Permalink
refactor a bit the start/serve cli options management
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Oct 5, 2020
1 parent cd0a1a3 commit 36e1dfd
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"build:v2:baseUrl": "yarn workspace docusaurus-2-website build:baseUrl",
"build:v2:blogOnly": "yarn workspace docusaurus-2-website build:blogOnly",
"serve:v1": "serve website-1.x/build/docusaurus",
"serve:v2": "serve website/build",
"serve:v2": "yarn workspace docusaurus-2-website serve",
"serve:v2:baseUrl": "serve website",
"serve:v2:ssl": "yarn serve:v2:ssl:gencert && yarn serve:v2:ssl:message && yarn serve:v2:ssl:serve",
"serve:v2:ssl:gencert": "openssl req -x509 -nodes -days 365 -newkey rsa:4096 -subj \"/C=US/ST=Docusaurus/L=Anywhere/O=Dis/CN=localhost\" -keyout ./website/.docusaurus/selfsigned.key -out ./website/.docusaurus/selfsigned.crt",
Expand Down
16 changes: 12 additions & 4 deletions packages/docusaurus-types/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,21 @@ export type PresetConfig =
| [string]
| string;

export interface StartCLIOptions {
port: string;
host: string;
export type HostPortCLIOptions = {
host?: string;
port?: string;
};

export type StartCLIOptions = HostPortCLIOptions & {
hotOnly: boolean;
open: boolean;
poll: boolean;
}
};

export type ServeCLIOptions = HostPortCLIOptions & {
build: boolean;
dir: string;
};

export interface BuildCLIOptions {
bundleAnalyzer: boolean;
Expand Down
23 changes: 23 additions & 0 deletions packages/docusaurus/src/commands/commandUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* 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 '../choosePort';
import {HostPortCLIOptions} from '@docusaurus/types';
import {DEFAULT_PORT} from '../constants';

export function getCLIOptionHost(
hostOption: HostPortCLIOptions['host'],
): string {
return hostOption || 'localhost';
}

export async function getCLIOptionPort(
portOption: HostPortCLIOptions['port'],
host: string,
): Promise<number | null> {
const basePort = portOption ? parseInt(portOption, 10) : DEFAULT_PORT;
return choosePort(host, basePort);
}
12 changes: 6 additions & 6 deletions packages/docusaurus/src/commands/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import chalk from 'chalk';
import path from 'path';

import build from './build';
import choosePort from '../choosePort';
import {getCLIOptionHost, getCLIOptionPort} from './commandUtils';
import {ServeCLIOptions} from '@docusaurus/types';

export default async function serve(
siteDir: string,
cliOptions: {port: string; build: boolean; dir: string; host: string},
cliOptions: ServeCLIOptions,
): Promise<void> {
let dir = path.join(siteDir, cliOptions.dir);
if (cliOptions.build) {
Expand All @@ -28,10 +29,9 @@ export default async function serve(
false,
);
}
const port: number | null = await choosePort(
cliOptions.host,
Number(cliOptions.port),
);

const host: string = getCLIOptionHost(cliOptions.host);
const port: number | null = await getCLIOptionPort(cliOptions.port, host);

if (port === null) {
process.exit();
Expand Down
23 changes: 5 additions & 18 deletions packages/docusaurus/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,14 @@ import merge from 'webpack-merge';
import HotModuleReplacementPlugin from 'webpack/lib/HotModuleReplacementPlugin';
import {load} from '../server';
import {StartCLIOptions} from '@docusaurus/types';
import {CONFIG_FILE_NAME, STATIC_DIR_NAME, DEFAULT_PORT} from '../constants';
import {CONFIG_FILE_NAME, STATIC_DIR_NAME} from '../constants';
import createClientConfig from '../webpack/client';
import {applyConfigureWebpack} from '../webpack/utils';
import choosePort from '../choosePort';

function getHost(reqHost: string | undefined): string {
return reqHost || 'localhost';
}

async function getPort(
reqPort: string | undefined,
host: string,
): Promise<number | null> {
const basePort = reqPort ? parseInt(reqPort, 10) : DEFAULT_PORT;
const port = await choosePort(host, basePort);
return port;
}
import {getCLIOptionHost, getCLIOptionPort} from './commandUtils';

export default async function start(
siteDir: string,
cliOptions: Partial<StartCLIOptions> = {},
cliOptions: Partial<StartCLIOptions>,
): Promise<void> {
process.env.NODE_ENV = 'development';
process.env.BABEL_ENV = 'development';
Expand Down Expand Up @@ -82,8 +69,8 @@ export default async function start(

const protocol: string = process.env.HTTPS === 'true' ? 'https' : 'http';

const host: string = getHost(cliOptions.host);
const port: number | null = await getPort(cliOptions.port, host);
const host: string = getCLIOptionHost(cliOptions.host);
const port: number | null = await getCLIOptionPort(cliOptions.port, host);

if (port === null) {
process.exit();
Expand Down
1 change: 1 addition & 0 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"swizzle": "docusaurus swizzle",
"deploy": "docusaurus deploy",
"clear": "docusaurus clear",
"serve": "docusaurus serve",
"start:baseUrl": "BASE_URL='/build/' yarn start",
"build:baseUrl": "BASE_URL='/build/' yarn build",
"start:bootstrap": "DOCUSAURUS_PRESET=bootstrap yarn start",
Expand Down

0 comments on commit 36e1dfd

Please sign in to comment.