Skip to content

Commit

Permalink
feat: add automatic url open to npm run serve
Browse files Browse the repository at this point in the history
  • Loading branch information
Zamiell committed May 26, 2022
1 parent 89ddc6d commit dcb15a7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/docusaurus/bin/docusaurus.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ cli
.option('-p, --port <port>', 'use specified port (default: 3000)')
.option('--build', 'build website before serving (default: false)')
.option('-h, --host <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),
);
Expand Down
11 changes: 8 additions & 3 deletions packages/docusaurus/src/commands/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -19,6 +20,7 @@ export type ServeCLIOptions = HostPortOptions &
Pick<LoadContextOptions, 'config'> & {
dir?: string;
build?: boolean;
open?: boolean;
};

export async function serve(
Expand Down Expand Up @@ -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);
}
}

0 comments on commit dcb15a7

Please sign in to comment.