Skip to content

Commit ea5997c

Browse files
committedMar 9, 2024
feat: include notice file in third party output
1 parent c6c059f commit ea5997c

File tree

10 files changed

+148
-20
lines changed

10 files changed

+148
-20
lines changed
 

‎src/dependency.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export class Dependency {
4646
this.private = pkg.private || false;
4747
this.license = pkg.license || null;
4848
this.licenseText = pkg.licenseText || null;
49+
this.noticeText = pkg.noticeText || null;
4950

5051
// Parse the author field to get an object.
5152
this.author = pkg.author ? new Person(pkg.author) : null;
@@ -105,8 +106,17 @@ export class Dependency {
105106
lines.push('===');
106107
lines.push('');
107108
lines.push(this.licenseText);
109+
lines.push('');
110+
}
111+
112+
if (this.noticeText) {
113+
lines.push('Notice:');
114+
lines.push('===');
115+
lines.push('');
116+
lines.push(this.noticeText);
117+
lines.push('');
108118
}
109119

110-
return lines.join(EOL);
120+
return lines.join(EOL).trim();
111121
}
112122
}

‎src/index.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ export interface Dependency {
168168
*/
169169
readonly licenseText: string | null;
170170

171+
/**
172+
* Full notice file text.
173+
*/
174+
readonly noticeText: string | null;
175+
171176
/**
172177
* Author information.
173178
*/

‎src/license-plugin.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,19 @@ class LicensePlugin {
194194
// We found it!
195195
pkg = pkgJson;
196196

197-
// Read license file, if it exists.
197+
// Read license & notice files, if it exists.
198198
const cwd = this._cwd || process.cwd();
199+
199200
const licenseText = readFile(dir, cwd, ['license', 'licence']);
200201
if (licenseText) {
201202
pkg.licenseText = licenseText;
202203
}
203204

205+
const noticeText = readFile(dir, cwd, 'notice');
206+
if (noticeText) {
207+
pkg.noticeText = noticeText;
208+
}
209+
204210
// Add the new dependency to the set of third-party dependencies.
205211
this.addDependency(pkg);
206212

‎test/base.spec.js

+28-17
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,32 @@ import _ from 'lodash';
2626
import {Dependency} from '../src/dependency.js';
2727
import {Person} from '../src/person.js';
2828

29+
/**
30+
* Normalize input dependency object, so it can be safely compared with another
31+
* normalized dependency object.
32+
*
33+
* @param {Dependency|object} input Input.
34+
* @returns {object} Normalized output.
35+
*/
36+
function normalizeDependencyObject(input) {
37+
const output = _.toPlainObject(input);
38+
39+
if (output.author) {
40+
output.author = _.toPlainObject(output.author);
41+
}
42+
if (output.contributors) {
43+
output.contributors = output.contributors.map((o) => _.toPlainObject(o));
44+
}
45+
if (output.licenseText) {
46+
output.licenseText = output.licenseText.trim();
47+
}
48+
if (output.noticeText) {
49+
output.noticeText = output.noticeText.trim();
50+
}
51+
52+
return output;
53+
}
54+
2955
beforeEach(() => {
3056
jasmine.addCustomEqualityTester((first, second) => {
3157
if ((first instanceof Person) || (second instanceof Person)) {
@@ -37,23 +63,8 @@ beforeEach(() => {
3763

3864
jasmine.addCustomEqualityTester((first, second) => {
3965
if ((first instanceof Dependency) || (second instanceof Dependency)) {
40-
const o1 = _.toPlainObject(first);
41-
const o2 = _.toPlainObject(second);
42-
43-
if (o1.author) {
44-
o1.author = _.toPlainObject(o1.author);
45-
}
46-
if (o1.contributors) {
47-
o1.contributors = o1.contributors.map((o) => _.toPlainObject(o));
48-
}
49-
50-
if (o2.author) {
51-
o2.author = _.toPlainObject(o2.author);
52-
}
53-
if (o2.contributors) {
54-
o2.contributors = o2.contributors.map((o) => _.toPlainObject(o));
55-
}
56-
66+
const o1 = normalizeDependencyObject(first);
67+
const o2 = normalizeDependencyObject(second);
5768
return _.isEqual(o1, o2);
5869
}
5970
});

‎test/dependency.spec.js

+33
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ describe('Dependency', () => {
5959
version: '1.0.0',
6060
license: 'MIT',
6161
licenseText: null,
62+
noticeText: null,
6263
description: 'Desc',
6364
private: false,
6465
homepage: 'https://github.com/mjeanroy',
@@ -345,4 +346,36 @@ describe('Dependency', () => {
345346
`The MIT License (MIT) -- Copyright (c) Mickael Jeanroy`,
346347
]));
347348
});
349+
350+
it('should format dependency with notice text', () => {
351+
const pkg = {
352+
name: 'foo',
353+
version: '1.0.0',
354+
license: 'MIT',
355+
licenseText: 'The MIT License (MIT) -- Copyright (c) Mickael Jeanroy',
356+
noticeText: 'Software libraries under third_party',
357+
description: 'Desc',
358+
homepage: 'https://github.com/mjeanroy',
359+
};
360+
361+
const dependency = new Dependency(pkg);
362+
363+
expect(dependency.text()).toEqual(join([
364+
`Name: ${pkg.name}`,
365+
`Version: ${pkg.version}`,
366+
`License: ${pkg.license}`,
367+
`Private: false`,
368+
`Description: ${pkg.description}`,
369+
`Homepage: ${pkg.homepage}`,
370+
`License Copyright:`,
371+
`===`,
372+
``,
373+
`The MIT License (MIT) -- Copyright (c) Mickael Jeanroy`,
374+
``,
375+
`Notice:`,
376+
`===`,
377+
``,
378+
`Software libraries under third_party`,
379+
]));
380+
});
348381
});
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2016-2023 Mickael Jeanroy
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
console.log('fake-package');
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
license.md file
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
notice.md file
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "fake-package",
3+
"version": "1.0.0",
4+
"description": "Fake package used in unit tests",
5+
"main": "src/index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "Mickael Jeanroy <mickael.jeanroy@gmail.com>",
10+
"license": "MIT",
11+
"private": true,
12+
"dependencies": {
13+
"lodash": "*"
14+
}
15+
}

‎test/license-plugin.spec.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import tmp from 'tmp';
2828
import moment from 'moment';
2929
import {licensePlugin} from '../src/license-plugin.js';
3030
import {join} from './utils/join.js';
31-
import {pkg} from '../scripts/config';
3231

3332
describe('LicensePlugin', () => {
3433
let tmpDir;
@@ -110,6 +109,7 @@ describe('LicensePlugin', () => {
110109
description: 'Fake package used in unit tests',
111110
license: 'MIT',
112111
licenseText: null,
112+
noticeText: null,
113113
private: true,
114114
homepage: null,
115115
repository: null,
@@ -190,6 +190,7 @@ describe('LicensePlugin', () => {
190190
private: true,
191191
license: 'MIT',
192192
licenseText: null,
193+
noticeText: null,
193194
author: null,
194195
contributors: [],
195196
},
@@ -238,6 +239,21 @@ describe('LicensePlugin', () => {
238239
});
239240
});
240241

242+
it('should load pkg and find notice file', () => {
243+
const id = path.join(__dirname, 'fixtures', 'fake-package-11', 'src', 'index.js');
244+
245+
plugin.scanDependency(id);
246+
247+
expect(addDependency).toHaveBeenCalled();
248+
expect(plugin._dependencies).toEqual({
249+
'fake-package': {
250+
...fakePackage,
251+
licenseText: 'license.md file',
252+
noticeText: 'notice.md file',
253+
},
254+
});
255+
});
256+
241257
it('should load pkg including license text from license.md file ignoring case of license file', () => {
242258
const id = path.join(__dirname, 'fixtures', 'fake-package-8', 'src', 'index.js');
243259

@@ -415,6 +431,7 @@ describe('LicensePlugin', () => {
415431
description: 'Fake Description',
416432
license: 'MIT',
417433
licenseText: null,
434+
noticeText: null,
418435
homepage: 'https://www.google.fr',
419436
private: true,
420437
maintainers: [],
@@ -1205,6 +1222,7 @@ describe('LicensePlugin', () => {
12051222
description: 'Foo Package',
12061223
license: 'MIT',
12071224
licenseText: null,
1225+
noticeText: null,
12081226
private: false,
12091227
homepage: null,
12101228
repository: null,
@@ -1242,6 +1260,7 @@ describe('LicensePlugin', () => {
12421260
description: 'Foo Package',
12431261
license: 'MIT',
12441262
licenseText: null,
1263+
noticeText: null,
12451264
private: false,
12461265
homepage: null,
12471266
repository: null,
@@ -1281,6 +1300,7 @@ describe('LicensePlugin', () => {
12811300
description: 'Foo Package',
12821301
license: 'MIT',
12831302
licenseText: null,
1303+
noticeText: null,
12841304
private: false,
12851305
maintainers: [],
12861306
contributors: [],
@@ -1298,6 +1318,7 @@ describe('LicensePlugin', () => {
12981318
description: 'Bar Package',
12991319
license: 'Apache 2.0',
13001320
licenseText: null,
1321+
noticeText: null,
13011322
maintainers: [],
13021323
contributors: [],
13031324
author: null,

0 commit comments

Comments
 (0)
Please sign in to comment.