Skip to content

Commit

Permalink
refactor: make verify more defensive
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re committed Nov 27, 2023
1 parent f1a16c9 commit 203530f
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions packages/upgrade/src/actions/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,16 @@ function collectPackageInfo(ctx: Pick<Context, 'version' | 'packages'>, dependen
}
}

async function verifyVersions(ctx: Pick<Context, 'version' | 'packages'>, registry: string) {
async function verifyVersions(ctx: Pick<Context, 'version' | 'packages' | 'exit'>, registry: string) {
const tasks: Promise<void>[] = [];
for (const packageInfo of ctx.packages) {
tasks.push(resolveTargetVersion(packageInfo, registry));
}
await Promise.all(tasks);
try {
await Promise.all(tasks);
} catch {
return false;
}
for (const packageInfo of ctx.packages) {
if (!packageInfo.targetVersion) {
return false;
Expand All @@ -107,6 +111,9 @@ async function verifyVersions(ctx: Pick<Context, 'version' | 'packages'>, regist

async function resolveTargetVersion(packageInfo: PackageInfo, registry: string): Promise<void> {
const packageMetadata = await fetch(`${registry}/${packageInfo.name}`, { headers: { accept: 'application/vnd.npm.install-v1+json' }});
if (packageMetadata.status >= 400) {
throw new Error(`Unable to resolve "${packageInfo.name}"`);
}
const { "dist-tags": distTags } = await packageMetadata.json();
let version = distTags[packageInfo.targetVersion];
if (version) {
Expand All @@ -129,13 +136,18 @@ async function resolveTargetVersion(packageInfo: PackageInfo, registry: string):
if (packageInfo.name === 'astro') {
const upgradeGuide = `https://docs.astro.build/en/guides/upgrade-to/v${toVersion.major}/`;
const docsRes = await fetch(upgradeGuide);
// OK if this request fails, it's probably a prerelease without a public migration guide.
// In that case, we should fallback to the CHANGELOG check below.
if (docsRes.status === 200) {
packageInfo.changelogURL = upgradeGuide;
packageInfo.changelogTitle = `Upgrade to Astro v${toVersion.major}`;
return;
}
}
const latestMetadata = await fetch(`${registry}/${packageInfo.name}/latest`);
if (latestMetadata.status >= 400) {
throw new Error(`Unable to resolve "${packageInfo.name}"`);
}
const { repository } = await latestMetadata.json();
const branch = bump === 'premajor' ? 'next' : 'main';
packageInfo.changelogURL = extractChangelogURLFromRepository(repository, version, branch);
Expand Down

0 comments on commit 203530f

Please sign in to comment.