@@ -10,7 +10,7 @@ import type { Plugin } from "vite-plus";
1010const THIRD_PARTY_LICENSES_FILE_NAME = "third-party-licenses.json" ;
1111const SPDX_LICENSE_LIST_VERSION = "v3.28.0" ;
1212const SPDX_LICENSE_LIST_REVISION = "c4a7237ec8f4654e867546f9f409749300f1bf4c" ;
13- const GENERATED_NOTICE_CACHE_DIRECTORY = ".generated/ third-party-licenses/spdx" ;
13+ const SPDX_TEMPLATE_DIRECTORY = "third-party-licenses/spdx" ;
1414
1515export interface ThirdPartyLicenseEntry {
1616 readonly bundles : ReadonlyArray < string > ;
@@ -312,10 +312,10 @@ async function readConfig(configFile: string | URL | undefined): Promise<{
312312 } ;
313313}
314314
315- function spdxLicenseCachePath ( configDirectory : string , licenseId : string ) : string {
315+ function spdxTemplatePath ( configDirectory : string , licenseId : string ) : string {
316316 return NodePath . join (
317317 configDirectory ,
318- GENERATED_NOTICE_CACHE_DIRECTORY ,
318+ SPDX_TEMPLATE_DIRECTORY ,
319319 SPDX_LICENSE_LIST_VERSION ,
320320 `${ licenseId } .json` ,
321321 ) ;
@@ -333,12 +333,12 @@ function decodeSpdxLicenseDetails(value: unknown, expectedLicenseId: string): Sp
333333 return { licenseId : expectedLicenseId , licenseText : value . licenseText . trim ( ) } ;
334334}
335335
336- async function readCachedSpdxLicense (
336+ async function readSpdxLicense (
337337 configDirectory : string ,
338338 licenseId : string ,
339339) : Promise < SpdxLicenseDetails | null > {
340340 try {
341- const source = await NodeFSP . readFile ( spdxLicenseCachePath ( configDirectory , licenseId ) , "utf8" ) ;
341+ const source = await NodeFSP . readFile ( spdxTemplatePath ( configDirectory , licenseId ) , "utf8" ) ;
342342 return decodeSpdxLicenseDetails ( JSON . parse ( source ) as unknown , licenseId ) ;
343343 } catch ( error ) {
344344 const code = isRecord ( error ) && typeof error . code === "string" ? error . code : null ;
@@ -347,10 +347,7 @@ async function readCachedSpdxLicense(
347347 }
348348}
349349
350- async function downloadSpdxLicense (
351- configDirectory : string ,
352- licenseId : string ,
353- ) : Promise < SpdxLicenseDetails > {
350+ async function downloadSpdxLicense ( configDirectory : string , licenseId : string ) : Promise < void > {
354351 const url = `https://raw.githubusercontent.com/spdx/license-list-data/${ SPDX_LICENSE_LIST_REVISION } /json/details/${ encodeURIComponent ( licenseId ) } .json` ;
355352 const response = await fetch ( url ) ;
356353 if ( ! response . ok ) {
@@ -359,20 +356,9 @@ async function downloadSpdxLicense(
359356 ) ;
360357 }
361358 const details = decodeSpdxLicenseDetails ( ( await response . json ( ) ) as unknown , licenseId ) ;
362- const cachePath = spdxLicenseCachePath ( configDirectory , licenseId ) ;
363- await NodeFSP . mkdir ( NodePath . dirname ( cachePath ) , { recursive : true } ) ;
364- await NodeFSP . writeFile ( cachePath , `${ JSON . stringify ( details ) } \n` , "utf8" ) ;
365- return details ;
366- }
367-
368- async function resolveSpdxLicense (
369- configDirectory : string ,
370- licenseId : string ,
371- allowMissing : boolean ,
372- ) : Promise < SpdxLicenseDetails | null > {
373- const cached = await readCachedSpdxLicense ( configDirectory , licenseId ) ;
374- if ( cached || allowMissing ) return cached ;
375- return downloadSpdxLicense ( configDirectory , licenseId ) ;
359+ const templatePath = spdxTemplatePath ( configDirectory , licenseId ) ;
360+ await NodeFSP . mkdir ( NodePath . dirname ( templatePath ) , { recursive : true } ) ;
361+ await NodeFSP . writeFile ( templatePath , `${ JSON . stringify ( details , null , 2 ) } \n` , "utf8" ) ;
376362}
377363
378364function renderGeneratedNotice ( config : GeneratedNoticeConfigEntry , licenseText : string ) : string {
@@ -397,17 +383,19 @@ function renderGeneratedNotice(config: GeneratedNoticeConfigEntry, licenseText:
397383async function generatedNoticeText (
398384 configs : ReadonlyArray < GeneratedNoticeConfigEntry > ,
399385 configDirectory : string ,
400- allowMissing : boolean ,
401- ) : Promise < string | null > {
386+ ) : Promise < string > {
402387 const sections = await Promise . all (
403388 configs . map ( async ( config ) => {
404- const details = await resolveSpdxLicense ( configDirectory , config . licenseId , allowMissing ) ;
405- return details ? renderGeneratedNotice ( config , details . licenseText ) : null ;
389+ const details = await readSpdxLicense ( configDirectory , config . licenseId ) ;
390+ if ( ! details ) {
391+ throw new Error (
392+ `SPDX license template ${ config . licenseId } is missing. Run \`vp run licenses:sync\` and commit ${ SPDX_TEMPLATE_DIRECTORY } .` ,
393+ ) ;
394+ }
395+ return renderGeneratedNotice ( config , details . licenseText ) ;
406396 } ) ,
407397 ) ;
408- return sections . some ( ( section ) => section === null )
409- ? null
410- : ( sections as ReadonlyArray < string > ) . join ( "\n\n---\n\n" ) ;
398+ return sections . join ( "\n\n---\n\n" ) ;
411399}
412400
413401function configuredGeneratedNotices (
@@ -426,7 +414,13 @@ async function syncConfiguredGeneratedNotices(
426414 const licenseIds = [
427415 ...new Set ( configuredGeneratedNotices ( config ) . map ( ( notice ) => notice . licenseId ) ) ,
428416 ] . sort ( ( left , right ) => left . localeCompare ( right ) ) ;
429- await Promise . all ( licenseIds . map ( ( licenseId ) => resolveSpdxLicense ( directory , licenseId , false ) ) ) ;
417+ await Promise . all (
418+ licenseIds . map ( async ( licenseId ) => {
419+ if ( ! ( await readSpdxLicense ( directory , licenseId ) ) ) {
420+ await downloadSpdxLicense ( directory , licenseId ) ;
421+ }
422+ } ) ,
423+ ) ;
430424}
431425
432426export async function syncThirdPartyLicenseNotices ( configFile : string | URL ) : Promise < void > {
@@ -844,8 +838,7 @@ async function packageEntry(
844838 configDirectory : string ,
845839 packageNotices : Map < string , Promise < string | null > > ,
846840 repositoryNotices : ReadonlyMap < string , string > ,
847- allowMissingGeneratedNotices : boolean ,
848- ) : Promise < ThirdPartyLicenseEntry | null > {
841+ ) : Promise < ThirdPartyLicenseEntry > {
849842 const name =
850843 typeof collected . packageJson . name === "string"
851844 ? collected . packageJson . name
@@ -867,19 +860,14 @@ async function packageEntry(
867860
868861 const repositoryKey = repositoryNoticeKey ( collected . packageJson , license ) ;
869862 const noticeText = override ?. generatedNotices
870- ? await generatedNoticeText (
871- override . generatedNotices ,
872- configDirectory ,
873- allowMissingGeneratedNotices ,
874- )
863+ ? await generatedNoticeText ( override . generatedNotices , configDirectory )
875864 : override ?. noticeFile
876865 ? (
877866 await NodeFSP . readFile ( NodePath . resolve ( configDirectory , override . noticeFile ) , "utf8" )
878867 ) . trim ( )
879868 : ( ( await packageNoticeText ( collected . packageRoot , packageNotices ) ) ??
880869 ( repositoryKey ? repositoryNotices . get ( repositoryKey ) : undefined ) ) ;
881870 if ( ! noticeText ) {
882- if ( override ?. generatedNotices && allowMissingGeneratedNotices ) return null ;
883871 throw new Error (
884872 `${ name } @${ version } does not include a license or notice file. Add a package override with "noticeFile" or "generatedNotice" in the third-party license config.` ,
885873 ) ;
@@ -900,7 +888,6 @@ async function customEntries(
900888 config : ThirdPartyLicensesConfig ,
901889 configDirectory : string ,
902890 includedBundles : ReadonlySet < string > ,
903- allowMissingGeneratedNotices : boolean ,
904891) : Promise < ReadonlyArray < ThirdPartyLicenseEntry > > {
905892 const deviceToolVersions : unknown = JSON . parse (
906893 await NodeFSP . readFile (
@@ -920,11 +907,7 @@ async function customEntries(
920907 )
921908 . map ( async ( notice ) => {
922909 const noticeText = notice . generatedNotices
923- ? await generatedNoticeText (
924- notice . generatedNotices ,
925- configDirectory ,
926- allowMissingGeneratedNotices ,
927- )
910+ ? await generatedNoticeText ( notice . generatedNotices , configDirectory )
928911 : (
929912 await Promise . all (
930913 notice . noticeFiles ! . map ( async ( noticeFile ) => {
@@ -938,10 +921,6 @@ async function customEntries(
938921 } ) ,
939922 )
940923 ) . join ( "\n\n---\n\n" ) ;
941- if ( noticeText === null ) {
942- if ( allowMissingGeneratedNotices ) return null ;
943- throw new Error ( `Could not generate custom third-party notice "${ notice . name } ".` ) ;
944- }
945924 if ( noticeText . length === 0 ) {
946925 throw new Error ( `Custom third-party notice "${ notice . name } " is empty.` ) ;
947926 }
@@ -961,7 +940,7 @@ async function customEntries(
961940 } ;
962941 } ) ,
963942 ) ;
964- return entries . flatMap ( ( entry ) => ( entry ? [ entry ] : [ ] ) ) ;
943+ return entries ;
965944}
966945
967946function entrySort ( left : ThirdPartyLicenseEntry , right : ThirdPartyLicenseEntry ) : number {
@@ -990,15 +969,11 @@ export async function generateThirdPartyLicenseManifest(input: {
990969 readonly packageManifests : ReadonlyArray < ThirdPartyLicensePackageManifest > ;
991970 readonly bundledModuleIds ?: ReadonlyArray < string > ;
992971 readonly bundleName ?: string ;
993- readonly allowMissingGeneratedNotices ?: boolean ;
994972} ) : Promise < ThirdPartyLicenseManifest > {
995973 const [ { config, directory } , collection ] = await Promise . all ( [
996974 readConfig ( input . configFile ) ,
997975 collectProductionDependencyPackages ( input . packageManifests ) ,
998976 ] ) ;
999- if ( ! ( input . allowMissingGeneratedNotices ?? false ) ) {
1000- await syncConfiguredGeneratedNotices ( config , directory ) ;
1001- }
1002977 if ( input . bundledModuleIds && input . bundleName ) {
1003978 await addBundledModulePackages ( collection , input . bundledModuleIds , input . bundleName ) ;
1004979 }
@@ -1008,14 +983,7 @@ export async function generateThirdPartyLicenseManifest(input: {
1008983
1009984 const packageEntryResults = await Promise . allSettled (
1010985 [ ...collection . byIdentity . values ( ) ] . map ( ( collected ) =>
1011- packageEntry (
1012- collected ,
1013- config ,
1014- directory ,
1015- packageNotices ,
1016- repositoryNotices ,
1017- input . allowMissingGeneratedNotices ?? false ,
1018- ) ,
986+ packageEntry ( collected , config , directory , packageNotices , repositoryNotices ) ,
1019987 ) ,
1020988 ) ;
1021989 const failures = packageEntryResults . flatMap ( ( result ) =>
@@ -1029,16 +997,11 @@ export async function generateThirdPartyLicenseManifest(input: {
1029997 ) ;
1030998 }
1031999 const packageEntries = packageEntryResults . flatMap ( ( result ) =>
1032- result . status === "fulfilled" && result . value ? [ result . value ] : [ ] ,
1000+ result . status === "fulfilled" ? [ result . value ] : [ ] ,
10331001 ) ;
10341002 const includedBundles = new Set ( input . packageManifests . map ( ( manifest ) => manifest . bundle ) ) ;
10351003 if ( input . bundleName ) includedBundles . add ( input . bundleName ) ;
1036- const manualEntries = await customEntries (
1037- config ,
1038- directory ,
1039- includedBundles ,
1040- input . allowMissingGeneratedNotices ?? false ,
1041- ) ;
1004+ const manualEntries = await customEntries ( config , directory , includedBundles ) ;
10421005 const entries = [ ...packageEntries , ...manualEntries ] . sort ( entrySort ) ;
10431006 assertUniqueEntries ( entries ) ;
10441007 return {
@@ -1074,7 +1037,6 @@ export function thirdPartyLicensesPlugin(options: ThirdPartyLicensesPluginOption
10741037 manifestPromise ??= generateThirdPartyLicenseManifest ( {
10751038 packageManifests : options . packageManifests ,
10761039 bundleName : options . bundleName ,
1077- allowMissingGeneratedNotices : true ,
10781040 ...( options . configFile !== undefined ? { configFile : options . configFile } : { } ) ,
10791041 } ) . catch ( ( error : unknown ) => {
10801042 manifestPromise = null ;
0 commit comments