66import fs from 'node:fs' ;
77import path from 'node:path' ;
88
9- import { XMLParser } from 'fast-xml-parser ' ;
10-
9+ import { selectTrustifyDABackend } from '../index.js ' ;
10+ import { matchForLicense , availableProviders } from '../provider.js' ;
1111import { getCustom , getTokenHeaders } from '../tools.js' ;
1212
1313const LICENSE_FILES = [ 'LICENSE' , 'LICENSE.md' , 'LICENSE.txt' ] ;
1414
15- /**
16- * Resolve project license from the manifest file only (no LICENSE file).
17- * @param {string } manifestPath - path to manifest (e.g. package.json, pom.xml)
18- * @param {{} } [opts={}] - options (for getCustom, etc.)
19- * @returns {{ fromManifest: string|null, fromFile: null, mismatch: false } }
20- */
21- export function getProjectLicenseFromManifest ( manifestPath , opts = { } ) {
22- const fromManifest = readLicenseFromManifest ( manifestPath , opts ) ;
23- return {
24- fromManifest : fromManifest || null ,
25- fromFile : null ,
26- mismatch : false
27- } ;
28- }
29-
3015/**
3116 * Resolve project license from manifest and from LICENSE / LICENSE.md in manifest dir or git root.
3217 * Uses local pattern matching for LICENSE file identification (synchronous).
@@ -35,8 +20,10 @@ export function getProjectLicenseFromManifest(manifestPath, opts = {}) {
3520 * @returns {{ fromManifest: string|null, fromFile: string|null, mismatch: boolean } }
3621 */
3722export function getProjectLicense ( manifestPath ) {
38- const fromManifest = readLicenseFromManifest ( manifestPath ) ;
39- const fromFile = readLicenseFromFile ( manifestPath ) ;
23+ const resolved = path . resolve ( manifestPath ) ;
24+ const provider = matchForLicense ( resolved , availableProviders ) ;
25+ const fromManifest = provider . readLicenseFromManifest ( resolved ) ;
26+ const fromFile = readLicenseFromFile ( resolved ) ;
4027 const mismatch = Boolean (
4128 fromManifest && fromFile && normalizeSpdx ( fromManifest ) !== normalizeSpdx ( fromFile )
4229 ) ;
@@ -47,71 +34,6 @@ export function getProjectLicense(manifestPath) {
4734 } ;
4835}
4936
50- /**
51- * Read license from manifest (package.json, pom.xml). Returns null if not present or unsupported manifest.
52- * @param {string } manifestPath
53- * @returns {string|null }
54- */
55- function readLicenseFromManifest ( manifestPath ) {
56- const base = path . basename ( manifestPath ) ;
57- if ( base === 'package.json' ) {
58- return readLicenseFromPackageJson ( manifestPath ) ;
59- }
60- if ( base === 'pom.xml' ) {
61- return readLicenseFromPomXml ( manifestPath ) ;
62- }
63- // build.gradle, go.mod, requirements.txt: no standard license field
64- return null ;
65- }
66-
67- /**
68- * @param {string } manifestPath
69- * @returns {string|null }
70- */
71- function readLicenseFromPackageJson ( manifestPath ) {
72- try {
73- const content = JSON . parse ( fs . readFileSync ( manifestPath , 'utf-8' ) ) ;
74- if ( typeof content . license === 'string' ) {
75- return content . license . trim ( ) || null ;
76- }
77- if ( Array . isArray ( content . licenses ) && content . licenses . length > 0 ) {
78- const first = content . licenses [ 0 ] ;
79- const name = first . type || first . name ;
80- return typeof name === 'string' ? name . trim ( ) : null ;
81- }
82- return null ;
83- } catch {
84- return null ;
85- }
86- }
87-
88- /**
89- * @param {string } manifestPath
90- * @returns {string|null }
91- */
92- function readLicenseFromPomXml ( manifestPath ) {
93- try {
94- const xml = fs . readFileSync ( manifestPath , 'utf-8' ) ;
95- const parser = new XMLParser ( { ignoreAttributes : false } ) ;
96- const obj = parser . parse ( xml ) ;
97- const project = obj ?. project ;
98- if ( ! project ?. licenses ?. license ) {
99- return null ;
100- }
101- const license = Array . isArray ( project . licenses . license )
102- ? project . licenses . license [ 0 ]
103- : project . licenses . license ;
104- const name = ( license ?. name && license . name . trim ( ) ) || null ;
105- if ( ! name ) {
106- return null ;
107- }
108-
109- return name ;
110- } catch {
111- return null ;
112- }
113- }
114-
11537/**
11638 * Find LICENSE file path in the same directory as the manifest.
11739 * @param {string } manifestPath
@@ -136,14 +58,14 @@ export function findLicenseFilePath(manifestPath) {
13658/**
13759 * Call backend /licenses/identify endpoint to identify license from file.
13860 * @param {string } licenseFilePath - path to LICENSE file
139- * @param {string } backendUrl - backend base URL
14061 * @param {{} } [opts={}] - options (proxy, token, etc.)
14162 * @returns {Promise<string|null> } - SPDX identifier or null
14263 */
143- export async function identifyLicense ( licenseFilePath , backendUrl , opts = { } ) {
64+ export async function identifyLicense ( licenseFilePath , opts = { } ) {
14465 try {
14566 const fileContent = fs . readFileSync ( licenseFilePath ) ;
146- const url = `${ backendUrl . replace ( / \/ $ / , '' ) } /licenses/identify` ;
67+ const backendUrl = selectTrustifyDABackend ( opts ) ;
68+ const url = new URL ( `${ backendUrl } /licenses/identify` ) ;
14769 const tokenHeaders = getTokenHeaders ( opts ) ;
14870 const fetchOptions = {
14971 method : 'POST' ,
0 commit comments