forked from fbeline/design-patterns-JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-docs.js
164 lines (126 loc) · 4.26 KB
/
build-docs.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const nPath = require('path');
const fs = require('fs-extra-promise');
/* Path Constants */
const SOURCE_DIR = './src';
const TEST_DIR = './test';
const DOCS_FILE = './docs.md';
/* Data Storage */
const sections = [];
// A map of file paths to contents
// e.g. { 'src/ex.js' : 'console.log("test")' }
const files = new Map();
/* Building Data Types */
const makeSection = ( name ) => ({
name,
path: `${SOURCE_DIR}/${name}`,
topics: [],
});
const makeTopic = ( sectionName ) => ( name ) => ({
name,
path: `${SOURCE_DIR}/${sectionName}/${name}`,
files: [],
tests: [],
});
/* Utility functions */
const getFileContents = ( path ) => fs.existsAsync( path )
.then( exists => exists ? fs.readFileAsync( path, 'utf-8' ) : '' );
const stripPunctuation = ( str ) =>
str.replace( /["'.,\/#!$%\^&\*;:’{}=_`~()]/g, '' );
const dashCase = ( str ) => stripPunctuation( str ).toLowerCase().split( ' ' ).filter(
( word ) => word !== ''
).join( '-' );
const reverseDashCase= (str) => str.split('-').map(
word => word.replace(
/(?:^\w|[a-z]|\b\w)/g,
( letter, index ) =>
index == 0 ?
letter.toUpperCase() :
letter.toLowerCase()
)
).join(' ');
/* Writing Markdown */
const writeDocument = () =>
`${writeHeader(sections)}
${sections.map( writeSection ).join('')}
`;
const writeHeader = ( sections ) =>
`# Design Patterns JS
${sections.map( writeSectionContents ).join('')}
`;
const writeSectionContents = ({ name, topics }) =>
`**[${reverseDashCase(name)}](#${dashCase(name)})**
${topics.map( writeTopicContents ).join('')}
`;
const writeTopicContents = ({ name }) => `* [${reverseDashCase(name)}](#${dashCase(name)})
`;
const writeSection = ({ name, path, topics }) =>
`## ${name}
${topics.map( writeTopic ).join('')}
`;
const writeTopic = ({ name, path, files, tests }) =>
`### ${reverseDashCase(name)}
${files.map( writeFile ).join('')}
`;
const writeFile = ( path ) =>
`##### ${nPath.basename(path)}
\`\`\`Javascript
${files.get(path)}
\`\`\`
`;
/* File System Actions */
fs.readdirAsync( SOURCE_DIR )
.then( sectionNames => {
// Make sections from section names and store.
sections.push( ...sectionNames.map( makeSection ) );
// Get topics of each section.
return Promise.all( sections.map(
({ path }) => fs.readdirAsync( path )
) );
} )
.then( topicsBySection => {
// Set topics of each section.
topicsBySection.forEach(
( topics, i ) => sections[i].topics = topics.map( makeTopic( sections[i].name ) )
);
// Get the files inside of each topic
return Promise.all( sections.map(
section => Promise.all( section.topics.map(
({ path }) => fs.readdirAsync( path )
) )
) );
} )
.then( filesByTopicBySection => {
filesByTopicBySection.forEach(
( filesByTopic, sectionIndex ) => filesByTopic.forEach(
( topicFiles, topicIndex ) => {
const { path, topics } = sections[sectionIndex];
const topic = topics[topicIndex];
// Store files in each topic
topic.files = topicFiles.map(
name => `${path}/${topic.name}/${name}`
);
// Store test files for each file
topic.tests = topicFiles.map(
path => `${TEST_DIR}/${nPath.basename(path,'.js')}-test.js`
);
// Give each file a default value in the file map.
[ ...topic.files, ...topic.tests ].forEach(
filePath => files.set( filePath, '' )
);
}
)
);
// Create array of paths.
const filePaths = [ ...files.keys() ];
// Get contents of each file.
return Promise.all( filePaths.map( getFileContents ) );
} )
.then( fileContents => {
// Create array of paths.
const filePaths = [ ...files.keys() ];
fileContents.forEach(
( contents, i ) => files.set( filePaths[i], contents )
);
return fs.outputFileAsync( DOCS_FILE, writeDocument() )
} )
.catch( error => { throw error; } )