Skip to content

Commit

Permalink
Fix multiple create-astro registry issues (#7539)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
4 people authored Jun 30, 2023
1 parent 2172dd4 commit 1170877
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 16 deletions.
6 changes: 6 additions & 0 deletions .changeset/old-seahorses-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'astro': patch
'create-astro': patch
---

Update registry logic, improving edge cases (http support, redirects, registries ending with '/')
2 changes: 1 addition & 1 deletion packages/astro/src/core/add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ async function getRegistry(): Promise<string> {
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';
}
Expand Down
1 change: 1 addition & 0 deletions packages/create-astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
18 changes: 6 additions & 12 deletions packages/create-astro/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -15,7 +15,7 @@ async function getRegistry(): Promise<string> {
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';
}
Expand Down Expand Up @@ -81,16 +81,10 @@ let v: string;
export const getVersion = () =>
new Promise<string>(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');
Expand Down
9 changes: 6 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1170877

Please sign in to comment.