-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathindex.js
106 lines (90 loc) · 2.8 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
import path from 'path'
import gh from 'gh-got'
import readmeFilename from 'readme-filename'
import replace from 'replace-in-file'
import mm from 'micromatch'
function getRepo(baton) {
var repo = baton.repo
if (!repo) {
const pkg = require(path.resolve(baton.dir, 'package.json'))
repo = pkg.repository && (pkg.repository.url || pkg.repository)
}
if (!repo) {
throw new Error(
`${path.join(baton.dir, 'package.json')}: repository is not set.`
)
}
baton.repo = repo.replace(/https?:\/\/[^\/]+\//, '').replace('.git', '')
return baton
}
function fetch(baton) {
const searchParams = {
['per_page']: baton.limit
}
return gh(`repos/${baton.repo}/contributors`, { searchParams }).then(res => {
baton.contributors = res.body
return baton
})
}
function filter(baton) {
if (!baton.exclude) return baton
const isExcluded = mm.matcher(baton.exclude)
baton.contributors = baton.contributors.filter(
contrib => !isExcluded(contrib.login)
)
return baton
}
function html(baton) {
baton.html = baton.contributors.reduce((html, contributor) => {
/* eslint-disable */
const line = `
<a href="${contributor.html_url}">
<img src="${contributor.avatar_url}" title="${contributor.login}" width="80" height="80">
</a>`
/* eslint-enable */
html += line.replace(/\n/gm, '').replace(/\s{2,}/g, '') + '\n'
return html
}, '')
return baton
}
function update(baton) {
return readmeFilename(baton.dir).then(
filename =>
new Promise((resolve, reject) => {
replace(
{
files: path.resolve(baton.dir, filename),
// eslint-disable-next-line
from: /\[\/\/\]: contributor-faces(?:(?:\n.*)+\[\/\/\]: contributor-faces)?/,
to: `[//]: contributor-faces\n${baton.html}\n[//]: contributor-faces`
},
err => {
/* istanbul ignore if */
if (err) return reject(err)
baton.filename = filename
resolve(baton)
}
)
})
)
}
function end(prop) {
return baton => baton[prop]
}
function core(dir, opts) {
opts = opts || {}
opts.dir = dir || '.'
opts.limit = opts.limit || 30
return Promise.resolve(opts).then(getRepo).then(fetch).then(filter)
}
/* ────────────────────────────────────────────────────────────────────────── */
function contributors(dir, opts) {
return core(dir, opts).then(end('contributors'))
}
contributors.html = function (dir, opts) {
return core(dir, opts).then(html).then(end('html'))
}
contributors.update = function (dir, opts) {
return core(dir, opts).then(html).then(update).then(end('filename'))
}
export default contributors