Skip to content

Commit

Permalink
Warn if astro add fetch failed with non 404 status (withastro#9121)
Browse files Browse the repository at this point in the history
Co-authored-by: bluwy <[email protected]>
  • Loading branch information
peng and bluwy authored Nov 17, 2023
1 parent e3dce21 commit f4efd1c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/brave-taxis-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Adds a warning if `astro add` fetches a package but returns a non-404 status
11 changes: 10 additions & 1 deletion packages/astro/src/cli/add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -731,8 +731,11 @@ async function fetchPackageJson(
const res = await fetch(`${registry}/${packageName}/${tag}`);
if (res.status >= 200 && res.status < 300) {
return await res.json();
} else {
} else if (res.status === 404) {
// 404 means the package doesn't exist, so we don't need an error message here
return new Error();
} else {
return new Error(`Failed to fetch ${registry}/${packageName}/${tag} - GET ${res.status}`);
}
}

Expand All @@ -754,6 +757,9 @@ export async function validateIntegrations(integrations: string[]): Promise<Inte
} else {
const firstPartyPkgCheck = await fetchPackageJson('@astrojs', name, tag);
if (firstPartyPkgCheck instanceof Error) {
if (firstPartyPkgCheck.message) {
spinner.warn(yellow(firstPartyPkgCheck.message));
}
spinner.warn(
yellow(`${bold(integration)} is not an official Astro package. Use at your own risk!`)
);
Expand All @@ -780,6 +786,9 @@ export async function validateIntegrations(integrations: string[]): Promise<Inte
if (pkgType === 'third-party') {
const thirdPartyPkgCheck = await fetchPackageJson(scope, name, tag);
if (thirdPartyPkgCheck instanceof Error) {
if (thirdPartyPkgCheck.message) {
spinner.warn(yellow(thirdPartyPkgCheck.message));
}
throw new Error(`Unable to fetch ${bold(integration)}. Does the package exist?`);
} else {
pkgJson = thirdPartyPkgCheck as any;
Expand Down

0 comments on commit f4efd1c

Please sign in to comment.