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] 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); + } }