-
Notifications
You must be signed in to change notification settings - Fork 2
/
update-library-info.js
65 lines (53 loc) · 1.64 KB
/
update-library-info.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
56
57
58
59
60
61
const checker = require("license-checker");
const fs = require("fs");
const path = require("path");
// Primary ordering
const ordered = ["vue@", "vue-", "aurelia", "core-js"].reverse();
const excluded = ["openhab-remote"];
const baseDir = __dirname;
const targetFile = path.join(baseDir, "src/", "libraries.json");
async function getLicenses(startDir) {
const options = {
start: startDir,
customFormat: {
"licenses": true,
"name": true,
"version": true,
"description": "",
"copyright": "",
"repository": false,
"path": false,
"licenseFile": false,
"licenseText": false
},
production: true
};
return new Promise((resolve, reject) => {
checker.init(options, (err, packages) => {
if (err) {
reject(err);
} else {
resolve(packages);
}
});
});
}
function indexIn(arr, packageName) {
return arr.findIndex(entry => packageName.startsWith(entry)) + 1;
}
async function run() {
console.log(`Writing library information to ${targetFile}`);
const packages = await getLicenses(baseDir);
const sorted = Object.keys(packages)
.filter(name => !indexIn(excluded, name))
.sort((a, b) => {
const primary = indexIn(ordered, b) - indexIn(ordered, a);
return primary || a.localeCompare(b);
})
.map(name => packages[name]);
const content = JSON.stringify(sorted, null, 4);
fs.writeFile(targetFile, content, null, err => {
if (err) throw err;
});
}
run();