-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
71 lines (65 loc) · 2.57 KB
/
gatsby-node.js
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
64
65
66
67
68
69
70
71
const fs = require('fs');
const getQuestionByTags = (jsonFile, topic) => {
const tagDict = {};
tagDict[topic] = [];
Object.keys(jsonFile).forEach(function (v) {
for (let eachTag of jsonFile[v]['tags']) {
if (!(eachTag in tagDict)) {
tagDict[eachTag] = [];
}
tagDict[eachTag].push(v);
}
tagDict[topic].push(v);
});
return tagDict;
};
exports.onPreInit = () => {
fs.readdirSync('./src/data').forEach((file) => {
fs.readFile(`./src/data/${file}`, 'utf8', (err, jsonString) => {
if (err) {
console.log('File read failed:', err);
return;
}
jsonFile = JSON.parse(jsonString);
const allQs = getQuestionByTags(
jsonFile,
`all_${file.split('_questions')[0]}`,
);
Object.keys(allQs).forEach(function (v) {
// Write to txt files the questions
var questionOnlyFileStream = fs.createWriteStream(
`./static/${v}_questions.txt`,
);
questionOnlyFileStream.on('error', function (err) {
console.log(err);
});
for (let question of allQs[v]) {
questionOnlyFileStream.write(`${question}\n`);
}
questionOnlyFileStream.end();
// Write to txt file the questions and resources by tags
var questionAndResourceStream = fs.createWriteStream(
`./static/${v}_questions_and_resources.txt`,
);
questionAndResourceStream.on('error', function (err) {
console.log(err);
});
for (let question of allQs[v]) {
questionAndResourceStream.write(`Question: ${question}\n`);
questionAndResourceStream.write(`Comments:\n`);
questionAndResourceStream.write(
`${jsonFile[question].comment}\n`,
);
questionAndResourceStream.write(`Resources:\n`);
for (let eachResource of jsonFile[question].resources) {
questionAndResourceStream.write(`${eachResource}\n`);
}
questionAndResourceStream.write(
`---------------------------------------------\n`,
);
}
questionAndResourceStream.end();
});
});
});
};