Skip to content

Commit

Permalink
Fixes an issue where create Astro doesn't respect custom npm registries
Browse files Browse the repository at this point in the history
  • Loading branch information
calebdwilliams committed Jun 7, 2023
1 parent 435a231 commit e381223
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/yellow-plants-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes issue where Astro doesn't respect custom npm registry settings during project creation
11 changes: 10 additions & 1 deletion packages/astro/src/core/add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ const OFFICIAL_ADAPTER_TO_IMPORT_MAP: Record<string, string> = {
deno: '@astrojs/deno',
};

// Users might lack access to the global npm registry, this function
// checks the user's project type and will return the proper npm registry
async function getRegistry(): Promise<string> {
const packageManager = (await preferredPM(process.cwd()))?.name || 'npm';
const { stdout } = await execa(packageManager, ['config', 'get', 'registry']);
return stdout || 'https://registry.npmjs.org';
}

export default async function add(names: string[], { cwd, flags, logging, telemetry }: AddOptions) {
applyPolyfill();
if (flags.help || names.length === 0) {
Expand Down Expand Up @@ -673,7 +681,8 @@ async function fetchPackageJson(
tag: string
): Promise<object | Error> {
const packageName = `${scope ? `${scope}/` : ''}${name}`;
const res = await fetch(`https://registry.npmjs.org/${packageName}/${tag}`);
const registry = await getRegistry();
const res = await fetch(`${registry}/${packageName}/${tag}`);
if (res.status === 404) {
return new Error();
} else {
Expand Down

0 comments on commit e381223

Please sign in to comment.