-
Notifications
You must be signed in to change notification settings - Fork 18
/
exampleDataFactory.js
241 lines (205 loc) · 5.49 KB
/
exampleDataFactory.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
const path = require('path');
const fs = require('fs-extra'); // to create folder and file
const mkdirp = require('mkdirp'); // to create folder
const nodedir = require('node-dir'); // to read files
const copydir = require('copy-dir'); // to copy folder
const directoryExists = require('directory-exists');
const cheerio = require('cheerio');
const Prism = require('prismjs');
const pwd = process.cwd();
const config = require(path.resolve(pwd, 'tuidoc.config.json'));
const examples = config.examples || false;
const { filePath, titles, globalErrorLogVariable = false } = examples;
const EXAMPLE_FILES_PATH = path.resolve(pwd, filePath || '');
const COPY_FILES_PATH = path.resolve(__dirname, `../static`);
const DATA_FILES_PATH = path.resolve(__dirname, `../src/data/examplePage`);
const extenralFolders = ['dist'];
const navItems = [];
const searchItems = [];
/**
* Get file name via file name of pull path
* @param {string} longname - file name of full path
* @returns {string} splited name
*/
function getFileName(longname) {
return path.parse(longname).base.split('.').shift();
}
/**
* Get pid (foramt: tutorial-foo)
* @param {string} filename - file name to create pid
* @returns {string} pid
*/
function getPid(filename) {
return `tutorial-${filename}`;
}
/**
* Get title
* @param {string} filename - file name to create title
* @returns {string} title
*/
function getTitle(filename) {
if (titles) {
return titles[filename] || filename;
}
return '';
}
/**
* Parse content of each file
* @param {string} content - file's content
* @returns {object} parsed content data using cheerio
*/
function parseContent(content) {
const $ = cheerio.load(content, {
decodeEntities: false,
normalizeWhitespace: false
});
return {
js: $('script.code-js').html(),
html: $('div.code-html').html()
};
}
/**
* Make data of example page
* @param {string} parsedContent - parsed content
* @param {string} longname - file name of full path
* @returns {object} data
*/
function makeExamplePageData(parsedContent, longname) {
const { js, html } = parsedContent;
const codeJs = js ? Prism.highlight(js, Prism.languages.javascript, 'javascript') : '';
const codeHtml = html ? Prism.highlight(html, Prism.languages.html, 'html') : '';
const filename = getFileName(longname);
return {
pid: getPid(filename),
title: getTitle(filename),
codeJs,
codeHtml
};
}
/**
* Make json file to using in example page
* @param {object} data - data to make file
*/
function makeExamplePageDataFile(data) {
mkdirp(DATA_FILES_PATH, (err) => {
if (err) {
throw err;
}
fs.writeFileSync(`${DATA_FILES_PATH}/${data.pid}.json`, JSON.stringify(data, null, 2));
});
}
/**
* Post processing for example files
*/
function postProcessingOfExampleFiles() {
nodedir.readFiles(
COPY_FILES_PATH,
{
match: /.html$/,
recursive: true
},
(err, content, filename, next) => {
if (err) {
throw err;
}
const parsedContent = parseContent(content);
const data = makeExamplePageData(parsedContent, filename);
makeExamplePageDataFile(data);
if (globalErrorLogVariable) {
injectScriptForErrorCatch(content, filename);
}
next(); // read next file
}
);
}
/**
* Copy example files to static folder
*/
function copyExampleFiles() {
copydir(EXAMPLE_FILES_PATH, `${COPY_FILES_PATH}/examples`, (err) => {
if (err) {
throw err;
}
postProcessingOfExampleFiles();
});
}
/**
* Inject script for error catch
* @param {string} content - example page html string
* @param {string} filename - exmaple page's filename
*/
function injectScriptForErrorCatch(content, filename) {
const injectVariable =
typeof globalErrorLogVariable === 'string' ? globalErrorLogVariable : 'errorLogs';
const injectScriptString = `var ${injectVariable}=[];window.onerror=function(o,r,e,n){${injectVariable}.push({message:o,source:r,lineno:e,colno:n})};`;
const newContent = content.replace(
/(\n?)(\s*)(<\/head>)/i,
`$1$2$2<script>${injectScriptString}</script>$1$2$3`
);
fs.writeFileSync(filename, newContent, { encoding: 'utf-8' });
}
/**
* Copy external files to folder
*/
function copyExternalFiles() {
extenralFolders.forEach((folder) => {
const copyPath = path.resolve(pwd, folder);
directoryExists(copyPath, (error, result) => {
if (!result) {
return;
}
copydir(copyPath, `${COPY_FILES_PATH}/${folder}`, (err) => {
if (err) {
throw err;
}
});
});
});
}
/**
* Make data of navigation and search keywords
*/
function makeNavAndSearchData() {
const files = nodedir.files(EXAMPLE_FILES_PATH, { sync: true });
files.forEach((file) => {
if (file.match(/.html$/)) {
const filename = getFileName(file);
const pid = getPid(filename);
const name = getTitle(filename);
navItems.push({
pid,
name,
parentPid: 'example',
type: 'example'
});
searchItems.push({
pid,
name,
parentPid: 'example'
});
}
});
}
function createData() {
fs.emptyDirSync(COPY_FILES_PATH);
if (examples) {
copyExampleFiles();
copyExternalFiles();
makeNavAndSearchData();
} else {
// make dummy file for graphql
makeExamplePageDataFile({
pid: 'tutorial-dummy',
title: '',
codeJs: '',
codeHtml: ''
});
}
return {
navigation: navItems,
searchKeywords: searchItems
};
}
module.exports = {
createData
};