Skip to content

Commit

Permalink
fix(cli): load polyfill before loading config during astro add (#10105
Browse files Browse the repository at this point in the history
)

* 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>
  • Loading branch information
negativems and lilnasy authored Feb 15, 2024
1 parent 4f6b0de commit 1f598b3
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/itchy-monkeys-fail.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion packages/astro/src/cli/add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ async function getRegistry(): Promise<string> {

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',
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/cli/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DBPackage>('@astrojs/db', logger, getPackageOpts, []);
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/cli/info/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 });

Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/cli/preferences/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,6 +46,7 @@ export async function preferences(
value: string | undefined,
{ flags }: PreferencesOptions
): Promise<number> {
applyPolyfill();
if (!isValidSubcommand(subcommand) || flags?.help || flags?.h) {
msg.printHelp({
commandName: 'astro preferences',
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/core/preview/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,6 +21,7 @@ import { ensureProcessNodeEnv } from '../util.js';
* @experimental The JavaScript API is experimental
*/
export default async function preview(inlineConfig: AstroInlineConfig): Promise<PreviewServer> {
applyPolyfills();
ensureProcessNodeEnv('production');
const logger = createNodeLogger(inlineConfig);
const { userConfig, astroConfig } = await resolveConfig(inlineConfig ?? {}, 'preview');
Expand Down

0 comments on commit 1f598b3

Please sign in to comment.