|
| 1 | +const { readdir, writeFile, stat } = require('fs/promises'); |
| 2 | + |
| 3 | +const ignorePaths = ['.git', 'README.md', 'node_modules', 'CONTRIBUTING.md', '.github']; |
| 4 | + |
| 5 | +const categories = { |
| 6 | + 'home-cooking': { |
| 7 | + title: '家常菜', |
| 8 | + template: '', |
| 9 | + }, |
| 10 | + breakfast: { |
| 11 | + title: '早餐', |
| 12 | + template: '', |
| 13 | + }, |
| 14 | + staple: { |
| 15 | + title: '主食', |
| 16 | + template: '', |
| 17 | + }, |
| 18 | + 'semi-finished': { |
| 19 | + title: '半成品加工', |
| 20 | + template: '', |
| 21 | + }, |
| 22 | + soup: { |
| 23 | + title: '汤与粥', |
| 24 | + template: '', |
| 25 | + }, |
| 26 | + drink: { |
| 27 | + title: '饮料', |
| 28 | + template: '', |
| 29 | + }, |
| 30 | + condiment: { |
| 31 | + title: '酱料和其它材料', |
| 32 | + template: '', |
| 33 | + }, |
| 34 | + dessert: { |
| 35 | + title: '甜品', |
| 36 | + template: '', |
| 37 | + }, |
| 38 | +}; |
| 39 | + |
| 40 | +let README_TEMPLATE = `# 程序员做饭指南 |
| 41 | +
|
| 42 | +[](https://github.com/Anduin2017/HowToCook/actions/workflows/ci.yml) |
| 43 | +[](./LICENSE) |
| 44 | +[](https://github.com/Anduin2017/HowToCook/graphs/contributors) |
| 45 | +
|
| 46 | +最近在家隔离,出不了门。只能宅在家做饭了。作为程序员,我偶尔在网上找找菜谱和做法。但是这些菜谱往往写法千奇百怪,经常中间莫名出来一些材料。对于习惯了形式语言的程序员来说极其不友好。 |
| 47 | +
|
| 48 | +所以,我计划自己搜寻菜谱和并结合实际做菜的经验,准备用更清晰精准的描述来整理常见菜的做法,以方便程序员在家做饭。 |
| 49 | +
|
| 50 | +同样,我希望它是一个由社区驱动和维护的开源项目,使更多人能够一起做一个有趣的仓库。所以非常欢迎大家贡献它~ |
| 51 | +
|
| 52 | +## 如何贡献 |
| 53 | +
|
| 54 | +针对发现的问题,直接修改并提交 Pull request 即可。 |
| 55 | +
|
| 56 | +在写新菜谱时,请复制并修改已有的菜谱模板: [示例菜](https://github.com/Anduin2017/HowToCook/blob/master/dishes/template/%E7%A4%BA%E4%BE%8B%E8%8F%9C/%E7%A4%BA%E4%BE%8B%E8%8F%9C.md?plain=1)。 |
| 57 | +在提交 Pull Request 前更新一下 README.md 里的引用。 |
| 58 | +
|
| 59 | +## 做菜之前 |
| 60 | +
|
| 61 | +{{before}} |
| 62 | +## 菜谱 |
| 63 | +{{main}} |
| 64 | +## 进阶知识学习 |
| 65 | +
|
| 66 | +如果你已经做了许多上面的菜,对于厨艺已经入门,并且想学习更加高深的烹饪技巧,请继续阅读下面的内容: |
| 67 | +
|
| 68 | +{{after}}`; |
| 69 | + |
| 70 | +async function main() { |
| 71 | + try { |
| 72 | + let BEFORE = (MAIN = AFTER = ''); |
| 73 | + const markdownObj = await getAllMarkdown('.'); |
| 74 | + for (const markdown of markdownObj) { |
| 75 | + if (markdown.path.includes('tips/advanced')) { |
| 76 | + AFTER += inlineTemplate(markdown.file, markdown.path, true); |
| 77 | + continue; |
| 78 | + } |
| 79 | + if (markdown.path.includes('tips')) { |
| 80 | + BEFORE += inlineTemplate(markdown.file, markdown.path, true); |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + for (const category of Object.keys(categories)) { |
| 85 | + if (markdown.path.includes(category)) { |
| 86 | + let currentCategoryStr = categories[category].template; |
| 87 | + currentCategoryStr += inlineTemplate(markdown.file, markdown.path); |
| 88 | + categories[category].template = currentCategoryStr; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + for (const category of Object.values(categories)) { |
| 94 | + MAIN += categoryTemplate(category.title, category.template); |
| 95 | + } |
| 96 | + |
| 97 | + await writeFile( |
| 98 | + './README.md', |
| 99 | + README_TEMPLATE.replace('{{before}}', BEFORE) |
| 100 | + .replace('{{main}}', MAIN) |
| 101 | + .replace('{{after}}', AFTER), |
| 102 | + ); |
| 103 | + } catch (error) { |
| 104 | + console.error(error); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +async function getAllMarkdown(path) { |
| 109 | + const paths = []; |
| 110 | + const files = await readdir(path); |
| 111 | + // chinese alphabetic order |
| 112 | + files.sort((a, b) => a.localeCompare(b, 'zh-CN')); |
| 113 | + |
| 114 | + // mtime order |
| 115 | + // files.sort(async (a, b) => { |
| 116 | + // const aStat = await stat(`${path}/${a}`); |
| 117 | + // const bStat = await stat(`${path}/${b}`); |
| 118 | + // return aStat.mtime - bStat.mtime; |
| 119 | + // }); |
| 120 | + for (const file of files) { |
| 121 | + const filePath = `${path}/${file}`; |
| 122 | + if (ignorePaths.includes(file)) continue; |
| 123 | + const fileStat = await stat(filePath); |
| 124 | + if (fileStat.isFile() && file.endsWith('.md')) { |
| 125 | + paths.push({ path, file }); |
| 126 | + } else if (fileStat.isDirectory()) { |
| 127 | + const subFiles = await getAllMarkdown(filePath); |
| 128 | + paths.push(...subFiles); |
| 129 | + } |
| 130 | + } |
| 131 | + return paths; |
| 132 | +} |
| 133 | + |
| 134 | +function inlineTemplate(file, path) { |
| 135 | + return `* [${file.replace('.md', '')}](${path}/${file})\n`; |
| 136 | +} |
| 137 | + |
| 138 | +function categoryTemplate(title, inlineStr) { |
| 139 | + return `\n### ${title}\n\n${inlineStr}`; |
| 140 | +} |
| 141 | + |
| 142 | +main(); |
0 commit comments