Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 15 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/hip-avocados-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': minor
---

add new flag with open for dev and preview
1 change: 1 addition & 0 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export interface CLIFlags {
port?: number;
config?: string;
drafts?: boolean;
open?: boolean;
experimentalAssets?: boolean;
}

Expand Down
6 changes: 6 additions & 0 deletions packages/astro/src/core/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export function resolveFlags(flags: Partial<Flags>): 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,
Expand Down Expand Up @@ -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.
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved
astroConfig.server.open = flags.open;
}
return astroConfig;
}

Expand Down
3 changes: 3 additions & 0 deletions packages/astro/src/core/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const ASTRO_CONFIG_DEFAULTS: AstroUserConfig & any = {
host: false,
port: 3000,
streaming: true,
open: false,
},
integrations: [],
markdown: {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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<OutgoingHttpHeaders>().optional(),
streaming: z.boolean().optional().default(true),
})
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/core/dev/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
},
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/core/dev/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default async function dev(
['--host', `Listen on all addresses, including LAN and public addresses.`],
['--host <custom-address>', `Expose on a network IP address at <custom-address>`],
['--help (-h)', 'See all available flags.'],
['--open (-h)', 'automatically open the app in the browser on server start'],
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved
],
},
description: `Check ${cyan(
Expand Down
5 changes: 4 additions & 1 deletion packages/astro/src/core/preview/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ export default async function preview(
commandName: 'astro preview',
usage: '[...flags]',
tables: {
Flags: [['--help (-h)', 'See all available flags.']],
Flags: [
['--help (-h)', 'See all available flags.'],
['--open (-h)', 'automatically open the app in the browser on server start'],
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved
],
},
description: `Starts a local server to serve your static dist/ directory. Check ${cyan(
'https://docs.astro.build/en/reference/cli-reference/#astro-preview'
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/core/preview/static-preview-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)],
});
Expand Down