forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthors.ts
182 lines (159 loc) · 6.29 KB
/
authors.ts
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
import fs = require('fs');
import path = require('path');
import child_process = require("child_process");
type Author = {
displayNames: string[];
preferedName?: string;
emails: string[];
};
type AuthorMap = { [s: string]: Author };
type Command = {
(...arg: string[]): void;
description?: string;
};
const mailMapPath = path.resolve("../.mailmap");
const authorsPath = path.resolve("../AUTHORS.md");
function getKnownAuthors(): Author[] {
const segmentRegExp = /\s?([^<]+)\s+<([^>]+)>/g;
const preferedNameRegeExp = /\s?#\s?([^#]+)$/;
const knownAuthors: Author[] = [];
if (!fs.existsSync(mailMapPath)) {
throw new Error(`Could not load known users form .mailmap file at: ${mailMapPath}`);
}
const mailMap = fs.readFileSync(mailMapPath).toString();
for (const line of mailMap.split("\r\n")) {
const author: Author = { displayNames: [], emails: [] };
let match: RegExpMatchArray | null;
while (match = segmentRegExp.exec(line)) {
author.displayNames.push(match[1]);
author.emails.push(match[2]);
}
if (match = preferedNameRegeExp.exec(line)) {
author.preferedName = match[1];
}
if (!author.emails) continue;
knownAuthors.push(author);
if (line.indexOf("#") > 0 && !author.preferedName) {
throw new Error("Could not match prefered name for: " + line);
}
// console.log("===> line: " + line);
// console.log(JSON.stringify(author, undefined, 2));
}
return knownAuthors;
}
function getAuthorName(author: Author) {
return author.preferedName || author.displayNames[0];
}
function getKnownAuthorMaps() {
const knownAuthors = getKnownAuthors();
const authorsByName: AuthorMap = {};
const authorsByEmail: AuthorMap = {};
knownAuthors.forEach(author => {
author.displayNames.forEach(n => authorsByName[n] = author);
author.emails.forEach(e => authorsByEmail[e.toLocaleLowerCase()] = author);
});
return {
knownAuthors,
authorsByName,
authorsByEmail
};
}
function deduplicate<T>(array: T[]): T[] {
let result: T[] = []
if (array) {
for (const item of array) {
if (result.indexOf(item) < 0) {
result.push(item);
}
}
}
return result;
}
function log(s: string) {
console.log(` ${s}`);
}
function sortAuthors(a: string, b: string) {
if (a.charAt(0) === "@") a = a.substr(1);
if (b.charAt(0) === "@") b = b.substr(1);
if (a.toLocaleLowerCase() < b.toLocaleLowerCase()) {
return -1;
}
else {
return 1;
}
}
namespace Commands {
export const writeAuthors: Command = function () {
const output = deduplicate(getKnownAuthors().map(getAuthorName).filter(a => !!a)).sort(sortAuthors).join("\r\n* ");
fs.writeFileSync(authorsPath, "TypeScript is authored by:\r\n* " + output);
};
writeAuthors.description = "Write known authors to AUTHORS.md file.";
export const listKnownAuthors: Command = function () {
deduplicate(getKnownAuthors().map(getAuthorName)).filter(a => !!a).sort(sortAuthors).forEach(log);
};
listKnownAuthors.description = "List known authors as listed in .mailmap file.";
export const listAuthors: Command = function (...specs:string[]) {
const cmd = "git shortlog -se " + specs.join(" ");
console.log(cmd);
const outputRegExp = /\d+\s+([^<]+)<([^>]+)>/;
const tty = process.platform === 'win32' ? 'CON' : '/dev/tty';
const authors: { name: string, email: string, knownAuthor?: Author }[] = [];
child_process.exec(`${cmd} < ${tty}`, { cwd: path.resolve("../") }, function (error, stdout, stderr) {
if (error) {
console.log(stderr.toString());
}
else {
const output = stdout.toString();
const lines = output.split("\n");
lines.forEach(line => {
if (line) {
let match: RegExpExecArray | null;
if (match = outputRegExp.exec(line)) {
authors.push({ name: match[1], email: match[2] });
}
else {
throw new Error("Could not parse output: " + line);
}
}
});
const maps = getKnownAuthorMaps();
const lookupAuthor = function ({name, email}: { name: string, email: string }) {
return maps.authorsByEmail[email.toLocaleLowerCase()] || maps.authorsByName[name];
};
const knownAuthors = authors
.map(lookupAuthor)
.filter(a => !!a)
.map(getAuthorName);
const unknownAuthors = authors
.filter(a => !lookupAuthor(a))
.map(a => `${a.name} <${a.email}>`);
if (knownAuthors.length) {
console.log("\r\n");
console.log("Found known authors: ");
console.log("=====================");
deduplicate(knownAuthors).sort(sortAuthors).forEach(log);
}
if (unknownAuthors.length) {
console.log("\r\n");
console.log("Found unknown authors: ");
console.log("=====================");
deduplicate(unknownAuthors).sort(sortAuthors).forEach(log);
}
}
});
};
listAuthors.description = "List known and unknown authors for a given spec";
}
var args = process.argv.slice(2);
if (args.length < 1) {
console.log('Usage: node authors.js [command]');
console.log('List of commands: ');
Object.keys(Commands).forEach(k => console.log(` ${k}: ${(Commands as any)[k]['description']}`));
} else {
var cmd: Function = (Commands as any)[args[0]];
if (cmd === undefined) {
console.log('Unknown command ' + args[1]);
} else {
cmd.apply(undefined, args.slice(1));
}
}