-
Notifications
You must be signed in to change notification settings - Fork 52
/
getLicenses.mjs
60 lines (56 loc) · 2.04 KB
/
getLicenses.mjs
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
56
57
58
59
60
import fs from "fs";
import fetch from "node-fetch";
import path from "path";
let licenses = [];
let packageLock = JSON.parse(fs.readFileSync("package-lock.json"));
Object.keys(packageLock.packages).forEach(async (modulePath) => {
let moduleName = modulePath === "" ? "AdvantageScope" : modulePath.replaceAll("node_modules/", "");
if (modulePath !== "" && !fs.existsSync(modulePath)) {
// Module not installed
return;
}
let licenseFiles = fs
.readdirSync(modulePath === "" ? "." : modulePath)
.filter(
(filename) =>
filename.toLowerCase().startsWith("license") && !filename.endsWith(".js") && !filename.endsWith(".json")
);
let licenseText = null;
if (licenseFiles.length > 0) {
// Get license text from local files
licenseText = licenseFiles.map((filename) => fs.readFileSync(path.join(modulePath, filename))).join("\n");
} else if (fs.existsSync(path.join(modulePath, "package.json"))) {
// Read from package.json
let packageJson = JSON.parse(fs.readFileSync(path.join(modulePath, "package.json")));
let request = await fetch(
"https://raw.githubusercontent.com/spdx/license-list-data/main/json/details/" +
encodeURIComponent(packageJson.license) +
".json"
);
if (!request.ok) {
console.error('Failed to get license for "' + moduleName + '"');
return;
}
let spdxLicense = await request.json();
licenseText = spdxLicense.licenseText;
}
if (licenseText !== null) {
licenses.push({
module: moduleName,
text: licenseText
});
}
});
// Save JSON version
fs.writeFileSync("src/licenses.json", JSON.stringify(licenses));
// Save text version
let fullText = "";
licenses.forEach((license, index) => {
if (index === 0) return; // AdvantageScope license already included
if (index > 1) fullText += "\n";
fullText += "---------- " + license.module + " ----------\n\n";
fullText += license.text;
});
fs.writeFileSync("ThirdPartyLicenses.txt", fullText);
// Print status
console.log("Saved " + licenses.length.toString() + " licenses");