Skip to content

Commit

Permalink
Fix invalid URLs being returned from getRegistry, synchronize all c…
Browse files Browse the repository at this point in the history
…opies (#10117)
  • Loading branch information
hippotastic authored Feb 14, 2024
1 parent ef080d5 commit 51b6ff7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
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

0 comments on commit 51b6ff7

Please sign in to comment.