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>
This commit is contained in:
Caleb Williams 2023-06-08 23:56:29 -05:00 committed by GitHub
parent 5c20476fd3
commit 1430ffb473
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 42 additions and 4 deletions

View file

@ -0,0 +1,5 @@
---
'create-astro': patch
---
Ensure create-astro respects package manager registry configuration

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes issue where Astro doesn't respect custom npm registry settings during project creation

View file

@ -73,6 +73,16 @@ 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
//
// A copy of this function also exists in the create-astro package
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) {
@ -673,7 +683,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 {

View file

@ -35,7 +35,8 @@
"chai": "^4.3.6",
"execa": "^6.1.0",
"giget": "1.0.0",
"mocha": "^9.2.2"
"mocha": "^9.2.2",
"preferred-pm": "^3.0.3"
},
"devDependencies": {
"@types/which-pm-runs": "^1.0.0",

View file

@ -3,8 +3,20 @@ import { color, label, say as houston, spinner as load } from '@astrojs/cli-kit'
import { align, sleep } from '@astrojs/cli-kit/utils';
import { exec } from 'node:child_process';
import { get } from 'node:https';
import { execa } from 'execa';
import preferredPM from 'preferred-pm';
import stripAnsi from 'strip-ansi';
// Users might lack access to the global npm registry, this function
// checks the user's project type and will return the proper npm registry
//
// A copy of this function also exists in the astro package
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';
}
let stdout = process.stdout;
/** @internal Used to mock `process.stdout.write` for testing purposes */
export function setStdout(writable: typeof process.stdout) {
@ -63,9 +75,10 @@ export const getName = () =>
let v: string;
export const getVersion = () =>
new Promise<string>((resolve) => {
new Promise<string>(async (resolve) => {
if (v) return resolve(v);
get('https://registry.npmjs.org/astro/latest', (res) => {
const registry = await getRegistry();
get(`${registry}/astro/latest`, (res) => {
let body = '';
res.on('data', (chunk) => (body += chunk));
res.on('end', () => {

View file

@ -3554,6 +3554,9 @@ importers:
mocha:
specifier: ^9.2.2
version: 9.2.2
preferred-pm:
specifier: ^3.0.3
version: 3.0.3
devDependencies:
'@types/which-pm-runs':
specifier: ^1.0.0