/
index.js
50 lines (40 loc) · 1.28 KB
/
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
import fs from 'fs';
import markdownIt from 'markdown-it';
const FILENAME = 'README.md';
const md = markdownIt({
html: true,
linkify: true,
breaks: true,
});
const getMarkdownContent = async () => {
try {
const sections = [];
for await (const file of await fs.promises.readdir(`./src/sections`)) {
if (file.startsWith('_')) {
continue;
}
const section = await import(`./src/sections/${file}`);
if (typeof section.getContent !== 'function' || typeof section.ORDER !== 'number') {
continue;
}
console.log(`Generating section - ${file}`);
sections.push(section);
}
sections.sort((a, b) => a.ORDER - b.ORDER);
const sectionsContent = await Promise.all(sections.map(async (section) => await section.getContent()));
return md.render(sectionsContent.join('\n'));
} catch (error) {
console.log('An error has occurred getting markdown content', error);
throw error;
}
};
(async () => {
try {
/* Write README.md */
const markdownContent = await getMarkdownContent();
await fs.promises.writeFile(FILENAME, markdownContent);
console.log(`Markdown content generated and written to ${FILENAME}`);
} catch (error) {
console.error('Error occurred, not writing new README:', error);
}
})();