-
Notifications
You must be signed in to change notification settings - Fork 32
/
generate-licenses.js
55 lines (55 loc) · 1.71 KB
/
generate-licenses.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const generateLicenses = function generateLicenses(file) {
const checker = require('license-checker');
const fs = require('fs');
const path = require('path');
function isMarkDown(filepath) {
const filename = path.basename(filepath).toUpperCase();
return filename === 'README.MD' || filename === 'README.MARKDOWN';
}
function copyLicense(item) {
if (item.licenseFile && !isMarkDown(item.licenseFile)) {
const licenseFile = fs.readFileSync(`${item.licenseFile}`, 'utf8');
fs.appendFileSync(file, `\n\n${item.module} (${item.repository})\n`);
fs.appendFileSync(file, '=========================================\n');
fs.appendFileSync(file, licenseFile);
}
}
function printLicenseDetails(licenses) {
fs.appendFileSync(file, 'THIRD-PARTY SOFTWARE NOTICES AND INFORMATION\n\n');
fs.appendFileSync(file, 'This project incorporates components from the projects listed below.\n');
Object.keys(licenses).forEach((key) => {
licenses[key].forEach(copyLicense);
});
}
function copyLicenseToText(json) {
const licenses = Object.keys(json).map((key) => {
const obj = json[key];
obj.module = key;
return obj;
})
.filter((item) => !isMarkDown(item.licenseFile))
.reduce(
(r, v) => {
(r[v.licenses] || (r[v.licenses] = [])).push(v);
return r;
},
{},
);
printLicenseDetails(licenses);
}
checker.init(
{
start: '.',
production: true,
excludePrivatePackages: true,
},
(err, json) => {
if (err) {
throw err;
} else {
copyLicenseToText(json); // The sorted json data
}
},
);
};
module.exports.generateLicenses = generateLicenses;