-
Notifications
You must be signed in to change notification settings - Fork 57
/
buildDocs.js
100 lines (85 loc) · 2.73 KB
/
buildDocs.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const fs = require('fs');
const nPath = require('path');
/* Path Constants */
const DOCS_FILE = './docs.md';
const sections = [
{
name: 'Creational',
topics: [
{ name: 'abstractFactory', path: './Creational' },
{ name: 'builder', path: './Creational' },
{ name: 'factoryMethod', path: './Creational' },
{ name: 'prototype', path: './Creational' },
{ name: 'singleton', path: './Creational' }
]
},
{
name: 'Structural',
topics: [
{ name: 'adapter', path: './Structural' },
{ name: 'bridge', path: './Structural' },
{ name: 'composite', path: './Structural' },
{ name: 'decorator', path: './Structural' },
{ name: 'facade', path: './Structural' },
{ name: 'flyweight', path: './Structural' },
{ name: 'proxy', path: './Structural' },
]
},
{
name: 'Behavioral',
topics: [
{ name: 'chainOfResponsibility', path: './Behavioral' },
{ name: 'command', path: './Behavioral' },
{ name: 'iterator', path: './Behavioral' },
{ name: 'mediator', path: './Behavioral' },
{ name: 'memento', path: './Behavioral' },
{ name: 'observer', path: './Behavioral' },
{ name: 'state', path: './Behavioral' },
{ name: 'strategy', path: './Behavioral' },
{ name: 'templateMethod', path: './Behavioral' },
{ name: 'visitor', path: './Behavioral' },
]
}
];
/* Utility functions */
const stripPunctuation = (str) =>
str.replace(/["'.,/#!$%^&*;:’{}=_`~()]/g, '');
const reverseCamelCase = (str) => {
return str.replace(/([A-Z])/g, ' $1')
.replace(/^./, function (str) { return str.toUpperCase(); });
};
const dashCase = (str) => {
return stripPunctuation(str).replace(/([A-Z])/g, ' $1')
.replace(/^./, function (str) { return str.toLowerCase(); }).toLowerCase().split(' ')
.filter((word) => word !== '').join('-');
};
/* Writing Markdown */
const writeHeader = (sections) =>
`# Real World Design Patterns using NodeJs APIs
${sections.map(writeSectionContents).join('')}
`;
const writeSectionContents = ({ name, topics }) =>
`**[${reverseCamelCase(name)}](#${dashCase(name)})**
${topics.map(writeTopicContents).join('')}
`;
const writeTopicContents = ({ name }) => `* [${reverseCamelCase(name)}](#${dashCase(name)})
`;
const writeSection = ({ name, topics }) =>
`## ${name}
${topics.map(writeTopic).join('')}
`;
const writeTopic = ({ name, path }) =>
`### ${reverseCamelCase(name)}
${[`${path}/${name}.js`].map(writeFile).join('')}
`;
const writeFile = (path) =>
`##### ${nPath.basename(path)}
\`\`\`Javascript
${fs.readFileSync(path)}
\`\`\`
`;
const writeDocument = () =>
`${writeHeader(sections)}
${sections.map(writeSection).join('')}
`;
fs.writeFileSync(DOCS_FILE, writeDocument());