Skip to content

Commit 2a397bf

Browse files
committed
added detection of a changelog file on the root of the github repo
1 parent 92193b5 commit 2a397bf

File tree

4 files changed

+67
-24
lines changed

4 files changed

+67
-24
lines changed

app/composables/usePackageHasChangelog.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,10 @@ export function usePackageHasChangelog(
22
packageName: MaybeRefOrGetter<string>,
33
version?: MaybeRefOrGetter<string | null | undefined>,
44
) {
5-
return useLazyFetch<boolean>(
6-
() => {
7-
const name = toValue(packageName)
8-
const ver = toValue(version)
9-
const base = `/api/changelog/has/${name}`
10-
return ver ? `${base}/v/${ver}` : base
11-
},
12-
{
13-
onResponse(r) {
14-
console.log({ r })
15-
},
16-
},
17-
)
5+
return useLazyFetch<boolean>(() => {
6+
const name = toValue(packageName)
7+
const ver = toValue(version)
8+
const base = `/api/changelog/has/${name}`
9+
return ver ? `${base}/v/${ver}` : base
10+
})
1811
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,6 @@ const { data: packageAnalysis } = usePackageAnalysis(packageName, requestedVersi
119119
const { data: moduleReplacement } = useModuleReplacement(packageName)
120120
const { data: hasChangelog } = usePackageHasChangelog(packageName, requestedVersion)
121121
122-
watchEffect(() => console.log('hasChangelog', hasChangelog.value))
123-
124122
const {
125123
data: resolvedVersion,
126124
status: versionStatus,
@@ -759,13 +757,15 @@ onKeyStroke(
759757
{{ $t('package.links.issues') }}
760758
</a>
761759
</li>
762-
<NuxtLink
763-
v-if="hasChangelog"
764-
class="link-subtle font-mono text-sm inline-flex items-center gap-1.5"
765-
>
766-
<span class="i-carbon:catalog w-4 h-4" aria-hidden="true" />
767-
{{ $t('package.links.changelog') }}
768-
</NuxtLink>
760+
<li>
761+
<NuxtLink
762+
v-if="hasChangelog"
763+
class="link-subtle font-mono text-sm inline-flex items-center gap-1.5"
764+
>
765+
<span class="i-carbon:warning w-4 h-4" aria-hidden="true" />
766+
{{ $t('package.links.changelog') }}
767+
</NuxtLink>
768+
</li>
769769
<li>
770770
<a
771771
:href="`https://www.npmjs.com/package/${pkg.name}`"

server/api/changelog/has/[...pkg].get.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default defineCachedEventHandler(
2626
`${NPM_REGISTRY}/${encodedName}${versionSuffix}`,
2727
)
2828

29-
return await detectHasChangelog(pkg)
29+
return !!(await detectHasChangelog(pkg))
3030
} catch (error) {
3131
handleApiError(error, {
3232
statusCode: 502,

server/utils/has-changelog.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export async function detectHasChangelog(
2424
return true
2525
}
2626

27-
return false
27+
return checkChangelogFile(repoRef)
2828
}
2929

3030
/**
@@ -64,3 +64,53 @@ function getLatestReleaseUrl(ref: RepoRef): null | string[] {
6464

6565
return null
6666
}
67+
68+
const CHANGELOG_FILENAMES = ['changelog', 'history', 'changes', 'news', 'releases'] as const
69+
70+
async function checkChangelogFile(ref: RepoRef) {
71+
const checkUrls = getChangelogUrls(ref)
72+
73+
for (const checkUrl of checkUrls ?? []) {
74+
const exists = await fetch(checkUrl, {
75+
headers: {
76+
// GitHub API requires User-Agent
77+
'User-Agent': 'npmx.dev',
78+
},
79+
method: 'HEAD', // we just need to know if it exists or not
80+
})
81+
.then(r => r.ok)
82+
.catch(() => false)
83+
if (exists) {
84+
console.log('exists', checkUrl)
85+
return true
86+
}
87+
}
88+
return false
89+
}
90+
91+
function getChangelogUrls(ref: RepoRef) {
92+
const baseUrl = getBaseFileUrl(ref)
93+
if (!baseUrl) {
94+
return
95+
}
96+
97+
return CHANGELOG_FILENAMES.flatMap(fileName => {
98+
const fileNameUpCase = fileName.toUpperCase()
99+
return [
100+
`${baseUrl}/${fileNameUpCase}.md`,
101+
`${baseUrl}/${fileName}.md`,
102+
`${baseUrl}/${fileNameUpCase}`,
103+
`${baseUrl}/${fileName}`,
104+
`${baseUrl}/${fileNameUpCase}.txt`,
105+
`${baseUrl}/${fileName}.txt`,
106+
]
107+
})
108+
}
109+
110+
function getBaseFileUrl(ref: RepoRef) {
111+
switch (ref.provider) {
112+
case 'github':
113+
return `https://ungh.cc/repos/${ref.owner}/${ref.repo}/files/HEAD`
114+
}
115+
return null
116+
}

0 commit comments

Comments
 (0)