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 <jincheng32537@hundsun.com>
Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
Co-authored-by: Nate Moore <nate@astro.build>
This commit is contained in:
金成 2023-07-01 06:24:36 +08:00 committed by GitHub
parent 2172dd4f0d
commit 1170877b51
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 16 deletions

View file

@ -0,0 +1,6 @@
---
'astro': patch
'create-astro': patch
---
Update registry logic, improving edge cases (http support, redirects, registries ending with '/')

View file

@ -79,7 +79,7 @@ async function getRegistry(): Promise<string> {
const packageManager = (await preferredPM(process.cwd()))?.name || 'npm'; const packageManager = (await preferredPM(process.cwd()))?.name || 'npm';
try { try {
const { stdout } = await execa(packageManager, ['config', 'get', 'registry']); const { stdout } = await execa(packageManager, ['config', 'get', 'registry']);
return stdout || 'https://registry.npmjs.org'; return stdout?.trim()?.replace(/\/$/, '') || 'https://registry.npmjs.org';
} catch (e) { } catch (e) {
return 'https://registry.npmjs.org'; return 'https://registry.npmjs.org';
} }

View file

@ -36,6 +36,7 @@
"execa": "^6.1.0", "execa": "^6.1.0",
"giget": "1.0.0", "giget": "1.0.0",
"mocha": "^9.2.2", "mocha": "^9.2.2",
"node-fetch-native": "^1.2.0",
"which-pm-runs": "^1.1.0" "which-pm-runs": "^1.1.0"
}, },
"devDependencies": { "devDependencies": {

View file

@ -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 { align, sleep } from '@astrojs/cli-kit/utils';
import { execa } from 'execa'; import { execa } from 'execa';
import { exec } from 'node:child_process'; import { exec } from 'node:child_process';
import { get } from 'node:https'; import fetch from 'node-fetch-native';
import stripAnsi from 'strip-ansi'; import stripAnsi from 'strip-ansi';
import detectPackageManager from 'which-pm-runs'; import detectPackageManager from 'which-pm-runs';
@ -15,7 +15,7 @@ async function getRegistry(): Promise<string> {
const packageManager = detectPackageManager()?.name || 'npm'; const packageManager = detectPackageManager()?.name || 'npm';
try { try {
const { stdout } = await execa(packageManager, ['config', 'get', 'registry']); const { stdout } = await execa(packageManager, ['config', 'get', 'registry']);
return stdout || 'https://registry.npmjs.org'; return stdout?.trim()?.replace(/\/$/, '') || 'https://registry.npmjs.org';
} catch (e) { } catch (e) {
return 'https://registry.npmjs.org'; return 'https://registry.npmjs.org';
} }
@ -81,17 +81,11 @@ let v: string;
export const getVersion = () => export const getVersion = () =>
new Promise<string>(async (resolve) => { new Promise<string>(async (resolve) => {
if (v) return resolve(v); if (v) return resolve(v);
const registry = await getRegistry(); let registry = await getRegistry();
get(`${registry}/astro/latest`, (res) => { const { version } = await fetch(`${registry}/astro/latest`, { redirect: 'follow' }).then(res => res.json());
let body = '';
res.on('data', (chunk) => (body += chunk));
res.on('end', () => {
const { version } = JSON.parse(body);
v = version; v = version;
resolve(version); resolve(version);
}); });
});
});
export const log = (message: string) => stdout.write(message + '\n'); export const log = (message: string) => stdout.write(message + '\n');
export const banner = async (version: string) => export const banner = async (version: string) =>

View file

@ -3574,6 +3574,9 @@ importers:
mocha: mocha:
specifier: ^9.2.2 specifier: ^9.2.2
version: 9.2.2 version: 9.2.2
node-fetch-native:
specifier: ^1.2.0
version: 1.2.0
which-pm-runs: which-pm-runs:
specifier: ^1.1.0 specifier: ^1.1.0
version: 1.1.0 version: 1.1.0
@ -11921,7 +11924,7 @@ packages:
defu: 6.1.2 defu: 6.1.2
https-proxy-agent: 5.0.1 https-proxy-agent: 5.0.1
mri: 1.2.0 mri: 1.2.0
node-fetch-native: 1.1.1 node-fetch-native: 1.2.0
pathe: 1.1.0 pathe: 1.1.0
tar: 6.1.14 tar: 6.1.14
transitivePeerDependencies: transitivePeerDependencies:
@ -14162,8 +14165,8 @@ packages:
engines: {node: '>=10.5.0'} engines: {node: '>=10.5.0'}
dev: false dev: false
/node-fetch-native@1.1.1: /node-fetch-native@1.2.0:
resolution: {integrity: sha512-9VvspTSUp2Sxbl+9vbZTlFGq9lHwE8GDVVekxx6YsNd1YH59sb3Ba8v3Y3cD8PkLNcileGGcA21PFjVl0jzDaw==} resolution: {integrity: sha512-5IAMBTl9p6PaAjYCnMv5FmqIF6GcZnawAVnzaCG0rX2aYZJ4CxEkZNtVPuTRug7fL7wyM5BQYTlAzcyMPi6oTQ==}
dev: false dev: false
/node-fetch@2.6.11: /node-fetch@2.6.11: