Add support for --base CLI argument (#4917)

* feat(cli): add support for `--base` CLI argument

* chore: update CLI --help

* Update wise-swans-live.md

* Update wise-swans-live.md

Co-authored-by: Nate Moore <nate@astro.build>
This commit is contained in:
Nate Moore 2022-10-26 10:13:43 -05:00 committed by GitHub
parent bde715700c
commit ddf2f8390e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 16 additions and 1 deletions

View file

@ -0,0 +1,9 @@
---
'astro': minor
---
Add support for `--base` CLI argument, which will override the [`base`](https://docs.astro.build/en/reference/configuration-reference/#base) set in your `astro.config.mjs` file.
```
astro --site https://astro.build --base /docs
```

View file

@ -10,6 +10,7 @@
<title>Astro</title>
</head>
<body>
<h1>Astro</h1>
<h1>SITE: {Astro.site}</h1>
<p>BASE_URL: {import.meta.env.BASE_URL}</p>
</body>
</html>

View file

@ -77,6 +77,7 @@ export interface AstroComponentMetadata {
export interface CLIFlags {
root?: string;
site?: string;
base?: string;
host?: string | boolean;
port?: number;
config?: string;

View file

@ -61,6 +61,8 @@ function printAstroHelp() {
'Global Flags': [
['--config <path>', 'Specify your config file.'],
['--root <path>', 'Specify your project root folder.'],
['--site <url>', 'Specify your project site.'],
['--base <pathname>', 'Specify your project base.'],
['--verbose', 'Enable verbose logging.'],
['--silent', 'Disable all logging.'],
['--version', 'Show the version number and exit.'],

View file

@ -97,6 +97,7 @@ export function resolveFlags(flags: Partial<Flags>): CLIFlags {
return {
root: typeof flags.root === 'string' ? flags.root : undefined,
site: typeof flags.site === 'string' ? flags.site : undefined,
base: typeof flags.base === 'string' ? flags.base : undefined,
port: typeof flags.port === 'number' ? flags.port : undefined,
config: typeof flags.config === 'string' ? flags.config : undefined,
host:
@ -114,6 +115,7 @@ function mergeCLIFlags(astroConfig: AstroUserConfig, flags: CLIFlags, cmd: strin
astroConfig.server = astroConfig.server || {};
astroConfig.markdown = astroConfig.markdown || {};
if (typeof flags.site === 'string') astroConfig.site = flags.site;
if (typeof flags.base === 'string') astroConfig.base = flags.base;
if (typeof flags.drafts === 'boolean') astroConfig.markdown.drafts = flags.drafts;
if (typeof flags.port === 'number') {
// @ts-expect-error astroConfig.server may be a function, but TS doesn't like attaching properties to a function.