This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
createAttributionJSON.js
100 lines (80 loc) · 2.77 KB
/
createAttributionJSON.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
const { execSync } = require("child_process");
const path = require("path");
const fs = require("fs");
const crypto = require('crypto');
/**
Updates the JSON file `attribution.json` with contributors based on commits to files, to run:
node createAttributionJSON.js
*/
const handleDupeNames = (name) => {
if (name === "Orta") return "Orta Therox"
return name
}
// Being first gets you a free x commits
const getOriginalAuthor = filepath => {
const creator = execSync(`git log --format='%an | %aE' --diff-filter=A -- "${filepath}"`)
.toString()
.trim();
return {
name: creator.split(" | ")[0],
email: creator.split(" | ")[1]
};
};
// Gets the rest of the authors for a file
const getAuthorsForFile = filepath => {
const cmd = `git log --format='%an | %aE' -- "${filepath}"`
const contributors = execSync(cmd).toString().trim()
const allContributions = contributors.split("\n").map(c => {
return {
name: handleDupeNames(c.split(" | ")[0]),
email: c.split(" | ")[1]
};
});
// Keep a map of all found authors,
const objs = new Map()
allContributions.forEach(c => {
const id = c.name.toLowerCase().replace(/\s/g, "")
const existing = objs.get(id)
if (existing) {
objs.set(id, { name: c.name, gravatar: existing.gravatar, count: existing.count + 1 })
} else {
const email = c.email || "NOOP"
objs.set(id, { name: c.name, gravatar: crypto.createHash('md5').update(email).digest('hex'), count: 1 })
}
})
return [...objs.values()]
};
const allFiles = recursiveReadDirSync("pages")
// const allFiles = ["pages/JSDoc Supported Types.md"];
const json = {}
allFiles.forEach(f => {
const first = getOriginalAuthor(f);
const rest = getAuthorsForFile(f)
const firstInRest = rest.find(a => a.name === first.name)
firstInRest.count += 50
rest.sort((l, r) => r.count - l.count)
console.log(" - " + f + " (" + rest.length + ")")
json[f] = { top: rest.slice(0, 5), total: rest.length }
});
fs.writeFileSync("attribution.json", JSON.stringify(json))
/** Recursively retrieve file paths from a given folder and its subfolders. */
// https://gist.github.com/kethinov/6658166#gistcomment-2936675
/** @returns {string[]} */
function recursiveReadDirSync(folderPath) {
if (!fs.existsSync(folderPath)) return []
const entryPaths = fs
.readdirSync(folderPath)
.map(entry => path.join(folderPath, entry))
const filePaths = entryPaths.filter(entryPath =>
fs.statSync(entryPath).isFile()
)
const dirPaths = entryPaths.filter(
entryPath => !filePaths.includes(entryPath)
)
const dirFiles = dirPaths.reduce(
(prev, curr) => prev.concat(recursiveReadDirSync(curr)),
[]
)
return [...filePaths, ...dirFiles]
.filter(f => !f.endsWith(".DS_Store") && !f.endsWith("README.md"))
}