|
1 | | -import type { MaybeRefOrGetter } from 'vue' |
2 | | -import { toValue } from 'vue' |
| 1 | +import type { MaybeRefOrGetter } from "vue"; |
| 2 | +import { toValue } from "vue"; |
3 | 3 |
|
4 | 4 | export interface LicenseChange { |
5 | | - from: string |
6 | | - to: string |
7 | | - version: string |
| 5 | + from: string; |
| 6 | + to: string; |
| 7 | + version: string; |
8 | 8 | } |
9 | 9 |
|
10 | 10 | export interface LicenseChangesResult { |
11 | | - changes: LicenseChange[] |
| 11 | + changes: LicenseChange[]; |
12 | 12 | } |
13 | 13 |
|
14 | 14 | // Type definitions for npm registry response |
15 | 15 | interface NpmRegistryVersion { |
16 | | - version: string |
17 | | - license?: string |
| 16 | + version: string; |
| 17 | + license?: string; |
18 | 18 | } |
19 | 19 |
|
20 | 20 | // for registry responses of $fetch function, the type includes the key versions as well as many others too. |
21 | 21 | interface NpmRegistryResponse { |
22 | | - versions: Record< |
23 | | - string, |
24 | | - { |
25 | | - version: string |
26 | | - license?: string |
27 | | - } |
28 | | - > |
| 22 | + time: Record<string, string>; |
| 23 | + versions: Record<string, NpmRegistryVersion>; |
29 | 24 | } |
30 | 25 |
|
31 | 26 | /** |
32 | 27 | * Composable to detect license changes across all versions of a package |
33 | 28 | */ |
34 | | -export function useLicenseChanges(packageName: MaybeRefOrGetter<string | null | undefined>) { |
| 29 | +export function useLicenseChanges( |
| 30 | + packageName: MaybeRefOrGetter<string | null | undefined>, |
| 31 | +) { |
35 | 32 | return useAsyncData<LicenseChangesResult>( |
36 | 33 | () => `license-changes:${toValue(packageName)}`, |
37 | 34 | async () => { |
38 | | - const name = toValue(packageName) |
39 | | - if (!name) return { changes: [] } |
| 35 | + const name = toValue(packageName); |
| 36 | + if (!name) return { changes: [] }; |
40 | 37 |
|
41 | 38 | // Fetch full package metadata from npm registry |
42 | | - const url = `https://registry.npmjs.org/${name}` |
43 | | - const data = await $fetch<NpmRegistryResponse>(url) |
| 39 | + const url = `https://registry.npmjs.org/${name}`; |
| 40 | + const data = await $fetch<NpmRegistryResponse>(url); |
44 | 41 |
|
45 | | - const changes: LicenseChange[] = [] |
46 | | - let prevLicense: string | undefined = undefined |
| 42 | + const changes: LicenseChange[] = []; |
| 43 | + let prevLicense: string | undefined = undefined; |
47 | 44 |
|
48 | 45 | // `data.versions` is an object with version keys |
49 | | - const versions = Object.values(data.versions) as NpmRegistryVersion[] |
| 46 | + const versions = Object.values(data.versions) as NpmRegistryVersion[]; |
50 | 47 |
|
51 | 48 | // Sort versions ascending to compare chronologically |
52 | 49 | versions.sort((a, b) => { |
53 | | - const parse = (v: string) => v.split('.').map(Number) |
54 | | - const [aMajor, aMinor, aPatch] = parse(a.version as string) |
55 | | - const [bMajor, bMinor, bPatch] = parse(b.version as string) |
56 | | - if (aMajor !== bMajor) return aMajor! - bMajor! |
57 | | - if (aMinor !== bMinor) return aMinor! - bMinor! |
58 | | - return aPatch! - bPatch! |
59 | | - }) |
| 50 | + const dateA = new Date(data.time[a.version] as string).getTime(); |
| 51 | + const dateB = new Date(data.time[b.version] as string).getTime(); |
| 52 | + |
| 53 | + // Ascending order (oldest to newest) |
| 54 | + return dateA - dateB; |
| 55 | + }); |
60 | 56 |
|
61 | 57 | // Detect license changes |
62 | 58 | for (const version of versions) { |
63 | | - const license = (version.license as string) ?? 'UNKNOWN' |
| 59 | + const license = (version.license as string) ?? "UNKNOWN"; |
64 | 60 | if (prevLicense && license !== prevLicense) { |
65 | 61 | changes.push({ |
66 | 62 | from: prevLicense, |
67 | 63 | to: license, |
68 | 64 | version: version.version as string, |
69 | | - }) |
| 65 | + }); |
70 | 66 | } |
71 | | - prevLicense = license |
| 67 | + prevLicense = license; |
72 | 68 | } |
73 | | - return { changes } |
| 69 | + return { changes }; |
74 | 70 | }, |
75 | 71 | { |
76 | 72 | default: () => ({ changes: [] }), |
77 | 73 | watch: [() => toValue(packageName)], |
78 | 74 | }, |
79 | | - ) |
| 75 | + ); |
80 | 76 | } |
0 commit comments