Skip to content

Commit c85ba33

Browse files
authored
Update readme-generate.js
1 parent 426f3e5 commit c85ba33

File tree

1 file changed

+86
-5
lines changed

1 file changed

+86
-5
lines changed

.github/readme-generate.js

Lines changed: 86 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ const README_PATH = './README.md';
55

66
const MKDOCS_PATH = 'mkdocs.yml';
77

8+
const dishesFolder = 'dishes';
9+
10+
const starsystemFolder = 'starsystem';
11+
812
const ignorePaths = ['.git', 'README.md', 'node_modules', 'CONTRIBUTING.md', '.github'];
913

1014
const categories = {
@@ -59,18 +63,78 @@ const categories = {
5963
mkdocs: '',
6064
},
6165
};
66+
async function countStars(filename) {
67+
const data = await fs.readFile(filename, 'utf-8');
68+
let stars = 0;
69+
const lines = data.split('\n');
70+
lines.forEach(line => {
71+
stars += (line.match(//g) || []).length;
72+
});
73+
return stars;
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+
const dishesFolderAbs = path.resolve(dishesFolder);
92+
const starsystemFolderAbs = path.resolve(starsystemFolder);
93+
94+
if (!await fs.access(starsystemFolderAbs).then(() => true).catch(() => false)) {
95+
await fs.mkdir(starsystemFolderAbs, { recursive: true });
96+
}
97+
98+
if (!await fs.access(dishesFolderAbs).then(() => true).catch(() => false)) {
99+
console.log(`Directory not found: ${dishesFolderAbs}, creating directory...`);
100+
await fs.mkdir(dishesFolderAbs, { recursive: true });
101+
}
102+
await processFolder(dishesFolderAbs);
103+
104+
const starRatings = Array.from(new Set(Object.values(dishes))).sort((a, b) => b - a);
105+
const navigationLinks = [];
106+
107+
for (const stars of starRatings) {
108+
const starsFile = path.join(starsystemFolderAbs, `${stars}Star.md`);
109+
const content = [`# Dishes with ${stars} Stars`, ''];
110+
for (const [filepath, starCount] of Object.entries(dishes)) {
111+
if (starCount === stars) {
112+
const relativePath = path.relative(starsystemFolderAbs, filepath).replace(/\\/g, '/');
113+
content.push(`* [${path.basename(filepath, '.md')}](./${relativePath})`);
114+
}
115+
}
116+
await writeFile(starsFile, content.join('\n'), 'utf-8');
117+
navigationLinks.push(`- [${stars} Star Dishes](${path.relative(path.dirname(README_PATH), starsFile).replace(/\\/g, '/')})`);
118+
}
119+
120+
return navigationLinks;
121+
}
62122

63123
async function main() {
64124
try {
65125
let README_BEFORE = (README_MAIN = README_AFTER = '');
66126
let MKDOCS_BEFORE = (MKDOCS_MAIN = MKDOCS_AFTER = '');
67127
const markdownObj = await getAllMarkdown('.');
128+
// Debug logging to understand the structure of markdownObj
129+
console.log("Markdown Object Structure:", JSON.stringify(markdownObj, null, 2));
130+
68131
for (const markdown of markdownObj) {
69132
if (markdown.path.includes('tips/advanced')) {
70133
README_AFTER += inlineReadmeTemplate(markdown.file, markdown.path);
71134
MKDOCS_AFTER += inlineMkdocsTemplate(markdown.file, markdown.path);
72135
continue;
73136
}
137+
74138

75139
if (markdown.path.includes('tips')) {
76140
README_BEFORE += inlineReadmeTemplate(markdown.file, markdown.path);
@@ -93,10 +157,27 @@ async function main() {
93157
README_MAIN += categoryReadmeTemplate(category.title, category.readme);
94158
MKDOCS_MAIN += categoryMkdocsTemplate(category.title, category.mkdocs);
95159
}
160+
let MKDOCS_TEMPLATE;
161+
let README_TEMPLATE;
162+
try {
163+
MKDOCS_TEMPLATE = await fs.readFile("./.github/templates/mkdocs_template.yml", "utf-8");
164+
} catch (error) {
165+
MKDOCS_TEMPLATE = `site_name: My Docs\nnav:\n {{main}}\n`;
166+
console.warn("mkdocs_template.yml not found, using default template");
167+
}
96168

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");
169+
try {
170+
README_TEMPLATE = await fs.readFile("./.github/templates/readme_template.md", "utf-8");
171+
} catch (error) {
172+
README_TEMPLATE = `# My Project\n\n{{before}}\n\n{{main}}\n\n{{after}}`;
173+
console.warn("readme_template.md not found, using default template");
174+
}
175+
const navigationLinks = await organizeByStars(dishesFolder, starsystemFolder);
176+
// Debug logging to ensure navigationLinks is defined and contains data
177+
console.log("Navigation Links:", navigationLinks);
178+
const navigationSection = `\n## Navigation\n\n${navigationLinks.join('\n')}`;
99179

180+
100181
await writeFile(
101182
README_PATH,
102183
README_TEMPLATE
@@ -118,9 +199,9 @@ async function main() {
118199
}
119200
}
120201

121-
async function getAllMarkdown(path) {
202+
async function getAllMarkdown(dir) {
122203
const paths = [];
123-
const files = await readdir(path);
204+
const files = await readdir(dir);
124205
// chinese alphabetic order
125206
files.sort((a, b) => a.localeCompare(b, 'zh-CN'));
126207

@@ -131,7 +212,7 @@ async function getAllMarkdown(path) {
131212
// return aStat.mtime - bStat.mtime;
132213
// });
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')) {

0 commit comments

Comments
 (0)