Skip to content

Commit

Permalink
chore: update generate script
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangshanmeta committed Dec 7, 2024
1 parent 7a1fe48 commit a2f0ef2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
3 changes: 3 additions & 0 deletions scripts/_prefix.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@

## 目录

* TS/JS Answer {{totalTSOrJS}}
* Java Answer {{totalJava}}

| index | title | code | Difficulty |
| :--: | :-------------------: | :---: | :--: |
26 changes: 25 additions & 1 deletion scripts/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,19 @@ const answersMap = fileList.reduce<Record<string, string[]>>((obj, dirName) => {
return obj;
}, {});

let totalJava = 0;
let totalTSOrJS = 0;

const mergedQuestions = questions.map((question) => {
const {
index,
title,
difficulty,
} = question;

let hasJava = false;
let hasTSOrJS = false;

const answers = (answersMap[index] || []).filter((answerFileName) => {
// 未来考虑添加md做题解
const ext = answerFileName.split('.').pop()!;
Expand All @@ -31,13 +38,30 @@ const mergedQuestions = questions.map((question) => {
const name = answerFileName.split('.');
const ext = name.pop()!;
const label = extLabelMap[ext];
if (ext === 'java') {
hasJava = true;
}
if (ext === 'js' || ext === 'ts') {
hasTSOrJS = true;
}

return `[${label}](./src/${genFolderName(question)}/${answerFileName})`;
}).join(' ');

if (hasJava) {
totalJava++;
}
if (hasTSOrJS) {
totalTSOrJS++;
}

return `| ${index} | ${title} | ${answers} | ${difficultyMap[difficulty]} |`;
}).join('\n');

const prefix = fs.readFileSync(path.join(__dirname, './_prefix.md'), 'utf8');
let prefix = fs.readFileSync(path.join(__dirname, './_prefix.md'), 'utf8');

prefix = prefix.replace('{{totalTSOrJS}}', totalTSOrJS.toString());
prefix = prefix.replace('{{totalJava}}', totalJava.toString());

fs.writeFile(path.join(__dirname, '../README.md'), prefix + mergedQuestions + '\n', 'utf8', (err) => {
if (err) throw err;
Expand Down

0 comments on commit a2f0ef2

Please sign in to comment.