@@ -26,19 +26,26 @@ interface NpmRegistryResponse {
2626/**
2727 * Composable to detect license changes across all versions of a package
2828 */
29- export function useLicenseChanges ( packageName : MaybeRefOrGetter < string | null | undefined > ) {
29+ export function useLicenseChanges (
30+ packageName : MaybeRefOrGetter < string | null | undefined > ,
31+ resolvedVersion : MaybeRefOrGetter < string | null | undefined > = ( ) => undefined ,
32+ ) {
3033 return useAsyncData < LicenseChangesResult > (
31- ( ) => `license-changes:${ toValue ( packageName ) } ` ,
34+ ( ) => {
35+ const name = toValue ( packageName )
36+ const version = toValue ( resolvedVersion ) ?? 'latest'
37+ return `license-changes:${ name } :${ version } `
38+ } ,
3239 async ( ) => {
3340 const name = toValue ( packageName )
41+ const resolvedVer = toValue ( resolvedVersion )
3442 if ( ! name ) return { changes : [ ] }
3543
3644 // Fetch full package metadata from npm registry
3745 const url = `https://registry.npmjs.org/${ name } `
3846 const data = await $fetch < NpmRegistryResponse > ( url )
3947
4048 const changes : LicenseChange [ ] = [ ]
41- let prevLicense : string | undefined = undefined
4249
4350 // `data.versions` is an object with version keys
4451 const versions = Object . values ( data . versions ) as NpmRegistryVersion [ ]
@@ -52,23 +59,30 @@ export function useLicenseChanges(packageName: MaybeRefOrGetter<string | null |
5259 return dateA - dateB
5360 } )
5461
55- // Detect license changes
56- for ( const version of versions ) {
57- const license = ( version . license as string ) ?? 'UNKNOWN'
58- if ( prevLicense && license !== prevLicense ) {
59- changes . push ( {
60- from : prevLicense ,
61- to : license ,
62- version : version . version as string ,
63- } )
62+ // When resolvedVer is not provided, check changes across all versions
63+ const targetVersion = resolvedVer ?? versions [ versions . length - 1 ] ?. version
64+
65+ if ( targetVersion ) {
66+ const resolvedIndex = versions . findIndex ( v => v . version === targetVersion )
67+
68+ if ( resolvedIndex > 0 ) {
69+ const currentLicense = ( versions [ resolvedIndex ] ?. license as string ) ?? 'UNKNOWN'
70+ const previousLicense = ( versions [ resolvedIndex - 1 ] ?. license as string ) ?? 'UNKNOWN'
71+
72+ if ( currentLicense !== previousLicense ) {
73+ changes . push ( {
74+ from : previousLicense ,
75+ to : currentLicense ,
76+ version : ( versions [ resolvedIndex ] ?. version as string ) || 'UNKNOWN' ,
77+ } )
78+ }
6479 }
65- prevLicense = license
6680 }
6781 return { changes }
6882 } ,
6983 {
7084 default : ( ) => ( { changes : [ ] } ) ,
71- watch : [ ( ) => toValue ( packageName ) ] ,
85+ watch : [ ( ) => toValue ( packageName ) , ( ) => toValue ( resolvedVersion ) ] ,
7286 } ,
7387 )
7488}
0 commit comments