forked from iZemil/job
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.js
More file actions
63 lines (50 loc) 路 1.65 KB
/
Copy pathparse.js
File metadata and controls
63 lines (50 loc) 路 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const fs = require('fs/promises');
const path = require('path');
const questionsDir = path.resolve('interview/questions');
const to = path.resolve('static/questions.json');
const initTopic = (title, topicPath) => ({
title,
dir: path.relative('interview', topicPath),
data: [],
});
const initQuestion = (id, title) => ({
id,
title,
});
(async function () {
const trimTitleSymbols = (str) => str.replace(/#+ /g, '');
try {
const questionsDirItems = await fs.readdir(questionsDir);
const parsed = [];
// read questions dir
for (const dirItem of questionsDirItems) {
const fromPath = path.join(questionsDir, dirItem);
const stat = await fs.stat(fromPath);
const isTopicDir = stat.isDirectory();
if (isTopicDir) {
const topicPath = path.resolve(questionsDir, dirItem);
const topicDir = await fs.readdir(topicPath);
const topic = initTopic(dirItem, topicPath);
// read topic files
for (const item of topicDir) {
const needParseItem = item.includes('index');
if (needParseItem) {
const data = await fs.readFile(path.resolve(topicPath, item), 'utf8');
const questionData = data.match(/{ id: .* }/gim);
const questionIds = questionData.join('').match(/'[\w\-]+'/gim);
for (const questionId of questionIds) {
const id = questionId.replace(/'/gi, '');
const qPath = path.resolve(topicPath, `${id}.md`);
const qData = await fs.readFile(qPath, 'utf8');
topic.data.push(initQuestion(id, trimTitleSymbols(qData.match(/# .*/i)[0])));
}
}
}
parsed.push(topic);
}
}
await fs.writeFile(to, JSON.stringify(parsed));
} catch (e) {
console.error(e);
}
})();