Skip to content

Commit c0a23b4

Browse files
committed
now able to detect whether gh releases are biing used by a package.
1 parent c3d63c4 commit c0a23b4

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

app/composables/usePackageAnalysis.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export interface PackageAnalysisResponse {
1010
npm?: string
1111
}
1212
createPackage?: CreatePackageInfo
13+
// TODO move this to it's own composable
14+
hasChangelog?: boolean
1315
}
1416

1517
/**

app/pages/package/[...package].vue

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,14 @@ defineOgImageComponent('Package', {
699699
{{ $t('package.links.issues') }}
700700
</a>
701701
</li>
702+
703+
<NuxtLink
704+
v-if="packageAnalysis?.hasChangelog"
705+
class="link-subtle font-mono text-sm inline-flex items-center gap-1.5"
706+
>
707+
<span class="i-carbon:catalog w-4 h-4" aria-hidden="true" />
708+
{{ $t('package.links.changelog') }}
709+
</NuxtLink>
702710
<li>
703711
<a
704712
:href="`https://www.npmjs.com/package/${pkg.name}`"

server/api/registry/analysis/[...pkg].get.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
} from '#shared/utils/constants'
2020
import { parseRepoUrl } from '#shared/utils/git-providers'
2121
import { getLatestVersion, getLatestVersionBatch } from 'fast-npm-meta'
22+
import { detectHasChangelog } from '~~/server/utils/has-changelog'
2223

2324
export default defineCachedEventHandler(
2425
async event => {
@@ -53,11 +54,13 @@ export default defineCachedEventHandler(
5354
const createPackage = await findAssociatedCreatePackage(packageName, pkg)
5455

5556
const analysis = analyzePackage(pkg, { typesPackage, createPackage })
56-
57+
// TODO move this to it's own endpoint
58+
const hasChangelog = await detectHasChangelog(pkg)
5759
return {
5860
package: packageName,
5961
version: pkg.version ?? version ?? 'latest',
6062
...analysis,
63+
hasChangelog,
6164
} satisfies PackageAnalysisResponse
6265
} catch (error: unknown) {
6366
handleApiError(error, {
@@ -215,4 +218,5 @@ function hasSameRepositoryOwner(
215218
export interface PackageAnalysisResponse extends PackageAnalysis {
216219
package: string
217220
version: string
221+
hasChangelog: boolean | null
218222
}

server/utils/has-changelog.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import type { ExtendedPackageJson } from '~~/shared/utils/package-analysis'
2+
import { parseRepoUrl, type RepoRef } from '~~/shared/utils/git-providers'
3+
4+
/**
5+
* Detect whether changelogs/releases are available for this package
6+
*
7+
* first checks if releases are available and then changelog.md
8+
*/
9+
export async function detectHasChangelog(
10+
pkg: ExtendedPackageJson,
11+
// packageName: string,
12+
// version: string,
13+
) {
14+
if (!pkg.repository?.url) {
15+
return false
16+
}
17+
18+
const repoRef = parseRepoUrl(pkg.repository.url)
19+
if (!repoRef) {
20+
return false
21+
}
22+
23+
if (await checkReleases(repoRef)) {
24+
return true
25+
}
26+
27+
return null
28+
}
29+
30+
/**
31+
* check whether releases are being used with this repo
32+
* @returns true if in use
33+
*/
34+
async function checkReleases(ref: RepoRef): Promise<boolean> {
35+
const checkUrls = getLatestReleaseUrl(ref)
36+
37+
for (const checkUrl of checkUrls ?? []) {
38+
const exists = await fetch(checkUrl, {
39+
headers: {
40+
// GitHub API requires User-Agent
41+
'User-Agent': 'npmx.dev',
42+
},
43+
method: 'HEAD', // we just need to know if it exists or not
44+
})
45+
.then(r => r.ok)
46+
.catch(() => false)
47+
if (exists) {
48+
return true
49+
}
50+
}
51+
return false
52+
}
53+
54+
/**
55+
* get the url to check if releases are being used.
56+
*
57+
* @returns returns an array so that if providers don't have a latest that we can check for versions
58+
*/
59+
function getLatestReleaseUrl(ref: RepoRef): null | string[] {
60+
switch (ref.provider) {
61+
case 'github':
62+
return [`https://ungh.cc/repos/${ref.owner}/${ref.repo}/releases/latest`]
63+
}
64+
65+
return null
66+
}

0 commit comments

Comments
 (0)