/
generate-readme
executable file
·241 lines (209 loc) · 7.07 KB
/
generate-readme
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env node
/**
Dynamically generate a README with up-to-date info from all my public repos. This runs on a cron within a GitHub Workflow [here](https://github.com/joel-hamilton/joel-hamilton/blob/master/.github/workflows/generate-readme.yml).
Currently, this script is using custom tag info in the README of each repo to sort them into categories, make a summary blurb, and to add icons.
*/
const fs = require("fs");
const readline = require("readline");
const fetch = require("node-fetch");
const util = require("util");
const exec = util.promisify(require("child_process").exec);
class Generator {
constructor() {
this.repos = null;
this.readme = "";
}
async generate() {
this.repos = await this.getRepos();
this.readme = this.makeReadme();
const currentReadme = fs.readFileSync("README.md", "utf-8");
if (this.readme !== currentReadme) {
console.log("READMEs are different, updating.");
await this.commit();
} else {
console.log("No changes, all done.");
}
}
makeReadme() {
const d = new Date();
let readme = "# Joel Hamilton\n\n";
// readme += this.getIntro();
// readme += this.getTable();
readme += this.getGuide();
readme += this.getContributions();
readme += `<br><br><sub>This README was generated on ${d.getFullYear()}-${(
"" +
(d.getMonth() + 1)
).padStart(2, "0")}-${("" + d.getDate()).padStart(
2,
"0"
)} using GitHub Actions and [scripts/generate-readme](https://github.com/joel-hamilton/joel-hamilton/blob/master/scripts/generate-readme), with icons from [devicon](https://github.com/devicons/devicon/)</sub>`;
return readme;
}
async commit() {
fs.writeFileSync("README.md", this.readme);
const cmds = [
"git config user.name github-actions",
"git config user.email github-actions@github.com",
"git add README.md",
'git commit -m "Workflow-triggered README update"',
"git push",
];
for (let cmd of cmds) {
const { stdout, stderr } = await exec(cmd);
console.log(stdout || stderr);
}
}
async getRepos() {
// fetch all my repos
const res = await fetch("https://api.github.com/users/joel-hamilton/repos");
const repoData = await res.json();
// parse data from READMEs in all public repos
const promises = repoData
.filter((r) => r.name !== "joel-hamilton")
.map(async (repo) => {
const res = await fetch(
`https://raw.githubusercontent.com/joel-hamilton/${repo.name}/${repo.default_branch}/README.md`
);
const fileStream = res.body; //fs.createReadStream(res.body);
const rl = readline.createInterface(fileStream);
let readme = "";
let summary = "";
let tags = [];
let lineNum = 0;
for await (let line of rl) {
line = line.trim();
// first non-empty line after header is summary
if (lineNum > 0 && line && !summary) {
summary = line;
}
// add any {{tags|formatted|like|this}}
const tagMatches = line.match(/{{([\w|]*)}}/);
if (tagMatches) {
tags = tags.concat(tagMatches[1].split("|").filter((str) => !!str));
}
// Each line in input.txt will be successively available here as `line`.
readme += `${line}\n`;
lineNum++;
}
let category = "";
if (tags.length) {
category = tags.shift();
}
return {
name: repo.name,
url: repo.html_url,
summary,
category,
tags,
readme,
};
});
return Promise.all(promises);
}
getIntro() {
return `Hi there! I'm a full stack developer with 7+ years experience building and shipping large-scale customer-facing apps.\n`;
}
getTable() {
const rows = [
[
"",
"Well",
"A Little (but would like to know better)",
"Not Very Well (but would like to learn)",
],
["------", "------", "------", "------"],
[
"**Frontend**",
"Javascript, Typescript, Vue/Vuex, React, React Native",
"Angular",
"Cordova",
],
[
"**Backend**",
"NodeJS, PHP, Java, Express, Laravel, MongoDB, MySQL, Postgres, Redis",
"Go, Clojure, Python, NumPy/Pandas, C++, Flask, Django, GIS Mapping",
"Ruby, Kotlin, Swift, Objective-C, Rails, NestJS, GraphQL, C#/.NET",
],
[
"**DevOps**",
"Bash scripting, server admin, AWS, Google Cloud Platform, Digital Ocean, Docker, Vagrant",
"Azure, Kubernetes",
"",
],
[
"**Fields**",
"Identity & Access Management, Healthcare, eLearning, Oil & Gas",
"Finance",
"",
],
];
let table = "## Things I know\n\n";
for (let i = 0; i < rows.length; i++) {
const row = rows[i];
const maxWidth = 0;
// const maxWidth = rows.reduce((max, row) => {
// return Math.max(max, row.length);
// }, 0);
table += `|${row.map((val) => val.padEnd(maxWidth)).join("|")}\n`;
}
return table;
}
getGuide() {
let guide = "## A Hitchhiker's Guide to the Repos";
const categories = [
{
name: "finance",
displayName: "Finance Things",
lede: "",
repos: this.repos.filter((r) => r.category === "finance"),
},
{
name: "building",
displayName: "Building Things",
lede: "Code integrated into physical projects",
repos: this.repos.filter((r) => r.category === "building"),
},
{
name: "scripts",
displayName: "Small Scripts and Executables",
lede: "Automate the boring things!",
repos: this.repos.filter((r) => r.category === "scripts"),
},
{
name: "learning",
displayName: "Just for Fun/Learning Projects",
lede: "",
repos: this.repos.filter((r) => r.category === "learning"),
},
];
for (let category of categories) {
guide += `\n### ${category.displayName}`;
guide += category.lede ? `\n_${category.lede}_` : "";
for (let repo of category.repos) {
let plainIcons = ["graphql"]; // have -plain suffix rather than -original
const tagImages = repo.tags.map((tag) => {
let tagLoc =
tag + (plainIcons.includes(tag) ? "-plain.svg" : "-original.svg");
return `<img src="icons/${tag}/${tagLoc}" alt="${tag}" width="20" height="20">`;
});
guide += `\n- [${repo.name}](${repo.url})`;
if (tagImages.length) {
guide += " " + tagImages.join(" ");
}
guide += `\n - ${repo.summary}`;
}
}
return guide;
}
getContributions() {
return `
## Open-source Contributions
- [chartjs/Chart.js](https://github.com/chartjs/Chart.js)
- [mdn/content](https://github.com/mdn/content)
- [mherrmann/fullcalendar-rightclick](https://github.com/mherrmann/fullcalendar-rightclick)
- [thodorisbais/percircle](https://github.com/thodorisbais/percircle)`;
}
}
let g = new Generator();
g.generate();