-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpackage.ts
More file actions
41 lines (33 loc) · 1.07 KB
/
package.ts
File metadata and controls
41 lines (33 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import type { PackageVersionsInfoWithMetadata } from 'fast-npm-meta'
import { logger } from '#state'
import { getVersions } from 'fast-npm-meta'
import { memoize } from '../memoize'
export interface PackageInfo extends PackageVersionsInfoWithMetadata {
versionToTag: Map<string, string>
}
/**
* Fetch npm package versions and build a version-to-tag lookup map.
*
* @see https://github.com/antfu/fast-npm-meta
*/
export const getPackageInfo = memoize<string, Promise<PackageInfo | null>>(async (name) => {
logger.info(`Fetching package info for ${name}`)
const pkg = await getVersions(name, {
metadata: true,
throw: false,
retry: 3,
})
if ('error' in pkg) {
logger.warn(`Fetching package info for ${name} error: ${JSON.stringify(pkg)}`)
// Return null to trigger a cache hit
return null
}
logger.info(`Fetched package info for ${name}`)
const versionToTag = new Map<string, string>()
if (pkg.distTags) {
for (const [tag, ver] of Object.entries(pkg.distTags)) {
versionToTag.set(ver, tag)
}
}
return { ...pkg, versionToTag }
})