0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00

add a new flag with --open for the dev and the preview (#6578)

This commit is contained in:
wulinsheng123 2023-03-28 19:59:43 +08:00 committed by GitHub
parent 4a32620600
commit adecda7d60
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 23 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
'astro': minor
---
add new flag with open for dev and preview

View file

@ -89,6 +89,7 @@ export interface CLIFlags {
port?: number; port?: number;
config?: string; config?: string;
drafts?: boolean; drafts?: boolean;
open?: boolean;
experimentalAssets?: boolean; experimentalAssets?: boolean;
} }

View file

@ -96,6 +96,7 @@ export function resolveFlags(flags: Partial<Flags>): CLIFlags {
site: typeof flags.site === 'string' ? flags.site : undefined, site: typeof flags.site === 'string' ? flags.site : undefined,
base: typeof flags.base === 'string' ? flags.base : undefined, base: typeof flags.base === 'string' ? flags.base : undefined,
port: typeof flags.port === 'number' ? flags.port : 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, config: typeof flags.config === 'string' ? flags.config : undefined,
host: host:
typeof flags.host === 'string' || typeof flags.host === 'boolean' ? flags.host : undefined, 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. // TODO: Come back here and refactor to remove this expected error.
astroConfig.server.host = flags.host; 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; return astroConfig;
} }

View file

@ -26,6 +26,7 @@ const ASTRO_CONFIG_DEFAULTS: AstroUserConfig & any = {
host: false, host: false,
port: 3000, port: 3000,
streaming: true, streaming: true,
open: false,
}, },
integrations: [], integrations: [],
markdown: { markdown: {
@ -108,6 +109,7 @@ export const AstroConfigSchema = z.object({
// validate // validate
z z
.object({ .object({
open: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.server.open),
host: z host: z
.union([z.string(), z.boolean()]) .union([z.string(), z.boolean()])
.optional() .optional()
@ -246,6 +248,7 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: URL) {
.optional() .optional()
.default(ASTRO_CONFIG_DEFAULTS.server.host), .default(ASTRO_CONFIG_DEFAULTS.server.host),
port: z.number().optional().default(ASTRO_CONFIG_DEFAULTS.server.port), port: z.number().optional().default(ASTRO_CONFIG_DEFAULTS.server.port),
open: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.server.open),
headers: z.custom<OutgoingHttpHeaders>().optional(), headers: z.custom<OutgoingHttpHeaders>().optional(),
streaming: z.boolean().optional().default(true), streaming: z.boolean().optional().default(true),
}) })

View file

@ -71,7 +71,7 @@ export async function createContainer(params: CreateContainerParams = {}): Promi
logging, logging,
isRestart, isRestart,
}); });
const { host, headers } = settings.config.server; const { host, headers, open } = settings.config.server;
// The client entrypoint for renderers. Since these are imported dynamically // The client entrypoint for renderers. Since these are imported dynamically
// we need to tell Vite to preoptimize them. // we need to tell Vite to preoptimize them.
@ -82,7 +82,7 @@ export async function createContainer(params: CreateContainerParams = {}): Promi
const viteConfig = await createVite( const viteConfig = await createVite(
{ {
mode: 'development', mode: 'development',
server: { host, headers }, server: { host, headers, open },
optimizeDeps: { optimizeDeps: {
include: rendererClientEntries, include: rendererClientEntries,
}, },

View file

@ -44,6 +44,7 @@ export default async function dev(
['--port', `Specify which port to run on. Defaults to 3000.`], ['--port', `Specify which port to run on. Defaults to 3000.`],
['--host', `Listen on all addresses, including LAN and public addresses.`], ['--host', `Listen on all addresses, including LAN and public addresses.`],
['--host <custom-address>', `Expose on a network IP address at <custom-address>`], ['--host <custom-address>', `Expose on a network IP address at <custom-address>`],
['--open', 'Automatically open the app in the browser on server start'],
['--help (-h)', 'See all available flags.'], ['--help (-h)', 'See all available flags.'],
], ],
}, },

View file

@ -26,7 +26,10 @@ export default async function preview(
commandName: 'astro preview', commandName: 'astro preview',
usage: '[...flags]', usage: '[...flags]',
tables: { 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( description: `Starts a local server to serve your static dist/ directory. Check ${cyan(
'https://docs.astro.build/en/reference/cli-reference/#astro-preview' 'https://docs.astro.build/en/reference/cli-reference/#astro-preview'

View file

@ -37,6 +37,7 @@ export default async function createStaticPreviewServer(
host: settings.config.server.host, host: settings.config.server.host,
port: settings.config.server.port, port: settings.config.server.port,
headers: settings.config.server.headers, headers: settings.config.server.headers,
open: settings.config.server.open,
}, },
plugins: [vitePluginAstroPreview(settings)], plugins: [vitePluginAstroPreview(settings)],
}); });