Skip to content

Commit 1430ffb

Browse files
Fixes an issue where create Astro doesn't respect custom npm registries (#7326)
* Fixes an issue where create Astro doesn't respect custom npm registries * chore: fix pnpm-lock * chore: update lockfile --------- Co-authored-by: Nate Moore <nate@astro.build> Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
1 parent 5c20476 commit 1430ffb

File tree

6 files changed

+42
-4
lines changed

6 files changed

+42
-4
lines changed

.changeset/thirty-books-smoke.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'create-astro': patch
3+
---
4+
5+
Ensure create-astro respects package manager registry configuration

.changeset/yellow-plants-stare.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes issue where Astro doesn't respect custom npm registry settings during project creation

packages/astro/src/core/add/index.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ const OFFICIAL_ADAPTER_TO_IMPORT_MAP: Record<string, string> = {
7373
deno: '@astrojs/deno',
7474
};
7575

76+
// Users might lack access to the global npm registry, this function
77+
// checks the user's project type and will return the proper npm registry
78+
//
79+
// A copy of this function also exists in the create-astro package
80+
async function getRegistry(): Promise<string> {
81+
const packageManager = (await preferredPM(process.cwd()))?.name || 'npm';
82+
const { stdout } = await execa(packageManager, ['config', 'get', 'registry']);
83+
return stdout || 'https://registry.npmjs.org';
84+
}
85+
7686
export default async function add(names: string[], { cwd, flags, logging, telemetry }: AddOptions) {
7787
applyPolyfill();
7888
if (flags.help || names.length === 0) {
@@ -673,7 +683,8 @@ async function fetchPackageJson(
673683
tag: string
674684
): Promise<object | Error> {
675685
const packageName = `${scope ? `${scope}/` : ''}${name}`;
676-
const res = await fetch(`https://registry.npmjs.org/${packageName}/${tag}`);
686+
const registry = await getRegistry();
687+
const res = await fetch(`${registry}/${packageName}/${tag}`);
677688
if (res.status === 404) {
678689
return new Error();
679690
} else {

packages/create-astro/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"chai": "^4.3.6",
3636
"execa": "^6.1.0",
3737
"giget": "1.0.0",
38-
"mocha": "^9.2.2"
38+
"mocha": "^9.2.2",
39+
"preferred-pm": "^3.0.3"
3940
},
4041
"devDependencies": {
4142
"@types/which-pm-runs": "^1.0.0",

packages/create-astro/src/messages.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,20 @@ import { color, label, say as houston, spinner as load } from '@astrojs/cli-kit'
33
import { align, sleep } from '@astrojs/cli-kit/utils';
44
import { exec } from 'node:child_process';
55
import { get } from 'node:https';
6+
import { execa } from 'execa';
7+
import preferredPM from 'preferred-pm';
68
import stripAnsi from 'strip-ansi';
79

10+
// Users might lack access to the global npm registry, this function
11+
// checks the user's project type and will return the proper npm registry
12+
//
13+
// A copy of this function also exists in the astro package
14+
async function getRegistry(): Promise<string> {
15+
const packageManager = (await preferredPM(process.cwd()))?.name || 'npm';
16+
const { stdout } = await execa(packageManager, ['config', 'get', 'registry']);
17+
return stdout || 'https://registry.npmjs.org';
18+
}
19+
820
let stdout = process.stdout;
921
/** @internal Used to mock `process.stdout.write` for testing purposes */
1022
export function setStdout(writable: typeof process.stdout) {
@@ -63,9 +75,10 @@ export const getName = () =>
6375

6476
let v: string;
6577
export const getVersion = () =>
66-
new Promise<string>((resolve) => {
78+
new Promise<string>(async (resolve) => {
6779
if (v) return resolve(v);
68-
get('https://registry.npmjs.org/astro/latest', (res) => {
80+
const registry = await getRegistry();
81+
get(`${registry}/astro/latest`, (res) => {
6982
let body = '';
7083
res.on('data', (chunk) => (body += chunk));
7184
res.on('end', () => {

pnpm-lock.yaml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)