Skip to content

Commit 437456b

Browse files
authored
Update readme-generate.js (#1329)
* Update readme-generate.js * Update readme-generate.js * Update readme-generate.js
1 parent 426f3e5 commit 437456b

File tree

1 file changed

+99
-18
lines changed

1 file changed

+99
-18
lines changed

.github/readme-generate.js

Lines changed: 99 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
const { readdir, writeFile, stat } = require('fs/promises');
22
const fs = require('fs').promises;
3+
const path = require('path');
34

45
const README_PATH = './README.md';
5-
66
const MKDOCS_PATH = 'mkdocs.yml';
7+
const dishesFolder = 'dishes';
8+
const starsystemFolder = 'starsystem';
79

810
const ignorePaths = ['.git', 'README.md', 'node_modules', 'CONTRIBUTING.md', '.github'];
911

@@ -60,12 +62,77 @@ const categories = {
6062
},
6163
};
6264

65+
async function countStars(filename) {
66+
const data = await fs.readFile(filename, 'utf-8');
67+
let stars = 0;
68+
const lines = data.split('\n');
69+
lines.forEach(line => {
70+
stars += (line.match(//g) || []).length;
71+
});
72+
return stars;
73+
}
74+
75+
async function organizeByStars(dishesFolder, starsystemFolder) {
76+
const dishes = {};
77+
78+
async function processFolder(folderPath) {
79+
const files = await readdir(folderPath);
80+
for (const filename of files) {
81+
const filepath = path.join(folderPath, filename);
82+
const fileStat = await stat(filepath);
83+
if (fileStat.isFile() && filename.endsWith('.md')) {
84+
const stars = await countStars(filepath);
85+
dishes[filepath] = stars;
86+
} else if (fileStat.isDirectory()) {
87+
await processFolder(filepath);
88+
}
89+
}
90+
}
91+
92+
const dishesFolderAbs = path.resolve(dishesFolder);
93+
const starsystemFolderAbs = path.resolve(starsystemFolder);
94+
95+
if (!await fs.access(starsystemFolderAbs).then(() => true).catch(() => false)) {
96+
await fs.mkdir(starsystemFolderAbs, { recursive: true });
97+
}
98+
99+
if (!await fs.access(dishesFolderAbs).then(() => true).catch(() => false)) {
100+
console.log(`Directory not found: ${dishesFolderAbs}, creating directory...`);
101+
await fs.mkdir(dishesFolderAbs, { recursive: true });
102+
}
103+
104+
await processFolder(dishesFolderAbs);
105+
106+
const starRatings = Array.from(new Set(Object.values(dishes))).sort((a, b) => b - a);
107+
const navigationLinks = [];
108+
109+
for (const stars of starRatings) {
110+
const starsFile = path.join(starsystemFolderAbs, `${stars}Star.md`);
111+
const content = [`# Dishes with ${stars} Stars`, ''];
112+
for (const [filepath, starCount] of Object.entries(dishes)) {
113+
if (starCount === stars) {
114+
const relativePath = path.relative(starsystemFolderAbs, filepath).replace(/\\/g, '/');
115+
content.push(`* [${path.basename(filepath, '.md')}](./${relativePath})`);
116+
}
117+
}
118+
await writeFile(starsFile, content.join('\n'), 'utf-8');
119+
navigationLinks.push(`- [${stars} Star Dishes](${path.relative(path.dirname(README_PATH), starsFile).replace(/\\/g, '/')})`);
120+
}
121+
122+
return navigationLinks;
123+
}
124+
63125
async function main() {
64126
try {
65-
let README_BEFORE = (README_MAIN = README_AFTER = '');
66-
let MKDOCS_BEFORE = (MKDOCS_MAIN = MKDOCS_AFTER = '');
127+
let README_BEFORE = '', README_MAIN = '', README_AFTER = '';
128+
let MKDOCS_BEFORE = '', MKDOCS_MAIN = '', MKDOCS_AFTER = '';
67129
const markdownObj = await getAllMarkdown('.');
130+
131+
// Debug logging to understand the structure of markdownObj
132+
console.log("Markdown Object Structure:", JSON.stringify(markdownObj, null, 2));
133+
68134
for (const markdown of markdownObj) {
135+
console.log("Processing markdown:", markdown);
69136
if (markdown.path.includes('tips/advanced')) {
70137
README_AFTER += inlineReadmeTemplate(markdown.file, markdown.path);
71138
MKDOCS_AFTER += inlineMkdocsTemplate(markdown.file, markdown.path);
@@ -94,48 +161,62 @@ async function main() {
94161
MKDOCS_MAIN += categoryMkdocsTemplate(category.title, category.mkdocs);
95162
}
96163

97-
const MKDOCS_TEMPLATE = await fs.readFile("./.github/templates/mkdocs_template.yml", "utf-8");
98-
const README_TEMPLATE = await fs.readFile("./.github/templates/readme_template.md", "utf-8");
164+
let MKDOCS_TEMPLATE;
165+
let README_TEMPLATE;
166+
167+
try {
168+
MKDOCS_TEMPLATE = await fs.readFile("./.github/templates/mkdocs_template.yml", "utf-8");
169+
} catch (error) {
170+
MKDOCS_TEMPLATE = `site_name: My Docs\nnav:\n {{main}}\n`;
171+
console.warn("mkdocs_template.yml not found, using default template");
172+
}
173+
174+
try {
175+
README_TEMPLATE = await fs.readFile("./.github/templates/readme_template.md", "utf-8");
176+
} catch (error) {
177+
README_TEMPLATE = `# My Project\n\n{{before}}\n\n{{main}}\n\n{{after}}`;
178+
console.warn("readme_template.md not found, using default template");
179+
}
180+
181+
const navigationLinks = await organizeByStars(dishesFolder, starsystemFolder);
182+
// Debug logging to ensure navigationLinks is defined and contains data
183+
console.log("Navigation Links:", navigationLinks);
184+
const navigationSection = `\n## Navigation\n\n${navigationLinks.join('\n')}`;
99185

100186
await writeFile(
101187
README_PATH,
102188
README_TEMPLATE
103189
.replace('{{before}}', README_BEFORE.trim())
104190
.replace('{{main}}', README_MAIN.trim())
105-
.replace('{{after}}', README_AFTER.trim()),
191+
.replace('{{after}}', README_AFTER.trim())+ navigationSection,
106192
);
107193

108-
109194
await writeFile(
110195
MKDOCS_PATH,
111196
MKDOCS_TEMPLATE
112197
.replace('{{before}}', MKDOCS_BEFORE)
113198
.replace('{{main}}', MKDOCS_MAIN)
114199
.replace('{{after}}', MKDOCS_AFTER),
115200
);
201+
202+
// Organize files by star rating
203+
//await organizeByStars(dishesFolder, starsystemFolder);
116204
} catch (error) {
117205
console.error(error);
118206
}
119207
}
120208

121-
async function getAllMarkdown(path) {
209+
async function getAllMarkdown(dir) {
122210
const paths = [];
123-
const files = await readdir(path);
124-
// chinese alphabetic order
211+
const files = await readdir(dir);
125212
files.sort((a, b) => a.localeCompare(b, 'zh-CN'));
126213

127-
// mtime order
128-
// files.sort(async (a, b) => {
129-
// const aStat = await stat(`${path}/${a}`);
130-
// const bStat = await stat(`${path}/${b}`);
131-
// return aStat.mtime - bStat.mtime;
132-
// });
133214
for (const file of files) {
134-
const filePath = `${path}/${file}`;
215+
const filePath = path.join(dir, file);
135216
if (ignorePaths.includes(file)) continue;
136217
const fileStat = await stat(filePath);
137218
if (fileStat.isFile() && file.endsWith('.md')) {
138-
paths.push({ path, file });
219+
paths.push({ path: dir, file });
139220
} else if (fileStat.isDirectory()) {
140221
const subFiles = await getAllMarkdown(filePath);
141222
paths.push(...subFiles);

0 commit comments

Comments
 (0)