-
Notifications
You must be signed in to change notification settings - Fork 49
/
index.js
376 lines (302 loc) · 11.6 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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
'use strict';
var fs = require('fs-extra');
var extend = require('extend');
var deepExtend = require('deep-extend');
var path = require('path');
var extendTillSpec = require(path.join(global.pathToApp,'core/lib/extendTillSpec'));
var unflatten = require(path.join(global.pathToApp,'core/unflat'));
var specUtils = require(path.join(global.pathToApp,'core/lib/specUtils'));
var utils = require(path.join(global.pathToApp,'core/lib/utils'));
var coreOpts = global.opts.core;
var prettyHrtime = require('pretty-hrtime');
var busy = false;
var config = {
includedDirs: coreOpts.common.includedDirs,
excludedDirs: [],
// TODO: merge with `excludedDirs` in next major release.
excludedDirsGlobal: [],
cron: false,
cronProd: true,
cronRepeatTime: 60000,
outputFile: path.join(global.pathToApp, 'core/api/data/pages-tree.json'),
specsRoot: path.join(global.pathToApp, coreOpts.common.pathToUser).replace(/\\/g, '/'),
busyTimeout: 300,
thumbnail: 'thumbnail.png'
};
// Overwriting base options
utils.extendOptions(config, coreOpts.fileTree);
var normalizedPathToApp = global.pathToApp.replace(/\\/g, '/');
var prepareExcludesRegex = function(){
var dirsForRegExp = '';
var i = 1;
config.excludedDirs.forEach(function (exlDir) {
if (i < config.excludedDirs.length) {
dirsForRegExp = dirsForRegExp + '^' + config.specsRoot + '\/' + exlDir + '|';
} else {
dirsForRegExp = dirsForRegExp + '^' + config.specsRoot + '\/' + exlDir;
}
i++;
});
return new RegExp(dirsForRegExp);
};
/**
* Prepare relative path for web usage (like `/docs/spec`, `/specs/btn`) out of absolute path
*
* @param {String} path - absolute path to Spec directory or Spec file
*
* @returns {String} Return Spec file meta info (used in file-tree.json and in other places)
*/
var getRelativeSpecPath = module.exports.getRelativeSpecPath = function(absolutePath){
var relativeSpecPath;
if (absolutePath.lastIndexOf(config.specsRoot, 0) === 0) {
// If starts with root (specs)
// Cleaning path to specs root folder
relativeSpecPath = absolutePath.replace(config.specsRoot, '');
} else {
// Cleaning path for included folders
relativeSpecPath = absolutePath.replace(normalizedPathToApp, '');
}
return relativeSpecPath;
};
var getSpecLocation = function(specDirOrPath){
// Normalize windows URL and remove trailing slash
var _specDirOrPath = specDirOrPath.replace(/\\/g, '/').replace(/\/$/, '');
var isSpecPath = path.extname(_specDirOrPath) !== '';
var specDir;
var specPath;
var relativeSpecPath = getRelativeSpecPath(_specDirOrPath);
if (isSpecPath) {
relativeSpecPath = path.dirname(relativeSpecPath);
}
// Try to get specPath
if (isSpecPath) {
specDir = path.dirname(_specDirOrPath);
specPath = _specDirOrPath;
} else {
specDir = _specDirOrPath;
specPath = specUtils.getSpecFromDir(_specDirOrPath);
}
return {
relativeSpecPath: relativeSpecPath,
isSpecPath: isSpecPath,
specDir: specDir,
specPath: specPath
};
};
/**
* Get spec thumbnail image path
*
* @param {String} specDir - absolute path to spec dir
* @param {String} [customThumbnailPath] - override default relative thumbnail path
*
* @returns {String} Return web url to spec thumbnail or undefined
*/
var getThumbnailPath = module.exports.getThumbnailPath = function(specDir, customThumbnailPath){
if (!specDir) return;
var thumbnail;
var thumbnailPathFromSpecDir = customThumbnailPath || config.thumbnail;
var absoluteThumbNailPath = path.join(specDir, thumbnailPathFromSpecDir).replace(/\\/g, '/');
if (fs.existsSync(absoluteThumbNailPath)) {
thumbnail = path.join(getRelativeSpecPath(specDir), thumbnailPathFromSpecDir);
}
return thumbnail;
};
/**
* Get contents of spec info file
*
* @param {String} specDir - absolute path to spec dir
*
* @returns {Object} Return spec info file object
*/
var getSpecInfoFile = module.exports.getSpecInfoFile = function(specDir){
var infoFileName = coreOpts.common.infoFile;
var infoJsonPath = path.join(specDir, infoFileName);
var infoContents = {};
if (!fs.existsSync(infoJsonPath)) return infoContents;
try {
infoContents = JSON.parse(fs.readFileSync(infoJsonPath, 'utf8'));
} catch (e) {
global.console.warn('Error reading ' + infoFileName + ': ' + infoJsonPath);
infoContents = {
error: 'Cannot parse the file',
path: infoJsonPath
};
}
return infoContents;
};
/**
* Prepares Spec meta object from specs directory or Spec file path
*
* @param {String} specDirOrPath - path to Spec directory or Spec file
*
* @returns {Object} Return Spec file meta info (used in file-tree.json and in other places)
*/
var getSpecMeta = module.exports.getSpecMeta = function(specDirOrPath){
var page = {};
// Check if arg is provided and path exists
if ( !(specDirOrPath && fs.existsSync(specDirOrPath))) return page;
var specLocation = getSpecLocation(specDirOrPath);
var specPath = specLocation.specPath;
var specDir = specLocation.specDir;
var relativeSpecPath = specLocation.relativeSpecPath;
// Remove first slash for ID
page.id = relativeSpecPath.substring(1);
page.url = relativeSpecPath;
// If we have Spec, get additional meta
if (specPath) {
var fileStats = fs.statSync(specPath);
var targetFile = path.basename(specPath);
var d = new Date(fileStats.mtime);
page.lastmod = [d.getDate(), d.getMonth() + 1, d.getFullYear()].join('.') || '';
page.lastmodSec = Date.parse(fileStats.mtime) || '';
page.fileName = targetFile || '';
}
var specInfo = getSpecInfoFile(specDir);
page.thumbnail = getThumbnailPath(specDir, specInfo.thumbnailPath) || false;
// Apply info file contents on top
deepExtend(page, specInfo);
return page;
};
var fileTree = function (workingDir) {
var processingDir = workingDir;
var outputJSON = {};
// Allow to run app without existing specsRoot
if (!fs.existsSync(workingDir) && workingDir === config.specsRoot) {
global.log.warn('Running SourceJS without user dir. This set-up should be used only for running tests.');
processingDir = global.pathToApp;
}
var dirContent = fs.readdirSync(processingDir);
var excludes = prepareExcludesRegex();
// Adding paths to files in array
for (var i = 0; dirContent.length > i; i++) {
dirContent[i] = path.join(processingDir, dirContent[i].replace(/\\/g, '/'));
}
//on first call we add includedDirs
if (processingDir === config.specsRoot) {
config.includedDirs.map(function (includedDir) {
dirContent.push(path.join(normalizedPathToApp, includedDir));
});
}
dirContent.forEach(function(pathToFile) {
// Path is excluded
if (excludes.test(processingDir)) return;
var infoFileName = coreOpts.common.infoFile;
var targetFile = path.basename(pathToFile);
// Normalizing path for windows
pathToFile = path.normalize(pathToFile).replace(/\\/g, '/');
var fileStats = fs.statSync(pathToFile);
if (fileStats.isDirectory()) {
if (config.excludedDirsGlobal.indexOf(targetFile) > -1) return;
// Going deeper
var childObj = fileTree(pathToFile);
if (Object.getOwnPropertyNames(childObj).length !== 0) {
outputJSON[targetFile] = extend(outputJSON[targetFile], childObj);
}
} else if (targetFile.toLowerCase() === infoFileName.toLowerCase()) {
outputJSON['specFile'] = getSpecMeta(processingDir);
}
});
return outputJSON;
};
// function for write file tree json
var writeDataFile = function (data, callback) {
var outputFile = config.outputFile;
callback = typeof callback === 'function' ? callback : function(){};
fs.outputFile(outputFile, JSON.stringify(data, null, 4), function (err) {
if (err) {
global.console.warn('Error writing file tree: ', err);
callback(err);
return;
}
global.log.trace('Pages tree JSON saved to ' + outputFile);
callback();
});
};
// function for updating file tree
var updateFileTree = module.exports.updateFileTree = function (data, unflattenData, callback) {
if (busy) {
setTimeout(function(){
updateFileTree(data, unflattenData, callback);
}, config.busyTimeout);
} else {
busy = true;
var prevData = {};
var dataStoragePath = path.join(global.pathToApp, coreOpts.api.specsData);
callback = typeof callback === 'function' ? callback : function(){};
if (unflattenData) {
data = unflatten(data, { delimiter: '/', overwrite: 'root' });
}
try {
prevData = fs.readJsonFileSync(dataStoragePath);
} catch (e) {
global.log.trace('Reading initial data error: ', e);
global.log.debug('Extending from empty object, as we do not have initial data');
}
var dataToWrite = extendTillSpec(prevData, data);
writeDataFile(dataToWrite, function(){
callback();
setTimeout(function(){
busy = false;
}, config.busyTimeout);
});
}
};
// function for deleting object from file tree
var deleteFromFileTree = module.exports.deleteFromFileTree = function (specID) {
if (busy) {
setTimeout(function(){
deleteFromFileTree(specID);
}, config.busyTimeout);
} else {
fs.readJSON(config.outputFile, function (err, data) {
if (err) return;
busy = true;
var pathSplit = specID.split('/');
var processPath = function (pathArr, obj) {
var pathArrQueue = pathArr.slice(0); // Arr copy
var currentItem = pathArrQueue.shift();
if (currentItem !== '' && obj[currentItem]) {
if (pathArrQueue.length === 0) {
delete obj[currentItem];
}
if (pathArrQueue.length !== 0 && obj[currentItem].toString() === '[object Object]') {
obj[currentItem] = processPath(pathArrQueue, obj[currentItem]);
}
}
return obj;
};
var processedData = processPath(pathSplit, data);
writeDataFile(processedData, function () {
global.log.trace('Deleted object from file tree: ', specID);
setTimeout(function(){
busy = false;
}, config.busyTimeout);
});
});
}
};
// function for update file tree json
var scan = module.exports.scan = function (callback) {
if (busy) {
setTimeout(function(){
scan(callback);
}, config.busyTimeout);
} else {
var start = process.hrtime();
callback = typeof callback === 'function' ? callback : function () {};
writeDataFile(fileTree(config.specsRoot), function(){
callback();
var end = process.hrtime(start);
global.log.debug('Full file-tree scan took: ', prettyHrtime(end));
setTimeout(function(){
busy = false;
}, config.busyTimeout);
});
}
};
// Running writeDataFile by cron
if (config.cron || (global.MODE === 'production' && config.cronProd)) {
setInterval(function () {
scan();
}, config.cronRepeatTime);
}