From dcb15a766eac7488d93b7f413479702be6612451 Mon Sep 17 00:00:00 2001 From: James <5511220+Zamiell@users.noreply.github.com> Date: Thu, 26 May 2022 01:40:59 -0400 Subject: [PATCH 1/4] feat: add automatic url open to npm run serve --- packages/docusaurus/bin/docusaurus.mjs | 1 + packages/docusaurus/src/commands/serve.ts | 11 ++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/docusaurus/bin/docusaurus.mjs b/packages/docusaurus/bin/docusaurus.mjs index 820d26e7de9a..7a4b21836e58 100755 --- a/packages/docusaurus/bin/docusaurus.mjs +++ b/packages/docusaurus/bin/docusaurus.mjs @@ -148,6 +148,7 @@ cli .option('-p, --port ', 'use specified port (default: 3000)') .option('--build', 'build website before serving (default: false)') .option('-h, --host ', 'use specified host (default: localhost)') + .option('--no-open', 'do not open page in the browser (default: false)') .action(async (siteDir, options) => serve(await resolveDir(siteDir), options), ); diff --git a/packages/docusaurus/src/commands/serve.ts b/packages/docusaurus/src/commands/serve.ts index 64845cb5ced4..13e4ab06514b 100644 --- a/packages/docusaurus/src/commands/serve.ts +++ b/packages/docusaurus/src/commands/serve.ts @@ -10,6 +10,7 @@ import path from 'path'; import logger from '@docusaurus/logger'; import {DEFAULT_BUILD_DIR_NAME} from '@docusaurus/utils'; import serveHandler from 'serve-handler'; +import openBrowser from 'react-dev-utils/openBrowser'; import {loadSiteConfig} from '../server/config'; import {build} from './build'; import {getHostPort, type HostPortOptions} from '../server/getHostPort'; @@ -19,6 +20,7 @@ export type ServeCLIOptions = HostPortOptions & Pick & { dir?: string; build?: boolean; + open?: boolean; }; export async function serve( @@ -76,8 +78,11 @@ export async function serve( }); }); - logger.success`Serving path=${buildDir} directory at url=${ - servingUrl + baseUrl - }.`; + const url = servingUrl + baseUrl; + logger.success`Serving path=${buildDir} directory at url=${url}.`; server.listen(port); + + if (cliOptions.open) { + openBrowser(url); + } } From cbdcece9c042a7457124663295d09eb30859c604 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Thu, 26 May 2022 15:12:30 +0800 Subject: [PATCH 2/4] fix --- packages/docusaurus/src/commands/serve.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/docusaurus/src/commands/serve.ts b/packages/docusaurus/src/commands/serve.ts index cf010c06083c..0b0486ea8433 100644 --- a/packages/docusaurus/src/commands/serve.ts +++ b/packages/docusaurus/src/commands/serve.ts @@ -78,9 +78,8 @@ export async function serve( }); }); - logger.success`Serving path=${buildDir} directory at: url=${ - servingUrl + baseUrl - }`; + const url = servingUrl + baseUrl; + logger.success`Serving path=${buildDir} directory at: url=${url}`; server.listen(port); if (cliOptions.open) { From 03909b584b60fec1058e265fa2ed09317ea153ed Mon Sep 17 00:00:00 2001 From: James <5511220+Zamiell@users.noreply.github.com> Date: Thu, 26 May 2022 03:43:41 -0400 Subject: [PATCH 3/4] adding ci check --- packages/docusaurus/src/commands/serve.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/docusaurus/src/commands/serve.ts b/packages/docusaurus/src/commands/serve.ts index 0b0486ea8433..0eb24b677f1b 100644 --- a/packages/docusaurus/src/commands/serve.ts +++ b/packages/docusaurus/src/commands/serve.ts @@ -82,7 +82,7 @@ export async function serve( logger.success`Serving path=${buildDir} directory at: url=${url}`; server.listen(port); - if (cliOptions.open) { + if (cliOptions.open && !process.env.CI) { openBrowser(url); } } From 9d9c043c469c06a9f5a35cee3016d9a5e7233be3 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Thu, 26 May 2022 20:58:56 +0800 Subject: [PATCH 4/4] add docs --- packages/docusaurus/bin/docusaurus.mjs | 5 ++++- website/docs/cli.md | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/docusaurus/bin/docusaurus.mjs b/packages/docusaurus/bin/docusaurus.mjs index 7a4b21836e58..8ae3d348ecf6 100755 --- a/packages/docusaurus/bin/docusaurus.mjs +++ b/packages/docusaurus/bin/docusaurus.mjs @@ -148,7 +148,10 @@ cli .option('-p, --port ', 'use specified port (default: 3000)') .option('--build', 'build website before serving (default: false)') .option('-h, --host ', 'use specified host (default: localhost)') - .option('--no-open', 'do not open page in the browser (default: false)') + .option( + '--no-open', + 'do not open page in the browser (default: false, or true in CI)', + ) .action(async (siteDir, options) => serve(await resolveDir(siteDir), options), ); diff --git a/website/docs/cli.md b/website/docs/cli.md index e19840601722..c1b79ddd9d87 100644 --- a/website/docs/cli.md +++ b/website/docs/cli.md @@ -150,6 +150,7 @@ Serve your built website locally. | `--build` | `false` | Build website before serving | | `--config` | `undefined` | Path to docusaurus config file, default to `[siteDir]/docusaurus.config.js` | | `--host` | `localhost` | Specify a host to use. For example, if you want your server to be accessible externally, you can use `--host 0.0.0.0`. | +| `--no-open` | `false` locally, `true` in CI | Do not open a browser window to the server location. | ### `docusaurus clear [siteDir]` {#docusaurus-clear-sitedir}