-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathgovernance.mjs
More file actions
97 lines (83 loc) · 2.76 KB
/
Copy pathgovernance.mjs
File metadata and controls
97 lines (83 loc) · 2.76 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
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
import { mkdir, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { fetchWithRetry } from '../utils/fetch.mjs';
import { rewriteRelativeLinks } from './sanitize.mjs';
const { GH_TOKEN } = process.env;
const BASE_HEADERS = {
...(GH_TOKEN && { Authorization: `Bearer ${GH_TOKEN}` }),
};
// Maps source filenames in webpack/governance repo to their output slug and sidebar label.
// Insertion order determines sidebar order, this could be changed as per need.
const FILE_MAP = {
'README.md': { output: 'index', label: 'Overview' },
'CHARTER.md': { output: 'charter', label: 'Charter' },
'MEMBER_EXPECTATIONS.md': {
output: 'member-expectations',
label: 'Member Expectations',
},
'MODERATION_POLICY.md': {
output: 'moderation-policy',
label: 'Moderation Policy',
},
'WORKING_GROUPS.md': { output: 'working-groups', label: 'Working Groups' },
'AI_POLICY.md': { output: 'ai-policy', label: 'AI Policy' },
};
const pageLink = output =>
output === 'index' ? '/about/governance' : `/about/governance/${output}`;
// Derived from FILE_MAP - stays in sync automatically if entries are added/removed.
const LINK_REWRITE_MAP = Object.fromEntries(
Object.entries(FILE_MAP).map(([source, { output }]) => [
source,
pageLink(output),
])
);
const outputDir = join(
import.meta.dirname,
'..',
'..',
'pages',
'about',
'governance'
);
await mkdir(outputDir, { recursive: true });
const results = await Promise.all(
Object.entries(FILE_MAP).map(async ([source, { output, label }]) => {
const url = `https://raw.githubusercontent.com/webpack/governance/HEAD/${source}`;
const res = await fetchWithRetry(url, { headers: BASE_HEADERS });
if (!res.ok) {
console.error(`Failed: ${source} -> ${res.status} ${res.statusText}`);
return null;
}
let body = rewriteRelativeLinks(
await res.text(),
file => LINK_REWRITE_MAP[file] ?? null
);
// Some governance docs (e.g. MEMBER_EXPECTATIONS.md) have no H1, which the
// site derives the page title from — fall back to the sidebar label.
if (!/^# /m.test(body)) body = `# ${label}\n\n${body}`;
const content = `---\nsource: ${url}\n---\n\n${body}`;
await writeFile(join(outputDir, `${output}.md`), content, 'utf8');
console.log(`Fetched: ${source} -> ${output}.md`);
return { output, label };
})
);
const fetched = results.filter(Boolean);
const siteJson = {
sidebar: [
{
groupName: 'Governance',
items: fetched.map(({ output, label }) => ({
link: pageLink(output),
label,
})),
},
],
};
await writeFile(
join(outputDir, 'site.json'),
JSON.stringify(siteJson, null, 2) + '\n',
'utf8'
);
console.log(
`Written: pages/about/governance/site.json (${fetched.length} pages)`
);