mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-20 19:47:52 +02:00
fix(v2): Fixes serve cli --port option (#3531)
* fix(v2): Fixes serve cli --port option * refactor a bit the start/serve cli options management Co-authored-by: slorber <lorber.sebastien@gmail.com>
This commit is contained in:
parent
f29da4ba77
commit
10973476ff
6 changed files with 53 additions and 26 deletions
|
@ -22,7 +22,7 @@
|
||||||
"build:v2:baseUrl": "yarn workspace docusaurus-2-website build:baseUrl",
|
"build:v2:baseUrl": "yarn workspace docusaurus-2-website build:baseUrl",
|
||||||
"build:v2:blogOnly": "yarn workspace docusaurus-2-website build:blogOnly",
|
"build:v2:blogOnly": "yarn workspace docusaurus-2-website build:blogOnly",
|
||||||
"serve:v1": "serve website-1.x/build/docusaurus",
|
"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:baseUrl": "serve website",
|
||||||
"serve:v2:ssl": "yarn serve:v2:ssl:gencert && yarn serve:v2:ssl:message && yarn serve:v2:ssl:serve",
|
"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",
|
"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",
|
||||||
|
|
16
packages/docusaurus-types/src/index.d.ts
vendored
16
packages/docusaurus-types/src/index.d.ts
vendored
|
@ -91,13 +91,21 @@ export type PresetConfig =
|
||||||
| [string]
|
| [string]
|
||||||
| string;
|
| string;
|
||||||
|
|
||||||
export interface StartCLIOptions {
|
export type HostPortCLIOptions = {
|
||||||
port: string;
|
host?: string;
|
||||||
host: string;
|
port?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type StartCLIOptions = HostPortCLIOptions & {
|
||||||
hotOnly: boolean;
|
hotOnly: boolean;
|
||||||
open: boolean;
|
open: boolean;
|
||||||
poll: boolean;
|
poll: boolean;
|
||||||
}
|
};
|
||||||
|
|
||||||
|
export type ServeCLIOptions = HostPortCLIOptions & {
|
||||||
|
build: boolean;
|
||||||
|
dir: string;
|
||||||
|
};
|
||||||
|
|
||||||
export interface BuildCLIOptions {
|
export interface BuildCLIOptions {
|
||||||
bundleAnalyzer: boolean;
|
bundleAnalyzer: boolean;
|
||||||
|
|
23
packages/docusaurus/src/commands/commandUtils.ts
Normal file
23
packages/docusaurus/src/commands/commandUtils.ts
Normal file
|
@ -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,11 +12,12 @@ import chalk from 'chalk';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
|
||||||
import build from './build';
|
import build from './build';
|
||||||
import choosePort from '../choosePort';
|
import {getCLIOptionHost, getCLIOptionPort} from './commandUtils';
|
||||||
|
import {ServeCLIOptions} from '@docusaurus/types';
|
||||||
|
|
||||||
export default async function serve(
|
export default async function serve(
|
||||||
siteDir: string,
|
siteDir: string,
|
||||||
cliOptions: {port: number; build: boolean; dir: string; host: string},
|
cliOptions: ServeCLIOptions,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
let dir = path.join(siteDir, cliOptions.dir);
|
let dir = path.join(siteDir, cliOptions.dir);
|
||||||
if (cliOptions.build) {
|
if (cliOptions.build) {
|
||||||
|
@ -28,7 +29,14 @@ export default async function serve(
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const port = await choosePort(cliOptions.host, cliOptions.port);
|
|
||||||
|
const host: string = getCLIOptionHost(cliOptions.host);
|
||||||
|
const port: number | null = await getCLIOptionPort(cliOptions.port, host);
|
||||||
|
|
||||||
|
if (port === null) {
|
||||||
|
process.exit();
|
||||||
|
}
|
||||||
|
|
||||||
const server = http.createServer((req, res) => {
|
const server = http.createServer((req, res) => {
|
||||||
serveHandler(req, res, {
|
serveHandler(req, res, {
|
||||||
cleanUrls: true,
|
cleanUrls: true,
|
||||||
|
|
|
@ -21,27 +21,14 @@ import merge from 'webpack-merge';
|
||||||
import HotModuleReplacementPlugin from 'webpack/lib/HotModuleReplacementPlugin';
|
import HotModuleReplacementPlugin from 'webpack/lib/HotModuleReplacementPlugin';
|
||||||
import {load} from '../server';
|
import {load} from '../server';
|
||||||
import {StartCLIOptions} from '@docusaurus/types';
|
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 createClientConfig from '../webpack/client';
|
||||||
import {applyConfigureWebpack} from '../webpack/utils';
|
import {applyConfigureWebpack} from '../webpack/utils';
|
||||||
import choosePort from '../choosePort';
|
import {getCLIOptionHost, getCLIOptionPort} from './commandUtils';
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default async function start(
|
export default async function start(
|
||||||
siteDir: string,
|
siteDir: string,
|
||||||
cliOptions: Partial<StartCLIOptions> = {},
|
cliOptions: Partial<StartCLIOptions>,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
process.env.NODE_ENV = 'development';
|
process.env.NODE_ENV = 'development';
|
||||||
process.env.BABEL_ENV = 'development';
|
process.env.BABEL_ENV = 'development';
|
||||||
|
@ -82,8 +69,8 @@ export default async function start(
|
||||||
|
|
||||||
const protocol: string = process.env.HTTPS === 'true' ? 'https' : 'http';
|
const protocol: string = process.env.HTTPS === 'true' ? 'https' : 'http';
|
||||||
|
|
||||||
const host: string = getHost(cliOptions.host);
|
const host: string = getCLIOptionHost(cliOptions.host);
|
||||||
const port: number | null = await getPort(cliOptions.port, host);
|
const port: number | null = await getCLIOptionPort(cliOptions.port, host);
|
||||||
|
|
||||||
if (port === null) {
|
if (port === null) {
|
||||||
process.exit();
|
process.exit();
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
"swizzle": "docusaurus swizzle",
|
"swizzle": "docusaurus swizzle",
|
||||||
"deploy": "docusaurus deploy",
|
"deploy": "docusaurus deploy",
|
||||||
"clear": "docusaurus clear",
|
"clear": "docusaurus clear",
|
||||||
|
"serve": "docusaurus serve",
|
||||||
"start:baseUrl": "BASE_URL='/build/' yarn start",
|
"start:baseUrl": "BASE_URL='/build/' yarn start",
|
||||||
"build:baseUrl": "BASE_URL='/build/' yarn build",
|
"build:baseUrl": "BASE_URL='/build/' yarn build",
|
||||||
"start:bootstrap": "DOCUSAURUS_PRESET=bootstrap yarn start",
|
"start:bootstrap": "DOCUSAURUS_PRESET=bootstrap yarn start",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue