HowToCook/.github/readme-generate.js

164 lines
4.1 KiB
JavaScript
Raw Normal View History

2022-02-26 10:05:53 +00:00
const { readdir, writeFile, stat } = require('fs/promises');
2022-03-06 19:28:45 +00:00
const fs = require('fs').promises;
2022-02-26 10:05:53 +00:00
const README_PATH = './README.md';
const MKDOCS_PATH = 'mkdocs.yml';
2022-02-26 10:05:53 +00:00
const ignorePaths = ['.git', 'README.md', 'node_modules', 'CONTRIBUTING.md', '.github'];
const categories = {
vegetable_dish: {
2022-03-02 00:08:33 +00:00
title: '素菜',
readme: '',
mkdocs: '',
2022-02-26 10:05:53 +00:00
},
meat_dish: {
2022-03-02 00:08:33 +00:00
title: '荤菜',
readme: '',
mkdocs: '',
2022-03-02 00:08:33 +00:00
},
2022-03-04 16:19:54 +00:00
aquatic: {
title: '水产',
readme: '',
mkdocs: '',
},
2022-02-26 10:05:53 +00:00
breakfast: {
title: '早餐',
readme: '',
mkdocs: '',
2022-02-26 10:05:53 +00:00
},
staple: {
title: '主食',
readme: '',
mkdocs: '',
2022-02-26 10:05:53 +00:00
},
'semi-finished': {
title: '半成品加工',
readme: '',
mkdocs: '',
2022-02-26 10:05:53 +00:00
},
soup: {
title: '汤与粥',
readme: '',
mkdocs: '',
2022-02-26 10:05:53 +00:00
},
drink: {
title: '饮料',
readme: '',
mkdocs: '',
2022-02-26 11:56:53 +00:00
},
condiment: {
title: '酱料和其它材料',
readme: '',
mkdocs: '',
2022-02-26 10:05:53 +00:00
},
dessert: {
title: '甜品',
readme: '',
mkdocs: '',
2022-02-26 10:05:53 +00:00
},
};
async function main() {
try {
let README_BEFORE = (README_MAIN = README_AFTER = '');
let MKDOCS_BEFORE = (MKDOCS_MAIN = MKDOCS_AFTER = '');
2022-02-26 10:05:53 +00:00
const markdownObj = await getAllMarkdown('.');
for (const markdown of markdownObj) {
if (markdown.path.includes('tips/advanced')) {
README_AFTER += inlineReadmeTemplate(markdown.file, markdown.path);
MKDOCS_AFTER += inlineMkdocsTemplate(markdown.file, markdown.path);
2022-02-26 10:05:53 +00:00
continue;
}
2022-02-26 10:05:53 +00:00
if (markdown.path.includes('tips')) {
README_BEFORE += inlineReadmeTemplate(markdown.file, markdown.path);
MKDOCS_BEFORE += inlineMkdocsTemplate(markdown.file, markdown.path);
2022-02-26 10:05:53 +00:00
continue;
}
for (const category of Object.keys(categories)) {
if (!markdown.path.includes(category)) continue;
categories[category].readme += inlineReadmeTemplate(markdown.file, markdown.path);
categories[category].mkdocs += inlineMkdocsTemplate(
markdown.file,
markdown.path,
true,
);
2022-02-26 10:05:53 +00:00
}
}
for (const category of Object.values(categories)) {
README_MAIN += categoryReadmeTemplate(category.title, category.readme);
MKDOCS_MAIN += categoryMkdocsTemplate(category.title, category.mkdocs);
2022-02-26 10:05:53 +00:00
}
2022-02-26 10:31:36 +00:00
2022-03-06 19:28:45 +00:00
const MKDOCS_TEMPLATE = await fs.readFile("./.github/templates/mkdocs_template.yml", "utf-8");
const README_TEMPLATE = await fs.readFile("./.github/templates/readme_template.md", "utf-8");
2022-02-27 06:51:37 +00:00
await writeFile(
README_PATH,
2022-03-06 19:28:45 +00:00
README_TEMPLATE
.replace('{{before}}', README_BEFORE.trim())
.replace('{{main}}', README_MAIN.trim())
.replace('{{after}}', README_AFTER.trim()),
);
2022-03-06 19:28:45 +00:00
await writeFile(
MKDOCS_PATH,
2022-03-06 19:28:45 +00:00
MKDOCS_TEMPLATE
.replace('{{before}}', MKDOCS_BEFORE)
.replace('{{main}}', MKDOCS_MAIN)
.replace('{{after}}', MKDOCS_AFTER),
2022-02-27 06:51:37 +00:00
);
2022-02-26 10:05:53 +00:00
} catch (error) {
console.error(error);
}
}
async function getAllMarkdown(path) {
const paths = [];
const files = await readdir(path);
2022-02-27 06:51:37 +00:00
// chinese alphabetic order
files.sort((a, b) => a.localeCompare(b, 'zh-CN'));
// mtime order
// files.sort(async (a, b) => {
// const aStat = await stat(`${path}/${a}`);
// const bStat = await stat(`${path}/${b}`);
// return aStat.mtime - bStat.mtime;
// });
2022-02-26 10:05:53 +00:00
for (const file of files) {
const filePath = `${path}/${file}`;
if (ignorePaths.includes(file)) continue;
const fileStat = await stat(filePath);
if (fileStat.isFile() && file.endsWith('.md')) {
paths.push({ path, file });
} else if (fileStat.isDirectory()) {
const subFiles = await getAllMarkdown(filePath);
paths.push(...subFiles);
}
}
return paths;
}
function inlineReadmeTemplate(file, path) {
2022-03-03 16:39:06 +00:00
return `- [${file.replace('.md', '')}](${path}/${file})\n`;
2022-02-26 10:05:53 +00:00
}
function categoryReadmeTemplate(title, inlineStr) {
2022-02-26 10:05:53 +00:00
return `\n### ${title}\n\n${inlineStr}`;
}
function inlineMkdocsTemplate(file, path, isDish = false) {
return `${' '.repeat(isDish ? 10 : 6)}- ${file.replace('.md', '')}: ${path}/${file}\n`;
}
function categoryMkdocsTemplate(title, inlineStr) {
return `\n${' '.repeat(6)}- ${title}:\n${inlineStr}`;
}
2022-02-26 10:05:53 +00:00
main();