@@ -113,43 +113,47 @@ async function getPackageJsonLicenseFromMaster(
113113 repo : string ,
114114) : Promise < string | undefined > {
115115 // Spec: license from package.json on master HEAD commit, if present.
116- // We intentionally only check the 'master' branch as requested,
117- // and do not fallback to 'main' here.
118- try {
119- const raw = await fetchText (
120- `https://raw.githubusercontent.com/${ owner } /${ repo } /master/package.json` ,
121- ) ;
116+ // Fallback: if 'master' isn't available, try 'main'.
117+ const candidateBranches = [ 'master' , 'main' ] as const ;
118+
119+ for ( const branch of candidateBranches ) {
122120 try {
123- const pkg = JSON . parse ( raw ) as unknown ;
124- if (
125- pkg &&
126- typeof pkg === 'object' &&
127- 'license' in ( pkg as Record < string , unknown > )
128- ) {
129- const lic = ( pkg as Record < string , unknown > ) . license ;
130- if ( typeof lic === 'string' ) return lic ;
121+ const raw = await fetchText (
122+ `https://raw.githubusercontent.com/${ owner } /${ repo } /${ branch } /package.json` ,
123+ ) ;
124+ try {
125+ const pkg = JSON . parse ( raw ) as unknown ;
131126 if (
132- lic &&
133- typeof lic === 'object' &&
134- 'type' in ( lic as Record < string , unknown > ) &&
135- typeof ( lic as Record < string , unknown > ) . type === 'string'
136- )
137- return ( lic as Record < string , unknown > ) . type as string ;
138- }
139- // Support legacy `licenses` array
140- if ( pkg && typeof pkg === 'object' ) {
141- const anyPkg = pkg as Record < string , unknown > ;
142- const licensesRaw = anyPkg . licenses ;
143- if ( Array . isArray ( licensesRaw ) && licensesRaw . length > 0 ) {
144- const first = licensesRaw [ 0 ] as { type ?: unknown } ;
145- if ( typeof first . type === 'string' ) return first . type ;
127+ pkg &&
128+ typeof pkg === 'object' &&
129+ 'license' in ( pkg as Record < string , unknown > )
130+ ) {
131+ const lic = ( pkg as Record < string , unknown > ) . license ;
132+ if ( typeof lic === 'string' ) return lic ;
133+ if (
134+ lic &&
135+ typeof lic === 'object' &&
136+ 'type' in ( lic as Record < string , unknown > )
137+ ) {
138+ const typeVal = ( lic as Record < string , unknown > ) . type ;
139+ if ( typeof typeVal === 'string' ) return typeVal ;
140+ }
141+ }
142+ // Support legacy `licenses` array
143+ if ( pkg && typeof pkg === 'object' ) {
144+ const anyPkg = pkg as Record < string , unknown > ;
145+ const licensesRaw = anyPkg . licenses ;
146+ if ( Array . isArray ( licensesRaw ) && licensesRaw . length > 0 ) {
147+ const first = licensesRaw [ 0 ] as { type ?: unknown } ;
148+ if ( typeof first . type === 'string' ) return first . type ;
149+ }
146150 }
151+ } catch {
152+ // ignore JSON parsing errors and try next branch
147153 }
148154 } catch {
149- // ignore JSON parsing errors and fall through
155+ // package.json not found or inaccessible on this branch; try next
150156 }
151- } catch {
152- // master/package.json not found or inaccessible
153157 }
154158 return undefined ;
155159}
0 commit comments