This repository has been archived by the owner on Aug 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
workbox:generate-contributors.js
94 lines (84 loc) · 2.42 KB
/
workbox:generate-contributors.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
const GitHubApi = require('github');
const fs = require('fs-extra');
const path = require('path');
const gulp = require('gulp');
const MAX_COLLABORATORS = 20;
function getContributorHTML(contributorStats) {
const contributor = contributorStats.author;
let html = `
<div class="wb-contrib">
<a class="wb-contrib__img-link" href="${contributor.url}" target="_blank">
<img class="wb-contrib__image" src="${contributor.avatar_url}">
</a>
<br />
<a class="wb-contrib__link" href="${contributor.url}" target="_blank">
${contributor.login}
</a>
</div>
`;
return html;
}
gulp.task(`workbox:generate-contributors`, function() {
if (!process.env.GITHUB_TOKEN) {
console.warn(`Unable to build Workbox contributors due to no ` +
`GITHUB_TOKEN existing on the current Path.`);
// Return a promise so gulp is happy and thinks the task completed
return Promise.resolve();
}
const github = new GitHubApi();
github.authenticate({
type: 'token',
token: process.env.GITHUB_TOKEN
});
return github.repos.getStatsContributors({
owner: 'googlechrome',
repo: 'workbox',
})
.then((contributorStats) => {
contributorStats.sort((a, b) => b.total - a.total);
if (contributorStats.length > MAX_COLLABORATORS) {
contributorStats = contributorStats.splice(0, MAX_COLLABORATORS);
}
return contributorStats.map(getContributorHTML).join('');
})
.then((html) => {
const finalHtml = `{% framebox height="auto" %}
<!-- This file is auto-generated by 'gulp workbox:generate-contributors' -->
<style>
.workbox-contributors-container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-around;
}
.wb-contrib {
text-align: center;
margin: 10px;
}
.wb-contrib a {
color: inherit;
}
.wb-contrib__image {
width: 80px;
height: 80px;
border-radius: 100%;
box-shadow: 0 4px 6px #616161;
background-color: white;
}
</style>
<div class="workbox-contributors-container">
${html}
</div>
{% endframebox %}`;
const templatePath = path.join(__dirname, '..', 'src', 'content',
'en', 'tools', 'workbox', 'templates', 'contributors.html');
return fs.ensureDir(path.dirname(templatePath))
.then(() => fs.writeFile(templatePath, finalHtml))
.catch((err) => {
console.warn(`An error occured when generating the Workbox ` +
`collaborators.`);
console.warn(`This is not a vital error.`);
console.warn(err);
});
});
});