From 51b6ff7403c1223b1c399e88373075972c82c24c Mon Sep 17 00:00:00 2001 From: Hippo <6137925+hippotastic@users.noreply.github.com> Date: Wed, 14 Feb 2024 23:27:18 +0100 Subject: [PATCH] Fix invalid URLs being returned from `getRegistry`, synchronize all copies (#10117) --- .changeset/twelve-waves-matter.md | 7 +++++++ packages/astro/src/cli/add/index.ts | 10 ++++++++-- packages/create-astro/src/messages.ts | 7 +++++-- packages/upgrade/src/messages.ts | 10 ++++++++-- 4 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 .changeset/twelve-waves-matter.md diff --git a/.changeset/twelve-waves-matter.md b/.changeset/twelve-waves-matter.md new file mode 100644 index 0000000000..8e7ddf6a51 --- /dev/null +++ b/.changeset/twelve-waves-matter.md @@ -0,0 +1,7 @@ +--- +"create-astro": patch +"@astrojs/upgrade": patch +"astro": patch +--- + +Fixes an issue where `create astro`, `astro add` and `@astrojs/upgrade` would fail due to unexpected package manager CLI output. diff --git a/packages/astro/src/cli/add/index.ts b/packages/astro/src/cli/add/index.ts index e1b64eb185..cd8ffd2a81 100644 --- a/packages/astro/src/cli/add/index.ts +++ b/packages/astro/src/cli/add/index.ts @@ -81,14 +81,20 @@ const OFFICIAL_ADAPTER_TO_IMPORT_MAP: Record = { // checks the user's project type and will return the proper npm registry // // A copy of this function also exists in the create-astro package +let _registry: string; async function getRegistry(): Promise { + if (_registry) return _registry; + const fallback = 'https://registry.npmjs.org'; const packageManager = (await preferredPM(process.cwd()))?.name || 'npm'; try { const { stdout } = await execa(packageManager, ['config', 'get', 'registry']); - return stdout?.trim()?.replace(/\/$/, '') || 'https://registry.npmjs.org'; + _registry = stdout?.trim()?.replace(/\/$/, '') || fallback; + // Detect cases where the shell command returned a non-URL (e.g. a warning) + if (!new URL(_registry).host) _registry = fallback; } catch (e) { - return 'https://registry.npmjs.org'; + _registry = fallback; } + return _registry; } export async function add(names: string[], { flags }: AddOptions) { diff --git a/packages/create-astro/src/messages.ts b/packages/create-astro/src/messages.ts index eea20d5169..8f6a2cf2ec 100644 --- a/packages/create-astro/src/messages.ts +++ b/packages/create-astro/src/messages.ts @@ -12,11 +12,14 @@ import { shell } from './shell.js'; let _registry: string; async function getRegistry(packageManager: string): Promise { if (_registry) return _registry; + const fallback = 'https://registry.npmjs.org'; try { const { stdout } = await shell(packageManager, ['config', 'get', 'registry']); - _registry = stdout?.trim()?.replace(/\/$/, '') || 'https://registry.npmjs.org'; + _registry = stdout?.trim()?.replace(/\/$/, '') || fallback; + // Detect cases where the shell command returned a non-URL (e.g. a warning) + if (!new URL(_registry).host) _registry = fallback; } catch (e) { - _registry = 'https://registry.npmjs.org'; + _registry = fallback; } return _registry; } diff --git a/packages/upgrade/src/messages.ts b/packages/upgrade/src/messages.ts index ae2ec49eb8..4f396422ea 100644 --- a/packages/upgrade/src/messages.ts +++ b/packages/upgrade/src/messages.ts @@ -10,14 +10,20 @@ import { shell } from './shell.js'; // checks the user's project type and will return the proper npm registry // // A copy of this function also exists in the astro package +let _registry: string; export async function getRegistry(): Promise { + if (_registry) return _registry; + const fallback = 'https://registry.npmjs.org'; const packageManager = detectPackageManager()?.name || 'npm'; try { const { stdout } = await shell(packageManager, ['config', 'get', 'registry']); - return stdout?.trim()?.replace(/\/$/, '') || 'https://registry.npmjs.org'; + _registry = stdout?.trim()?.replace(/\/$/, '') || fallback; + // Detect cases where the shell command returned a non-URL (e.g. a warning) + if (!new URL(_registry).host) _registry = fallback; } catch (e) { - return 'https://registry.npmjs.org'; + _registry = fallback; } + return _registry; } let stdout = process.stdout;