Skip to content

Commit ad162f1

Browse files
authored
deprecated licenses detection from package.json (#309)
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent 72acf5f commit ad162f1

File tree

5 files changed

+47
-14
lines changed

5 files changed

+47
-14
lines changed

HISTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
## unreleased
66

7+
* Added
8+
* Detection for node-package manifests deprecated licenses format in the node-specific builders ([#308] via [#309])
9+
10+
[#308]: https://github.com/CycloneDX/cyclonedx-javascript-library/issues/308
11+
[#309]: https://github.com/CycloneDX/cyclonedx-javascript-library/pull/309
12+
713
## 1.7.0 - 2022-10-25
814

915
* Changed

src/_helpers/packageJson.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ export interface PackageJson {
3232
version?: string
3333
description?: string
3434
license?: string
35+
licenses?: Array<{
36+
type?: string
37+
url?: string
38+
}>
3539
author?: string | {
3640
name?: string
3741
email?: string

src/builders/fromNodePackageJson.node.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,30 +91,43 @@ export class ComponentBuilder {
9191
: (typeof data.author?.name === 'string'
9292
? data.author.name
9393
: undefined)
94+
9495
/** @see {@link https://docs.npmjs.com/cli/v8/configuring-npm/package-json#description-1} */
9596
const description = typeof data.description === 'string'
9697
? data.description
9798
: undefined
99+
98100
/** @see {@link https://docs.npmjs.com/cli/v8/configuring-npm/package-json#version} */
99101
const version = typeof data.version === 'string'
100102
? data.version
101103
: undefined
104+
102105
const externalReferences = this.#extRefFactory.makeExternalReferences(data)
103-
/** @see {@link https://docs.npmjs.com/cli/v8/configuring-npm/package-json#license} */
104-
const license = typeof data.license === 'string'
105-
? this.#licenseFactory.makeFromString(data.license)
106-
: undefined
106+
107+
const licenses = new Models.LicenseRepository()
108+
if (typeof data.license === 'string') {
109+
/** @see {@link https://docs.npmjs.com/cli/v8/configuring-npm/package-json#license} */
110+
licenses.add(this.#licenseFactory.makeFromString(data.license))
111+
}
112+
if (Array.isArray(data.licenses)) {
113+
/** @see {@link https://github.com/SchemaStore/schemastore/blob/master/src/schemas/json/package.json} */
114+
for (const licenseData of data.licenses) {
115+
if (typeof licenseData?.type === 'string') {
116+
const license = this.#licenseFactory.makeDisjunctive(licenseData.type)
117+
license.url = typeof licenseData.url === 'string'
118+
? licenseData.url
119+
: undefined
120+
licenses.add(license)
121+
}
122+
}
123+
}
107124

108125
return new Models.Component(type, name, {
109126
author,
110127
description,
111128
externalReferences: new Models.ExternalReferenceRepository(externalReferences),
112129
group,
113-
licenses: new Models.LicenseRepository(
114-
license === undefined
115-
? []
116-
: [license]
117-
),
130+
licenses,
118131
version
119132
})
120133
}

tests/integration/Builders.FromNodePackageJson.ComponentBuilder.test.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ suite('Builders.FromNodePackageJson.ComponentBuilder', () => {
3434
const extRefFactory = new Factories.FromNodePackageJson.ExternalReferenceFactory()
3535
extRefFactory.makeExternalReferences = () => [`FAKE REFERENCES ${salt}`]
3636
const licenseFactory = new Factories.LicenseFactory()
37-
licenseFactory.makeFromString = () => `FAKE LICENSE ${salt}`
37+
licenseFactory.makeFromString = (s) => ({ name: `FAKE LICENSE: ${s}` })
38+
licenseFactory.makeDisjunctive = (s) => ({ name: `FAKE DISJUNCTIVE LICENSE: ${s}` })
3839

3940
const sut = new ComponentBuilder(extRefFactory, licenseFactory)
4041

@@ -44,9 +45,15 @@ suite('Builders.FromNodePackageJson.ComponentBuilder', () => {
4445
description: `dummy lib ${salt}`,
4546
author: {
4647
name: 'Jane Doe',
47-
url: 'http://acme.org/~jd'
48+
url: 'https://acme.org/~jd'
4849
},
49-
license: 'dummy license'
50+
license: `dummy license ${salt}`,
51+
licenses: [
52+
{
53+
type: `some license ${salt}`,
54+
url: `https://acme.org/license/${salt}`
55+
}
56+
]
5057
// to be continued
5158
}
5259
const expected = new Models.Component(
@@ -56,7 +63,10 @@ suite('Builders.FromNodePackageJson.ComponentBuilder', () => {
5663
author: 'Jane Doe',
5764
description: `dummy lib ${salt}`,
5865
externalReferences: new Models.ExternalReferenceRepository([`FAKE REFERENCES ${salt}`]),
59-
licenses: new Models.LicenseRepository([`FAKE LICENSE ${salt}`]),
66+
licenses: new Models.LicenseRepository([
67+
{ name: `FAKE LICENSE: dummy license ${salt}` },
68+
{ name: `FAKE DISJUNCTIVE LICENSE: some license ${salt}`, url: `https://acme.org/license/${salt}` }
69+
]),
6070
group: '@foo',
6171
version: `1.33.7-alpha.23.${salt}`
6272
}

tests/unit/Builders.FromNodePackageJson.ComponentBuilder.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const {
2929
}
3030
} = require('../../')
3131

32-
suite('Builders.FromNodePackageJson.ToolBuilder', () => {
32+
suite('Builders.FromNodePackageJson.ComponentBuilder', () => {
3333
test('construct', () => {
3434
const extRefFactory = new ExternalReferenceFactory()
3535
const licenseFactory = new LicenseFactory()

0 commit comments

Comments
 (0)