Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid URLs being returned from getRegistry, synchronize all copies of the function #10117

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/twelve-waves-matter.md
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 8 additions & 2 deletions packages/astro/src/cli/add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 5 additions & 2 deletions packages/create-astro/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 8 additions & 2 deletions packages/upgrade/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading