diff options
author | 2023-06-08 23:56:29 -0500 | |
---|---|---|
committer | 2023-06-09 07:56:29 +0300 | |
commit | 1430ffb4734edbb67cbeaaee7e89a9f78e00473c (patch) | |
tree | 9c2219fc34cba741b5896b825656744724c19d28 | |
parent | 5c20476fd333d57c583777893e8c6196b4764d1f (diff) | |
download | astro-1430ffb4734edbb67cbeaaee7e89a9f78e00473c.tar.gz astro-1430ffb4734edbb67cbeaaee7e89a9f78e00473c.tar.zst astro-1430ffb4734edbb67cbeaaee7e89a9f78e00473c.zip |
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>
-rw-r--r-- | .changeset/thirty-books-smoke.md | 5 | ||||
-rw-r--r-- | .changeset/yellow-plants-stare.md | 5 | ||||
-rw-r--r-- | packages/astro/src/core/add/index.ts | 13 | ||||
-rw-r--r-- | packages/create-astro/package.json | 3 | ||||
-rw-r--r-- | packages/create-astro/src/messages.ts | 17 | ||||
-rw-r--r-- | pnpm-lock.yaml | 3 |
6 files changed, 42 insertions, 4 deletions
diff --git a/.changeset/thirty-books-smoke.md b/.changeset/thirty-books-smoke.md new file mode 100644 index 000000000..f127b4017 --- /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 000000000..753541888 --- /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 aeb2c2fd7..7af09b4de 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<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 { diff --git a/packages/create-astro/package.json b/packages/create-astro/package.json index ff70a70e0..c38e0839a 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 45bb7f96a..1134b14df 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<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', () => { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a6f92fd05..fa63609b1 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 |