-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
213 lines (190 loc) · 5.49 KB
/
index.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const fs = require("fs");
const path = require("path");
const parseSPDX = require("spdx-expression-parse");
/**
* https://github.com/jonschlinkert/parse-author and
* https://github.com/jonschlinkert/author-regex
* MIT © Jon Schlinkert
*/
function parseAuthor(str) {
if (typeof str === "object") return str;
if (!str || !/\w/.test(str)) return {};
const authorRegex = /^([^<(]+?)?[ \t]*(?:<([^>(]+?)>)?[ \t]*(?:\(([^)]+?)\)|$)/gm;
const match = [].concat.apply([], authorRegex.exec(str));
const author = {};
if (match[1]) {
author.name = match[1];
}
for (var i = 2; i < match.length; i++) {
var val = match[i];
if (i % 2 === 0 && val && match[i + 1]) {
if (val.charAt(0) === "<") {
author.email = match[i + 1];
i++;
} else if (val.charAt(0) === "(") {
author.url = match[i + 1];
i++;
}
}
}
return author;
}
function equalityMap() {
const m = new Map();
return function(val) {
const str = JSON.stringify(val);
if (m.has(str)) {
return m.get(str);
}
m.set(str, val);
return val;
};
}
function flattenLicense(root) {
if (root.left) {
return [...flattenLicense(root.left), ...flattenLicense(root.right)];
} else if (root.license) {
return [root.license];
}
}
function arrayToSentence(arr) {
const separator = ", ";
const lastSeparator = " and ";
if (arr.length === 0) {
return "";
}
if (arr.length === 1) {
return arr[0];
}
return arr.slice(0, -1).join(separator) + lastSeparator + arr[arr.length - 1];
}
// [modules...] => { Tom Selleck => [], John Walsh => [] }
// Priority:
//
// - author field
// - first maintainer
// - ?
function groupByAuthor(modules) {
let groups = {};
for (let module of modules) {
let authors = [];
if (module.author) {
authors = [parseAuthor(module.author)];
} else if (module.authors) {
authors = module.authors.map(parseAuthor);
} else if (module.maintainers) {
authors = module.maintainers.map(parseAuthor);
} else if (module.licenseText) {
let match = module.licenseText.match(
/Copyright (?:\(c\))?(?:©)?\s*(?:[\-\d]*(?:present)?,?)?\s*(.*)/i
);
if (match) {
authors = [parseAuthor(match[1])];
}
}
let authorString = arrayToSentence(
authors.map(a => a.name).filter(Boolean)
);
if (!groups[authorString]) groups[authorString] = [];
groups[authorString].push(module.name);
}
return Object.entries(groups)
.map(([author, modules]) => ({
author,
modules: modules.sort()
}))
.sort((a, b) => a.author.localeCompare(b.author));
}
module.exports = ({ whitelist } = {}) => {
const cache = new Map();
const dependencies = new Map();
const whitelistSet = new Set(whitelist);
const cwd = process.cwd();
return {
name: "rollup-plugin-credits",
load(id) {
let dir = path.parse(id).dir;
let pkg = null;
const scannedDirs = [];
while (dir && dir !== cwd) {
if (cache.has(dir)) {
return;
}
scannedDirs.push(dir);
const pkgPath = path.join(dir, "package.json");
if (fs.existsSync(pkgPath)) {
pkg = require(pkgPath);
if (pkg.private) return;
for (let licenseVariation of [
"LICENSE",
"license",
"LICENSE.md",
"LICENSE.txt",
"license.md",
"license.txt"
]) {
const licensePath = path.join(dir, licenseVariation);
if (fs.existsSync(licensePath)) {
pkg.licenseText = fs.readFileSync(licensePath, "utf8");
break;
}
}
dependencies.set(pkg.name, pkg);
break;
}
dir = path.normalize(path.join(dir, ".."));
}
scannedDirs.forEach(scannedDir => {
cache.set(scannedDir, pkg);
});
},
renderChunk() {
// Step 1: transform flat list of dependency into {license} => [{package}...]
// Map
const licenseGroups = new Map();
// I'm being a little fancy here, and I want a map of license objects
// to packages. So I keep a secondary map of stringified JSON
// to single object instances.
let licenseObjects = equalityMap();
for (let [name, dependency] of dependencies) {
if (!dependency.license) {
continue;
}
let parsedLicense;
try {
parsedLicense = licenseObjects(parseSPDX(dependency.license));
} catch (e) {
console.log(
`Could not parse license of ${name} (${dependency.license})`
);
}
if (parsedLicense) {
if (whitelist) {
for (let license of flattenLicense(parsedLicense)) {
if (!whitelistSet.has(license)) {
throw new Error(
`Non-whitelisted license detected in ${name}: ${license}`
);
}
}
}
let existing = licenseGroups.get(parsedLicense);
licenseGroups.set(
parsedLicense,
existing ? existing.concat(dependency) : [dependency]
);
}
}
const output = [];
for (let [license, modules] of licenseGroups) {
output.push({
license,
modules: groupByAuthor(modules)
});
}
output.sort((a, b) => a.license.license > b.license.license);
return "export default " + JSON.stringify(output, null, 2);
}
};
};
module.exports.flattenLicense = flattenLicense;