-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(astro): astro sync and astro:env (#11326)
- Loading branch information
1 parent
4c4741b
commit 41121fb
Showing
13 changed files
with
113 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Fixes a case where running `astro sync` when using the experimental `astro:env` feature would fail if environment variables were missing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import type { AstroSettings } from '../@types/astro.js'; | ||
import fsMod from 'node:fs'; | ||
import { getEnvFieldType } from './validators.js'; | ||
import { ENV_TYPES_FILE, TYPES_TEMPLATE_URL } from './constants.js'; | ||
|
||
export function syncAstroEnv(settings: AstroSettings, fs = fsMod) { | ||
if (!settings.config.experimental.env) { | ||
return; | ||
} | ||
|
||
const schema = settings.config.experimental.env.schema ?? {}; | ||
|
||
let client = ''; | ||
let server = ''; | ||
|
||
for (const [key, options] of Object.entries(schema)) { | ||
const str = `export const ${key}: ${getEnvFieldType(options)}; \n`; | ||
if (options.context === 'client') { | ||
client += str; | ||
} else { | ||
server += str; | ||
} | ||
} | ||
|
||
const template = fs.readFileSync(TYPES_TEMPLATE_URL, 'utf-8'); | ||
const dts = template.replace('// @@CLIENT@@', client).replace('// @@SERVER@@', server); | ||
|
||
fs.mkdirSync(settings.dotAstroDir, { recursive: true }); | ||
fs.writeFileSync(new URL(ENV_TYPES_FILE, settings.dotAstroDir), dts, 'utf-8'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
packages/astro/test/fixtures/astro-env-required-public/astro.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { defineConfig, envField } from 'astro/config'; | ||
|
||
// https://astro.build/config | ||
export default defineConfig({ | ||
experimental: { | ||
env: { | ||
schema: { | ||
FOO: envField.string({ context: "client", access: "public" }), | ||
BAR: envField.number({ context: "server", access: "public" }), | ||
} | ||
} | ||
} | ||
}); |
8 changes: 8 additions & 0 deletions
8
packages/astro/test/fixtures/astro-env-required-public/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "@test/astro-env-required-public", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"astro": "workspace:*" | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/astro/test/fixtures/astro-env-required-public/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "astro/tsconfigs/base" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.