-
Notifications
You must be signed in to change notification settings - Fork 4
/
generateReadme.js
executable file
·66 lines (57 loc) · 2.12 KB
/
generateReadme.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
const fs = require('fs');
const repositories = require('./repositories.json');
const readme = fs.readFileSync('./README.md', 'utf8');
const HEADER = '<!-- Repositories -->\n';
const repositoryIndex = readme.indexOf(HEADER);
const repoToTableRow = (repo) => {
const url = repo.url;
// Only supports github repositories right now
const [_, ghOrg, ghRepoName] = url.match(
/https:\/\/github.com\/(\w+)\/([\w_-]+)/
);
const slug = `${ghOrg}/${ghRepoName}`;
const supportsReleases =
typeof repo.supportsReleases === "boolean" ? repo.supportsReleases : true;
const repository = `[${slug}](https://github.com/${slug})`;
const type = repo.packages.map(getTypeBadge).join("<br>");
const packages = repo.packages.map(getPackageBadge).join("<br>");
const released =
!repo.private && supportsReleases
? `![GitHub Release Date](https://img.shields.io/github/release-date/${slug}?color=ffc16b&label=%20)`
: "";
const tableSegments = [repository, type, packages, released];
return `| ${tableSegments.join(" | ")} |`;
};
const colorMap = new Map([
['issues', 'white'],
['library', 'yellowgreen'],
['plugin', 'blue'],
['GHA', 'orange'],
['config', 'lightblue'],
['template', 'purple'],
])
/**
* Get the img.shield badge as <type>.
* @param {name: string, type: 'package' | 'library' } package
*/
function getTypeBadge(package) {
const color = colorMap.get(package.type) ?? 'lightgrey';
return `![type](https://img.shields.io/badge/%20-${package.type}-${color})`;
}
/**
* Get the img.shield badge as <name><version>.
* @param {name: string, type: 'package' | 'library' } package
*/
function getPackageBadge(package) {
if(package.type !== 'GHA' && package.name) {
return `[![NPM](https://img.shields.io/npm/v/${package.name}.svg?label=${package.name})](https://www.npmjs.com/package/${package.name})`;
}
}
const contents = [
HEADER,
"| Repository | Type | Package | Released |",
"|------------|:----:|---------|----------|",
...repositories.map(repoToTableRow),
];
const newContents = readme.substring(0, repositoryIndex) + contents.join('\n') + '\n';
fs.writeFileSync('README.md', newContents, '')