forked from VGraupera/1on1-questions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (71 loc) · 2.15 KB
/
index.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
72
73
74
75
76
77
78
79
// Generate README.md file based on JSON data
const fs = require('fs');
const questions = require('./questions.json');
function sortQuestions(a, b) {
// case insensitive sort
const qA = a.question.toUpperCase();
const qB = b.question.toUpperCase();
if (qA < qB) {
return -1;
}
if (qA > qB) {
return 1;
}
return 0;
}
function stringToSlug(string) {
return string
.toLowerCase()
.replace(/ /g, '-')
.replace(/[^\w-]+/g, '');
}
function categoryMapReducer(accumulator, item) {
if (accumulator[item.category]) {
accumulator[item.category] = [...accumulator[item.category], item];
} else {
accumulator[item.category] = [item];
}
return accumulator;
};
function questionSectionReducer(accumulator, [category, items]) {
return [
...accumulator,
`\n## ${category}`,
...items.sort(sortQuestions).map((item) => `* ${item.question}`)
];
}
function tableOfContentsReducer(accumulator, category) {
return [
...accumulator,
`\n- [${category}](#${stringToSlug(category)})`
];
}
const title = `# 1 on 1 Meeting Questions
Mega list compiled from a variety to sources. Also available here: http://www.managersclub.com/mega-list-of-1-on-1-meeting-questions/`;
const faq = `
## FAQ
Why is there also a JSON file?
- It can be directly consumed by apps
- README.md can be generated from json file so you only have to make changes in one place`;
const contributing = `
## Contributing
1. Fork repo
2. Add your question to \`questions.json\`.
3. Create new Pull Request
You can update the README manually running \`npm start\` but there is GitHub action that will automatically update the README with your questions.
`;
const categoryMap = questions.reduce(categoryMapReducer, {});
const questionsBySection = Object.entries(categoryMap).reduce(questionSectionReducer, []);
const tableOfContents = Object.keys(categoryMap).reduce(tableOfContentsReducer, ['\n## Table of Contents']).join('');
const content = [
title,
tableOfContents,
...questionsBySection,
faq,
contributing
];
// create README file
fs.writeFile('./README.md', content.join('\n'), function (err) {
if (err) throw err;
console.log('Updated README.md');
});