0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

Fix invalid URLs being returned from getRegistry, synchronize all copies (#10117)

This commit is contained in:
Hippo 2024-02-14 23:27:18 +01:00 committed by GitHub
parent ef080d5b93
commit 51b6ff7403
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 28 additions and 6 deletions

View file

@ -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.

View file

@ -81,14 +81,20 @@ const OFFICIAL_ADAPTER_TO_IMPORT_MAP: Record<string, string> = {
// 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<string> {
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) {

View file

@ -12,11 +12,14 @@ import { shell } from './shell.js';
let _registry: string;
async function getRegistry(packageManager: string): Promise<string> {
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;
}

View file

@ -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<string> {
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;