-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopulateLibraries.ts
39 lines (33 loc) · 1.27 KB
/
populateLibraries.ts
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
import { readdir, readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
const utf8: BufferEncoding = "utf8";
const readmePath = join(__dirname, "README.md");
const markers = {
start: "<!-- START: Auto-generated by ./populateLibraries.ts -->",
end: "<!-- END: Auto-generated by ./populateLibraries.ts -->",
};
async function updateReadme() {
const [readme, dirNames] = await Promise.all([
readFile(readmePath, utf8),
readdir(join(__dirname, "packages"), { withFileTypes: true }).then((d) =>
d.filter((dirent) => dirent.isDirectory()),
),
]);
const readmeLines = await Promise.all(
dirNames.map(async (dirent) => {
const path = join(__dirname, "packages", dirent.name, "package.json");
const { description } = JSON.parse(await readFile(path, utf8));
return `- [${dirent.name}](./packages/${dirent.name}/README.md): ${description ?? ""}`;
}),
);
const updatedReadme = readme.replace(
new RegExp(`${markers.start}[\\s\\S]*${markers.end}`),
`${markers.start}\n\n${readmeLines.join("\n")}\n\n${markers.end}`,
);
if (readme === updatedReadme) {
console.log("No README file changes to '## Libraries' section.");
} else {
await writeFile(readmePath, updatedReadme, utf8);
}
}
updateReadme();