From adecda7d6009793c5d20519a997e3b7afb08ad57 Mon Sep 17 00:00:00 2001 From: wulinsheng123 <409187100@qq.com> Date: Tue, 28 Mar 2023 19:59:43 +0800 Subject: [PATCH] add a new flag with --open for the dev and the preview (#6578) --- .changeset/hip-avocados-grow.md | 5 +++++ packages/astro/src/@types/astro.ts | 1 + packages/astro/src/core/config/config.ts | 6 ++++++ packages/astro/src/core/config/schema.ts | 3 +++ packages/astro/src/core/dev/container.ts | 4 ++-- packages/astro/src/core/dev/dev.ts | 1 + packages/astro/src/core/preview/index.ts | 5 ++++- packages/astro/src/core/preview/static-preview-server.ts | 1 + 8 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 .changeset/hip-avocados-grow.md diff --git a/.changeset/hip-avocados-grow.md b/.changeset/hip-avocados-grow.md new file mode 100644 index 0000000000..ba92ed186f --- /dev/null +++ b/.changeset/hip-avocados-grow.md @@ -0,0 +1,5 @@ +--- +'astro': minor +--- + +add new flag with open for dev and preview diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index e1d8176070..20aee729d9 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -89,6 +89,7 @@ export interface CLIFlags { port?: number; config?: string; drafts?: boolean; + open?: boolean; experimentalAssets?: boolean; } diff --git a/packages/astro/src/core/config/config.ts b/packages/astro/src/core/config/config.ts index 9c45c9c5f7..9c09a934f5 100644 --- a/packages/astro/src/core/config/config.ts +++ b/packages/astro/src/core/config/config.ts @@ -96,6 +96,7 @@ export function resolveFlags(flags: Partial): CLIFlags { site: typeof flags.site === 'string' ? flags.site : undefined, base: typeof flags.base === 'string' ? flags.base : undefined, port: typeof flags.port === 'number' ? flags.port : undefined, + open: typeof flags.open === 'boolean' ? flags.open : undefined, config: typeof flags.config === 'string' ? flags.config : undefined, host: typeof flags.host === 'string' || typeof flags.host === 'boolean' ? flags.host : undefined, @@ -130,6 +131,11 @@ function mergeCLIFlags(astroConfig: AstroUserConfig, flags: CLIFlags) { // TODO: Come back here and refactor to remove this expected error. astroConfig.server.host = flags.host; } + if (typeof flags.open === 'boolean') { + // @ts-expect-error astroConfig.server may be a function, but TS doesn't like attaching properties to a function. + // TODO: Come back here and refactor to remove this expected error. + astroConfig.server.open = flags.open; + } return astroConfig; } diff --git a/packages/astro/src/core/config/schema.ts b/packages/astro/src/core/config/schema.ts index 5c7a31909a..5b374ce33e 100644 --- a/packages/astro/src/core/config/schema.ts +++ b/packages/astro/src/core/config/schema.ts @@ -26,6 +26,7 @@ const ASTRO_CONFIG_DEFAULTS: AstroUserConfig & any = { host: false, port: 3000, streaming: true, + open: false, }, integrations: [], markdown: { @@ -108,6 +109,7 @@ export const AstroConfigSchema = z.object({ // validate z .object({ + open: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.server.open), host: z .union([z.string(), z.boolean()]) .optional() @@ -246,6 +248,7 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: URL) { .optional() .default(ASTRO_CONFIG_DEFAULTS.server.host), port: z.number().optional().default(ASTRO_CONFIG_DEFAULTS.server.port), + open: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.server.open), headers: z.custom().optional(), streaming: z.boolean().optional().default(true), }) diff --git a/packages/astro/src/core/dev/container.ts b/packages/astro/src/core/dev/container.ts index 1b1ccca678..161a3bdf61 100644 --- a/packages/astro/src/core/dev/container.ts +++ b/packages/astro/src/core/dev/container.ts @@ -71,7 +71,7 @@ export async function createContainer(params: CreateContainerParams = {}): Promi logging, isRestart, }); - const { host, headers } = settings.config.server; + const { host, headers, open } = settings.config.server; // The client entrypoint for renderers. Since these are imported dynamically // we need to tell Vite to preoptimize them. @@ -82,7 +82,7 @@ export async function createContainer(params: CreateContainerParams = {}): Promi const viteConfig = await createVite( { mode: 'development', - server: { host, headers }, + server: { host, headers, open }, optimizeDeps: { include: rendererClientEntries, }, diff --git a/packages/astro/src/core/dev/dev.ts b/packages/astro/src/core/dev/dev.ts index 888c5afc69..8dbaabddf8 100644 --- a/packages/astro/src/core/dev/dev.ts +++ b/packages/astro/src/core/dev/dev.ts @@ -44,6 +44,7 @@ export default async function dev( ['--port', `Specify which port to run on. Defaults to 3000.`], ['--host', `Listen on all addresses, including LAN and public addresses.`], ['--host ', `Expose on a network IP address at `], + ['--open', 'Automatically open the app in the browser on server start'], ['--help (-h)', 'See all available flags.'], ], }, diff --git a/packages/astro/src/core/preview/index.ts b/packages/astro/src/core/preview/index.ts index 9dd5fcb38f..faaa67428a 100644 --- a/packages/astro/src/core/preview/index.ts +++ b/packages/astro/src/core/preview/index.ts @@ -26,7 +26,10 @@ export default async function preview( commandName: 'astro preview', usage: '[...flags]', tables: { - Flags: [['--help (-h)', 'See all available flags.']], + Flags: [ + ['--open', 'Automatically open the app in the browser on server start'], + ['--help (-h)', 'See all available flags.'], + ], }, description: `Starts a local server to serve your static dist/ directory. Check ${cyan( 'https://docs.astro.build/en/reference/cli-reference/#astro-preview' diff --git a/packages/astro/src/core/preview/static-preview-server.ts b/packages/astro/src/core/preview/static-preview-server.ts index 52cd7da73e..b908f74377 100644 --- a/packages/astro/src/core/preview/static-preview-server.ts +++ b/packages/astro/src/core/preview/static-preview-server.ts @@ -37,6 +37,7 @@ export default async function createStaticPreviewServer( host: settings.config.server.host, port: settings.config.server.port, headers: settings.config.server.headers, + open: settings.config.server.open, }, plugins: [vitePluginAstroPreview(settings)], });