-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-index.js
59 lines (52 loc) · 1.83 KB
/
build-index.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
import fs from 'node:fs';
import path from 'node:path';
import { stringify } from 'yaml';
const rootPackage = JSON.parse(fs.readFileSync('./package.json'));
const repoURL = new URL(rootPackage.homepage);
repoURL.hash = '';
const output = {
name: 'PlaceKit implementation examples',
url: repoURL.href,
author: rootPackage.author,
examples: [],
};
const examplesPath = 'examples';
const baseURL = [repoURL.href, 'tree/main', examplesPath].join('/');
for (const exampleDir of fs.readdirSync(examplesPath, { withFileTypes: true })) {
if (exampleDir.isDirectory()) {
for (const exampleFile of fs.readdirSync(path.join(exampleDir.path, exampleDir.name), {
withFileTypes: true,
})) {
if (exampleFile.name === 'package.json') {
const packagePath = path.join(exampleFile.path, exampleFile.name);
const packageRaw = fs.readFileSync(packagePath, { encoding: 'utf8' });
const packageJSON = JSON.parse(packageRaw);
output.examples.push({
slug: exampleDir.name,
url: [baseURL, exampleDir.name].join('/'),
name: packageJSON.description,
env: packageJSON.keywords.sort(),
});
}
}
}
}
const outputFile = 'examples-index.yml';
fs.writeFileSync(outputFile, stringify(output));
console.log(`✅ ${outputFile} updated!`);
const readmeFile = 'README.md';
let readmeText = fs.readFileSync(readmeFile, { encoding: 'utf8' });
readmeText = readmeText.replace(/(\#{2,}\s*examples index)[^#]+/im, (...m) => {
return `${m[1]}
<!-- prettier-ignore -->
| Description | Environment |
| :--- | :--- |
| ${output.examples
.map((example) =>
[`[${example.name}](./examples/${example.slug})`, example.env.join(', ')].join(' | '),
)
.join(` |\n| `)} |
\n`;
});
fs.writeFileSync(readmeFile, readmeText);
console.log(`✅ ${readmeFile} updated!`);