From 1f598b372410066c6fcd41cba9915f6aaf7befa8 Mon Sep 17 00:00:00 2001 From: Mohamed Date: Thu, 15 Feb 2024 15:42:03 +0100 Subject: [PATCH] fix(cli): load polyfill before loading config during `astro add` (#10105) * Adds crypto import to Vercel adapter * Update the 'crypto' import to 'node:crypto' * load polyfills before loading config * add changeset * load polyfills for `astro info`, `astro preferences` etc * update changeset * clarify impact --------- Co-authored-by: lilnasy <69170106+lilnasy@users.noreply.github.com> --- .changeset/itchy-monkeys-fail.md | 5 +++++ packages/astro/src/cli/add/index.ts | 2 +- packages/astro/src/cli/db/index.ts | 2 ++ packages/astro/src/cli/info/index.ts | 2 ++ packages/astro/src/cli/preferences/index.ts | 2 ++ packages/astro/src/core/preview/index.ts | 2 ++ 6 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 .changeset/itchy-monkeys-fail.md diff --git a/.changeset/itchy-monkeys-fail.md b/.changeset/itchy-monkeys-fail.md new file mode 100644 index 0000000000..5e638e5da4 --- /dev/null +++ b/.changeset/itchy-monkeys-fail.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Fixes an issue where some astro commands failed if the astro config file or an integration used the global `crypto` object. diff --git a/packages/astro/src/cli/add/index.ts b/packages/astro/src/cli/add/index.ts index cd8ffd2a81..13782aaf1e 100644 --- a/packages/astro/src/cli/add/index.ts +++ b/packages/astro/src/cli/add/index.ts @@ -99,10 +99,10 @@ async function getRegistry(): Promise { export async function add(names: string[], { flags }: AddOptions) { ensureProcessNodeEnv('production'); + applyPolyfill(); const inlineConfig = flagsToAstroInlineConfig(flags); const { userConfig } = await resolveConfig(inlineConfig, 'add'); telemetry.record(eventCliSession('add', userConfig)); - applyPolyfill(); if (flags.help || names.length === 0) { printHelp({ commandName: 'astro add', diff --git a/packages/astro/src/cli/db/index.ts b/packages/astro/src/cli/db/index.ts index 8b45e88e03..95d6f40453 100644 --- a/packages/astro/src/cli/db/index.ts +++ b/packages/astro/src/cli/db/index.ts @@ -3,12 +3,14 @@ import { createLoggerFromFlags, flagsToAstroInlineConfig } from '../flags.js'; import { getPackage } from '../install-package.js'; import { resolveConfig } from '../../core/config/config.js'; import type { AstroConfig } from '../../@types/astro.js'; +import { apply as applyPolyfill } from '../../core/polyfill.js'; type DBPackage = { cli: (args: { flags: Arguments; config: AstroConfig }) => unknown; }; export async function db({ flags }: { flags: Arguments }) { + applyPolyfill(); const logger = createLoggerFromFlags(flags); const getPackageOpts = { skipAsk: flags.yes || flags.y, cwd: flags.root }; const dbPackage = await getPackage('@astrojs/db', logger, getPackageOpts, []); diff --git a/packages/astro/src/cli/info/index.ts b/packages/astro/src/cli/info/index.ts index c6586b28d2..8275d409ca 100644 --- a/packages/astro/src/cli/info/index.ts +++ b/packages/astro/src/cli/info/index.ts @@ -8,6 +8,7 @@ import type { AstroConfig, AstroUserConfig } from '../../@types/astro.js'; import { resolveConfig } from '../../core/config/index.js'; import { ASTRO_VERSION } from '../../core/constants.js'; import { flagsToAstroInlineConfig } from '../flags.js'; +import { apply as applyPolyfill } from '../../core/polyfill.js'; interface InfoOptions { flags: yargs.Arguments; @@ -47,6 +48,7 @@ export async function getInfoOutput({ } export async function printInfo({ flags }: InfoOptions) { + applyPolyfill(); const { userConfig } = await resolveConfig(flagsToAstroInlineConfig(flags), 'info'); const output = await getInfoOutput({ userConfig, print: true }); diff --git a/packages/astro/src/cli/preferences/index.ts b/packages/astro/src/cli/preferences/index.ts index e66f9083aa..686e0854e6 100644 --- a/packages/astro/src/cli/preferences/index.ts +++ b/packages/astro/src/cli/preferences/index.ts @@ -16,6 +16,7 @@ import { createLoggerFromFlags, flagsToAstroInlineConfig } from '../flags.js'; import { flattie } from 'flattie'; import { formatWithOptions } from 'node:util'; import { collectErrorMetadata } from '../../core/errors/dev/utils.js'; +import { apply as applyPolyfill } from '../../core/polyfill.js'; interface PreferencesOptions { flags: yargs.Arguments; @@ -45,6 +46,7 @@ export async function preferences( value: string | undefined, { flags }: PreferencesOptions ): Promise { + applyPolyfill(); if (!isValidSubcommand(subcommand) || flags?.help || flags?.h) { msg.printHelp({ commandName: 'astro preferences', diff --git a/packages/astro/src/core/preview/index.ts b/packages/astro/src/core/preview/index.ts index 5d6adc0c64..d34faf3a21 100644 --- a/packages/astro/src/core/preview/index.ts +++ b/packages/astro/src/core/preview/index.ts @@ -12,6 +12,7 @@ import { createSettings } from '../config/settings.js'; import createStaticPreviewServer from './static-preview-server.js'; import { getResolvedHostForHttpServer } from './util.js'; import { ensureProcessNodeEnv } from '../util.js'; +import { apply as applyPolyfills } from '../polyfill.js'; /** * Starts a local server to serve your static dist/ directory. This command is useful for previewing @@ -20,6 +21,7 @@ import { ensureProcessNodeEnv } from '../util.js'; * @experimental The JavaScript API is experimental */ export default async function preview(inlineConfig: AstroInlineConfig): Promise { + applyPolyfills(); ensureProcessNodeEnv('production'); const logger = createNodeLogger(inlineConfig); const { userConfig, astroConfig } = await resolveConfig(inlineConfig ?? {}, 'preview');