@@ -10,7 +10,7 @@ import type { Plugin } from "vite-plus"; |
10 | 10 | const THIRD_PARTY_LICENSES_FILE_NAME = "third-party-licenses.json"; |
11 | 11 | const SPDX_LICENSE_LIST_VERSION = "v3.28.0"; |
12 | 12 | const 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"; |
14 | 14 |
|
15 | 15 | export interface ThirdPartyLicenseEntry { |
16 | 16 | readonly bundles: ReadonlyArray<string>; |
@@ -312,10 +312,10 @@ async function readConfig(configFile: string | URL | undefined): Promise<{ |
312 | 312 | }; |
313 | 313 | } |
314 | 314 |
|
315 | | -function spdxLicenseCachePath(configDirectory: string, licenseId: string): string { |
| 315 | +function spdxTemplatePath(configDirectory: string, licenseId: string): string { |
316 | 316 | return NodePath.join( |
317 | 317 | configDirectory, |
318 | | - GENERATED_NOTICE_CACHE_DIRECTORY, |
| 318 | + SPDX_TEMPLATE_DIRECTORY, |
319 | 319 | SPDX_LICENSE_LIST_VERSION, |
320 | 320 | `${licenseId}.json`, |
321 | 321 | ); |
@@ -333,12 +333,12 @@ function decodeSpdxLicenseDetails(value: unknown, expectedLicenseId: string): Sp |
333 | 333 | return { licenseId: expectedLicenseId, licenseText: value.licenseText.trim() }; |
334 | 334 | } |
335 | 335 |
|
336 | | -async function readCachedSpdxLicense( |
| 336 | +async function readSpdxLicense( |
337 | 337 | configDirectory: string, |
338 | 338 | licenseId: string, |
339 | 339 | ): Promise<SpdxLicenseDetails | null> { |
340 | 340 | try { |
341 | | - const source = await NodeFSP.readFile(spdxLicenseCachePath(configDirectory, licenseId), "utf8"); |
| 341 | + const source = await NodeFSP.readFile(spdxTemplatePath(configDirectory, licenseId), "utf8"); |
342 | 342 | return decodeSpdxLicenseDetails(JSON.parse(source) as unknown, licenseId); |
343 | 343 | } catch (error) { |
344 | 344 | const code = isRecord(error) && typeof error.code === "string" ? error.code : null; |
@@ -347,10 +347,7 @@ async function readCachedSpdxLicense( |
347 | 347 | } |
348 | 348 | } |
349 | 349 |
|
350 | | -async function downloadSpdxLicense( |
351 | | - configDirectory: string, |
352 | | - licenseId: string, |
353 | | -): Promise<SpdxLicenseDetails> { |
| 350 | +async function downloadSpdxLicense(configDirectory: string, licenseId: string): Promise<void> { |
354 | 351 | const url = `https://raw.githubusercontent.com/spdx/license-list-data/${SPDX_LICENSE_LIST_REVISION}/json/details/${encodeURIComponent(licenseId)}.json`; |
355 | 352 | const response = await fetch(url); |
356 | 353 | if (!response.ok) { |
@@ -359,20 +356,9 @@ async function downloadSpdxLicense( |
359 | 356 | ); |
360 | 357 | } |
361 | 358 | 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"); |
376 | 362 | } |
377 | 363 |
|
378 | 364 | function renderGeneratedNotice(config: GeneratedNoticeConfigEntry, licenseText: string): string { |
@@ -397,17 +383,19 @@ function renderGeneratedNotice(config: GeneratedNoticeConfigEntry, licenseText: |
397 | 383 | async function generatedNoticeText( |
398 | 384 | configs: ReadonlyArray<GeneratedNoticeConfigEntry>, |
399 | 385 | configDirectory: string, |
400 | | - allowMissing: boolean, |
401 | | -): Promise<string | null> { |
| 386 | +): Promise<string> { |
402 | 387 | const sections = await Promise.all( |
403 | 388 | 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); |
406 | 396 | }), |
407 | 397 | ); |
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"); |
411 | 399 | } |
412 | 400 |
|
413 | 401 | function configuredGeneratedNotices( |
@@ -426,7 +414,13 @@ async function syncConfiguredGeneratedNotices( |
426 | 414 | const licenseIds = [ |
427 | 415 | ...new Set(configuredGeneratedNotices(config).map((notice) => notice.licenseId)), |
428 | 416 | ].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 | + ); |
430 | 424 | } |
431 | 425 |
|
432 | 426 | export async function syncThirdPartyLicenseNotices(configFile: string | URL): Promise<void> { |
@@ -844,8 +838,7 @@ async function packageEntry( |
844 | 838 | configDirectory: string, |
845 | 839 | packageNotices: Map<string, Promise<string | null>>, |
846 | 840 | repositoryNotices: ReadonlyMap<string, string>, |
847 | | - allowMissingGeneratedNotices: boolean, |
848 | | -): Promise<ThirdPartyLicenseEntry | null> { |
| 841 | +): Promise<ThirdPartyLicenseEntry> { |
849 | 842 | const name = |
850 | 843 | typeof collected.packageJson.name === "string" |
851 | 844 | ? collected.packageJson.name |
@@ -867,19 +860,14 @@ async function packageEntry( |
867 | 860 |
|
868 | 861 | const repositoryKey = repositoryNoticeKey(collected.packageJson, license); |
869 | 862 | const noticeText = override?.generatedNotices |
870 | | - ? await generatedNoticeText( |
871 | | - override.generatedNotices, |
872 | | - configDirectory, |
873 | | - allowMissingGeneratedNotices, |
874 | | - ) |
| 863 | + ? await generatedNoticeText(override.generatedNotices, configDirectory) |
875 | 864 | : override?.noticeFile |
876 | 865 | ? ( |
877 | 866 | await NodeFSP.readFile(NodePath.resolve(configDirectory, override.noticeFile), "utf8") |
878 | 867 | ).trim() |
879 | 868 | : ((await packageNoticeText(collected.packageRoot, packageNotices)) ?? |
880 | 869 | (repositoryKey ? repositoryNotices.get(repositoryKey) : undefined)); |
881 | 870 | if (!noticeText) { |
882 | | - if (override?.generatedNotices && allowMissingGeneratedNotices) return null; |
883 | 871 | throw new Error( |
884 | 872 | `${name}@${version} does not include a license or notice file. Add a package override with "noticeFile" or "generatedNotice" in the third-party license config.`, |
885 | 873 | ); |
@@ -900,7 +888,6 @@ async function customEntries( |
900 | 888 | config: ThirdPartyLicensesConfig, |
901 | 889 | configDirectory: string, |
902 | 890 | includedBundles: ReadonlySet<string>, |
903 | | - allowMissingGeneratedNotices: boolean, |
904 | 891 | ): Promise<ReadonlyArray<ThirdPartyLicenseEntry>> { |
905 | 892 | const deviceToolVersions: unknown = JSON.parse( |
906 | 893 | await NodeFSP.readFile( |
@@ -920,11 +907,7 @@ async function customEntries( |
920 | 907 | ) |
921 | 908 | .map(async (notice) => { |
922 | 909 | const noticeText = notice.generatedNotices |
923 | | - ? await generatedNoticeText( |
924 | | - notice.generatedNotices, |
925 | | - configDirectory, |
926 | | - allowMissingGeneratedNotices, |
927 | | - ) |
| 910 | + ? await generatedNoticeText(notice.generatedNotices, configDirectory) |
928 | 911 | : ( |
929 | 912 | await Promise.all( |
930 | 913 | notice.noticeFiles!.map(async (noticeFile) => { |
@@ -938,10 +921,6 @@ async function customEntries( |
938 | 921 | }), |
939 | 922 | ) |
940 | 923 | ).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 | | - } |
945 | 924 | if (noticeText.length === 0) { |
946 | 925 | throw new Error(`Custom third-party notice "${notice.name}" is empty.`); |
947 | 926 | } |
@@ -961,7 +940,7 @@ async function customEntries( |
961 | 940 | }; |
962 | 941 | }), |
963 | 942 | ); |
964 | | - return entries.flatMap((entry) => (entry ? [entry] : [])); |
| 943 | + return entries; |
965 | 944 | } |
966 | 945 |
|
967 | 946 | function entrySort(left: ThirdPartyLicenseEntry, right: ThirdPartyLicenseEntry): number { |
@@ -990,15 +969,11 @@ export async function generateThirdPartyLicenseManifest(input: { |
990 | 969 | readonly packageManifests: ReadonlyArray<ThirdPartyLicensePackageManifest>; |
991 | 970 | readonly bundledModuleIds?: ReadonlyArray<string>; |
992 | 971 | readonly bundleName?: string; |
993 | | - readonly allowMissingGeneratedNotices?: boolean; |
994 | 972 | }): Promise<ThirdPartyLicenseManifest> { |
995 | 973 | const [{ config, directory }, collection] = await Promise.all([ |
996 | 974 | readConfig(input.configFile), |
997 | 975 | collectProductionDependencyPackages(input.packageManifests), |
998 | 976 | ]); |
999 | | - if (!(input.allowMissingGeneratedNotices ?? false)) { |
1000 | | - await syncConfiguredGeneratedNotices(config, directory); |
1001 | | - } |
1002 | 977 | if (input.bundledModuleIds && input.bundleName) { |
1003 | 978 | await addBundledModulePackages(collection, input.bundledModuleIds, input.bundleName); |
1004 | 979 | } |
@@ -1008,14 +983,7 @@ export async function generateThirdPartyLicenseManifest(input: { |
1008 | 983 |
|
1009 | 984 | const packageEntryResults = await Promise.allSettled( |
1010 | 985 | [...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), |
1019 | 987 | ), |
1020 | 988 | ); |
1021 | 989 | const failures = packageEntryResults.flatMap((result) => |
@@ -1029,16 +997,11 @@ export async function generateThirdPartyLicenseManifest(input: { |
1029 | 997 | ); |
1030 | 998 | } |
1031 | 999 | const packageEntries = packageEntryResults.flatMap((result) => |
1032 | | - result.status === "fulfilled" && result.value ? [result.value] : [], |
| 1000 | + result.status === "fulfilled" ? [result.value] : [], |
1033 | 1001 | ); |
1034 | 1002 | const includedBundles = new Set(input.packageManifests.map((manifest) => manifest.bundle)); |
1035 | 1003 | 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); |
1042 | 1005 | const entries = [...packageEntries, ...manualEntries].sort(entrySort); |
1043 | 1006 | assertUniqueEntries(entries); |
1044 | 1007 | return { |
@@ -1074,7 +1037,6 @@ export function thirdPartyLicensesPlugin(options: ThirdPartyLicensesPluginOption |
1074 | 1037 | manifestPromise ??= generateThirdPartyLicenseManifest({ |
1075 | 1038 | packageManifests: options.packageManifests, |
1076 | 1039 | bundleName: options.bundleName, |
1077 | | - allowMissingGeneratedNotices: true, |
1078 | 1040 | ...(options.configFile !== undefined ? { configFile: options.configFile } : {}), |
1079 | 1041 | }).catch((error: unknown) => { |
1080 | 1042 | manifestPromise = null; |
|