-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate-index.mjs
40 lines (35 loc) · 1.15 KB
/
generate-index.mjs
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
import fs from 'node:fs';
import path from 'node:path';
import { execSync } from 'node:child_process';
const dirs = fs
.readdirSync('./', { withFileTypes: true })
.filter((d) => d.isDirectory())
.filter((d) => !d.name.startsWith('.') && d.name !== 'node_modules');
const listItems = dirs.map((dirent) => {
const dirname = dirent.name;
console.assert(fs.statSync(`${dirname}/index.html`)); // throw if no index.html
return `<li><a href="${dirname}/index.html">${dirname}</a></li>`;
});
const html = `
<!doctype html>
<style>
head, title { display: block; font-weight: bold; margin: 8px; }
html { font-size: 200%}
</style>
<title>performance-stories index</title>
<ul>
${listItems.join('\n')}
</ul>
`;
fs.writeFileSync('index.html', html, 'utf-8');
console.log('Wrote:', `${process.cwd()}/index.html`);
if (process.argv.includes('--validate')) {
const status = execSync('git status --porcelain', { encoding: 'utf8' });
if (status.trim().length > 0) {
console.error('ERROR: Unexpected git diff after validating index');
console.error(
'Make sure you have run `npm run genindex` to update the index page.',
);
process.exit(1);
}
}