-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate_index.js
More file actions
62 lines (54 loc) · 1.9 KB
/
Copy pathupdate_index.js
File metadata and controls
62 lines (54 loc) · 1.9 KB
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
'use strict';
const fs = require('fs');
const path = require('path');
const repository = 'h1cr.io/website';
const indexFile = 'README.md';
const markerStart = '<!-- toc-start -->\n';
const markerEnd = '<!-- toc-end -->\n';
const listImage = async () => {
const images = [];
for (const name of fs.readdirSync(__dirname)) {
let config;
try {
config = require(await path.join(__dirname, name, 'tags'));
} catch (err) {
continue;
}
const tags = Object.keys(config);
const packages = Object.values(config).map(x => x.packages).flat().filter(x => !!x);
images.push({ name, tags, packages });
}
return images;
};
const quote = (v) => ['```', v, '```'].join('');
const generateToc = (images) => {
const line = [];
line.push('| Image | Tag | Repology latest |');
line.push('|------ | --- | --------------- |');
for (const image of images) {
const row = [
quote(`${repository}/${image.name}`),
image.tags.map(x => quote(x)).join(', '),
image.packages.map(x => ``).join(' ')
];
line.push(`| ${row.join(' | ')} |`);
};
return `${line.join('\n')}\n`;
};
const updateIndex = async (index) => {
const source = await fs.promises.readFile(indexFile, { encoding: 'utf-8' });
const start = source.substring(0, source.indexOf(markerStart));
const end = source.substring(source.indexOf(markerEnd) + markerEnd.length);
const output = `${start}${markerStart}${index}${markerEnd}${end}`;
await fs.promises.writeFile(indexFile, output);
};
const main = async () => {
const images = await listImage();
const index = generateToc(images);
await updateIndex(index);
};
main().catch(err => {
console.error(err);
console.error(err.stack);
process.exit(1);
});