/
db_child.js
323 lines (307 loc) · 11.1 KB
/
db_child.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/**
* Copyright (c) 2013-2018 Memba Sarl. All rights reserved.
* Sources at https://github.com/Memba
*/
/* jshint node: true */
'use strict';
var fs = require('fs');
var paths = require('path');
var util = require('util');
// var async = require('async');
var eachSeries = require('async/eachSeries');
var i18n = require('i18n');
var config = require('../config');
var convert = require('./convert');
var github = require('./github');
var logger = require('./logger');
var markdown = require('./markdown');
var utils = require('./utils.es6');
var indexPath = paths.join(__dirname, config.get('db:index'));
var indexDir = paths.dirname(indexPath);
var inProgress = {};
// i18n: it is a child process so it needs to be configured too
i18n.configure({
locales: config.get('locales'), // ['en', 'fr'],
directory: paths.join(__dirname, '../locales'),
objectNotation: true // Use hierarchies in locales.json files
});
/**
* format an index entry (and update Github contents accordingly)
* @param response
* @param callback
*/
function formatIndexEntry(response, callback) {
/**
* Update guthub content with yml head
* @param updateCallback
*/
function update(updateCallback) {
var out = '---\n';
for (var key in head) {
if (head.hasOwnProperty(key) && head[key]) {
out += key + ': ' + head[key] + '\n';
}
}
out += '---\n' + body;
github.updateContent(path, out, sha, updateCallback);
}
/* This function's cyclomatic complexity is too high. */
/* jshint -W074 */
/**
* Add default meta tags to yml head
* @param head
* @returns {*}
*/
function metaFormat() {
/* jshint maxcomplexity: 8 */
if (!head.category) {
head.category = i18n.__('meta.category');
dirty = true;
}
if (!head.description) {
head.description = i18n.__('meta.description');
dirty = true;
}
if (!head.icon) {
head.icon = i18n.__('meta.icon');
dirty = true;
}
if (!head.keywords) {
head.keywords = i18n.__('meta.keywords');
dirty = true;
}
if (!head.language) {
head.language = convert.path2language(path); // i18n.__('locale');
dirty = true;
}
if (!head.title) {
head.title = i18n.__('meta.title');
dirty = true;
}
if (!head.uuid) {
head.uuid = utils.uuid();
dirty = true;
}
}
/* jshint +W074 */
/**
* Read commits to add system data to yml head
* @param systemCallback
*/
function systemFormat(systemCallback) {
/* jscs: disable requireCamelCaseOrUpperCaseIdentifiers */
if (head.author && head.author_url && head.avatar_url && head.creation_date && head.edit_url && head.site_url /*&& yml.update_date*/) {
/* jscs: enable requireCamelCaseOrUpperCaseIdentifiers */
formatted = utils.deepExtend({ text: body }, head);
if (dirty) {
update(function (error, data) {
systemCallback(error, error instanceof Error ? undefined : formatted);
});
} else {
systemCallback(null, formatted);
}
} else {
github.getCommits(path, function (error, commits) {
if (!error && Array.isArray(commits)) {
var first = commits[commits.length - 1];
// last = commits[0];
head.author = first.commit.author.name;
/* jscs: disable requireCamelCaseOrUpperCaseIdentifiers */
head.author_url = first.author.html_url;
head.avatar_url = first.author.avatar_url;
head.creation_date = first.commit.author.date;
head.edit_url = response.html_url;
head.site_url = convert.path2site_url(path, head.creation_date);
// TODO: update_date will never be updated because it will always be found in the yml portion of the github file
// yml.update_date = last.commit.author.date;
/* jscs: enable requireCamelCaseOrUpperCaseIdentifiers */
formatted = utils.deepExtend({ text: body }, head);
// console.dir(formatted);
update(function (error, data) {
systemCallback(error, error instanceof Error ? undefined : formatted);
});
} else {
systemCallback(error || new Error('Getting Github commits for `' + path + '` returned an unexpected value'));
}
});
}
}
var buf = Buffer.from(response.content, 'base64');
var content = buf.toString();
var path = response.path;
// var name = response.name;
var sha = response.sha;
var head = markdown.head(content);
var body = markdown.body(content);
var dirty = false;
var formatted;
metaFormat();
systemFormat(callback);
}
/**
* Exports for mocha testing
*/
module.exports = {
/**
* Get a formatted index entry
* @param path
* @param callback
*/
getIndexEntry: function (path, callback) {
logger.info({
message: 'Indexing ' + path,
module: 'lib/db_child',
method: 'getIndexEntry'
});
if (!convert.isMarkdown(path)) {
return callback(new Error('The path to an index entry should designate a markdown file'));
}
github.getContent(path, function (error, response) {
if (!error && response) {
formatIndexEntry(response, function (error, indexEntry) {
if (!error && indexEntry) {
indexEntry.path = path;
callback(null, indexEntry);
} else {
callback(error);
}
});
} else {
callback(error);
}
});
},
/**
* Build an index array (This is the recursive function)
* @param dir
* @param callback
*/
buildIndex: function (dir, callback) {
github.getContent(dir, function (error, entries) {
if (!error && Array.isArray(entries)) {
var index = [];
// TODO prefer async.each but http://stackoverflow.com/questions/19576601/github-api-issue-with-file-upload
// async.eachSeries(
eachSeries(
entries,
function (entry, eachCallback) {
// Only process markedown files
if (entry.type === 'file' && convert.isMarkdown(entry.path)) {
// process markdown yml for indexation
module.exports.getIndexEntry(entry.path, function (error, indexEntry) {
if (!error && indexEntry) {
index.push(indexEntry);
eachCallback();
} else {
eachCallback(error);
}
});
} else if (entry.type === 'dir') {
// recursive inside directory
module.exports.buildIndex(entry.path, function (error, subindex) {
if (!error && Array.isArray(subindex)) {
index = index.concat(subindex);
eachCallback();
} else {
eachCallback(error);
}
});
} else {
// ignore other types of files
eachCallback();
}
},
function (error) {
if (error) {
callback(error);
} else {
callback(null, index);
}
}
);
} else {
callback(error || new Error('Getting Github contents for `' + dir + '` returned an unexpected value'));
}
});
},
/**
* Create an index on disk (this is the root function)
* @param language
* @param callback
*/
createIndex: function (language, callback) {
var dir = convert.getLanguageDir(language);
var indexFile = util.format(indexPath, language);
logger.debug({
message: 'Building index ' + indexFile,
module: 'lib/db_child',
method: 'createIndex'
});
module.exports.buildIndex(dir, function (error, index) {
if (!error && Array.isArray(index)) {
// Check that directory exists or create
if (!fs.existsSync(indexDir)) {
logger.debug({
message: 'Creating directory ' + indexDir,
module: 'lib/db_child',
method: 'createIndex'
});
fs.mkdirSync(indexDir);
}
// Save index to disk in directory
logger.debug({
message: 'Writing file ' + indexFile,
module: 'lib/db_child',
method: 'createIndex'
// data: index <-- clutters logs
});
fs.writeFile(indexFile, JSON.stringify(index), callback);
} else {
callback(error);
}
});
}
};
/**
* Handler triggered when the worker receives a request to rebuild an index
*/
process.on('message', function (language) {
if (!inProgress[language]) {
inProgress[language] = true;
module.exports.createIndex(language, function (error) {
inProgress[language] = false;
if (error) {
logger.error({
message: 'Error creating index in ' + language,
module: 'lib/db_child',
method: 'process.onmessage',
error: error
});
} else {
logger.info({
message: 'Done creating index in ' + language,
module: 'lib/db_child',
method: 'process.onmessage'
});
}
});
}
});
/**
* Handler triggered when there is an uncaught exception on the child process (not the main one)
*/
process.on('uncaughtException', function (error) {
for (var language in inProgress) {
inProgress[language] = false;
}
try {
logger.critical({
message: 'Uncaught exception',
module: 'lib/db_child',
method: 'process.onuncaughtException',
error: error
});
} catch (exception) {
console.error(exception);
}
});