From 1430ffb4734edbb67cbeaaee7e89a9f78e00473c Mon Sep 17 00:00:00 2001 From: Caleb Williams Date: Thu, 8 Jun 2023 23:56:29 -0500 Subject: [PATCH] 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 Co-authored-by: Nate Moore --- .changeset/thirty-books-smoke.md | 5 +++++ .changeset/yellow-plants-stare.md | 5 +++++ packages/astro/src/core/add/index.ts | 13 ++++++++++++- packages/create-astro/package.json | 3 ++- packages/create-astro/src/messages.ts | 17 +++++++++++++++-- pnpm-lock.yaml | 3 +++ 6 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 .changeset/thirty-books-smoke.md create mode 100644 .changeset/yellow-plants-stare.md diff --git a/.changeset/thirty-books-smoke.md b/.changeset/thirty-books-smoke.md new file mode 100644 index 000000000000..f127b4017636 --- /dev/null +++ b/.changeset/thirty-books-smoke.md @@ -0,0 +1,5 @@ +--- +'create-astro': patch +--- + +Ensure create-astro respects package manager registry configuration diff --git a/.changeset/yellow-plants-stare.md b/.changeset/yellow-plants-stare.md new file mode 100644 index 000000000000..753541888d33 --- /dev/null +++ b/.changeset/yellow-plants-stare.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes issue where Astro doesn't respect custom npm registry settings during project creation diff --git a/packages/astro/src/core/add/index.ts b/packages/astro/src/core/add/index.ts index aeb2c2fd7e3d..7af09b4de3a2 100644 --- a/packages/astro/src/core/add/index.ts +++ b/packages/astro/src/core/add/index.ts @@ -73,6 +73,16 @@ const OFFICIAL_ADAPTER_TO_IMPORT_MAP: Record = { 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 { + 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 { 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 { diff --git a/packages/create-astro/package.json b/packages/create-astro/package.json index ff70a70e0355..c38e0839ae0c 100644 --- a/packages/create-astro/package.json +++ b/packages/create-astro/package.json @@ -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", diff --git a/packages/create-astro/src/messages.ts b/packages/create-astro/src/messages.ts index 45bb7f96a603..1134b14dfdd2 100644 --- a/packages/create-astro/src/messages.ts +++ b/packages/create-astro/src/messages.ts @@ -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 { + 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((resolve) => { + new Promise(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', () => { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a6f92fd05cee..fa63609b13ff 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -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