From 1170877b51aaa13203e8c488dcf4e39d1b5553ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=90?= <32624612+jc1144096387@users.noreply.github.com> Date: Sat, 1 Jul 2023 06:24:36 +0800 Subject: [PATCH] Fix multiple `create-astro` registry issues (#7539) * fix: getVersion Error when registry ends with '/' * Update .changeset/old-seahorses-fold.md * Update .changeset/old-seahorses-fold.md * refactor: cleanup registry logic * chore: update changeset * fix(create-astro): update registry logic to rely on `fetch` * chore: update changeset * chore: update lockfile * Update old-seahorses-fold.md --------- Co-authored-by: jincheng32537 Co-authored-by: Nate Moore Co-authored-by: Nate Moore --- .changeset/old-seahorses-fold.md | 6 ++++++ packages/astro/src/core/add/index.ts | 2 +- packages/create-astro/package.json | 1 + packages/create-astro/src/messages.ts | 18 ++++++------------ pnpm-lock.yaml | 9 ++++++--- 5 files changed, 20 insertions(+), 16 deletions(-) create mode 100644 .changeset/old-seahorses-fold.md diff --git a/.changeset/old-seahorses-fold.md b/.changeset/old-seahorses-fold.md new file mode 100644 index 0000000000..f5d6cad63c --- /dev/null +++ b/.changeset/old-seahorses-fold.md @@ -0,0 +1,6 @@ +--- +'astro': patch +'create-astro': patch +--- + +Update registry logic, improving edge cases (http support, redirects, registries ending with '/') diff --git a/packages/astro/src/core/add/index.ts b/packages/astro/src/core/add/index.ts index 12554409fe..9c7c701afd 100644 --- a/packages/astro/src/core/add/index.ts +++ b/packages/astro/src/core/add/index.ts @@ -79,7 +79,7 @@ async function getRegistry(): Promise { const packageManager = (await preferredPM(process.cwd()))?.name || 'npm'; try { const { stdout } = await execa(packageManager, ['config', 'get', 'registry']); - return stdout || 'https://registry.npmjs.org'; + return stdout?.trim()?.replace(/\/$/, '') || 'https://registry.npmjs.org'; } catch (e) { return 'https://registry.npmjs.org'; } diff --git a/packages/create-astro/package.json b/packages/create-astro/package.json index a3f1d6052c..4ce3cf383e 100644 --- a/packages/create-astro/package.json +++ b/packages/create-astro/package.json @@ -36,6 +36,7 @@ "execa": "^6.1.0", "giget": "1.0.0", "mocha": "^9.2.2", + "node-fetch-native": "^1.2.0", "which-pm-runs": "^1.1.0" }, "devDependencies": { diff --git a/packages/create-astro/src/messages.ts b/packages/create-astro/src/messages.ts index 4a51e0f808..edf22f5f62 100644 --- a/packages/create-astro/src/messages.ts +++ b/packages/create-astro/src/messages.ts @@ -3,7 +3,7 @@ import { color, label, say as houston, spinner as load } from '@astrojs/cli-kit' import { align, sleep } from '@astrojs/cli-kit/utils'; import { execa } from 'execa'; import { exec } from 'node:child_process'; -import { get } from 'node:https'; +import fetch from 'node-fetch-native'; import stripAnsi from 'strip-ansi'; import detectPackageManager from 'which-pm-runs'; @@ -15,7 +15,7 @@ async function getRegistry(): Promise { const packageManager = detectPackageManager()?.name || 'npm'; try { const { stdout } = await execa(packageManager, ['config', 'get', 'registry']); - return stdout || 'https://registry.npmjs.org'; + return stdout?.trim()?.replace(/\/$/, '') || 'https://registry.npmjs.org'; } catch (e) { return 'https://registry.npmjs.org'; } @@ -81,16 +81,10 @@ let v: string; export const getVersion = () => new Promise(async (resolve) => { if (v) return resolve(v); - const registry = await getRegistry(); - get(`${registry}/astro/latest`, (res) => { - let body = ''; - res.on('data', (chunk) => (body += chunk)); - res.on('end', () => { - const { version } = JSON.parse(body); - v = version; - resolve(version); - }); - }); + let registry = await getRegistry(); + const { version } = await fetch(`${registry}/astro/latest`, { redirect: 'follow' }).then(res => res.json()); + v = version; + resolve(version); }); export const log = (message: string) => stdout.write(message + '\n'); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ee0f3c0911..0611eadc18 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3574,6 +3574,9 @@ importers: mocha: specifier: ^9.2.2 version: 9.2.2 + node-fetch-native: + specifier: ^1.2.0 + version: 1.2.0 which-pm-runs: specifier: ^1.1.0 version: 1.1.0 @@ -11921,7 +11924,7 @@ packages: defu: 6.1.2 https-proxy-agent: 5.0.1 mri: 1.2.0 - node-fetch-native: 1.1.1 + node-fetch-native: 1.2.0 pathe: 1.1.0 tar: 6.1.14 transitivePeerDependencies: @@ -14162,8 +14165,8 @@ packages: engines: {node: '>=10.5.0'} dev: false - /node-fetch-native@1.1.1: - resolution: {integrity: sha512-9VvspTSUp2Sxbl+9vbZTlFGq9lHwE8GDVVekxx6YsNd1YH59sb3Ba8v3Y3cD8PkLNcileGGcA21PFjVl0jzDaw==} + /node-fetch-native@1.2.0: + resolution: {integrity: sha512-5IAMBTl9p6PaAjYCnMv5FmqIF6GcZnawAVnzaCG0rX2aYZJ4CxEkZNtVPuTRug7fL7wyM5BQYTlAzcyMPi6oTQ==} dev: false /node-fetch@2.6.11: