-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.js
66 lines (50 loc) · 1.45 KB
/
db.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
require('total4');
FUNC.indent = function(count, val) {
var lines = val.split('\n');
var str = '';
var total = Math.abs(count);
var is = false;
for (var i = 0; i < total; i++)
str += '\t';
for (var i = 0; i < lines.length; i++) {
if (count > 0 && lines[i])
lines[i] = str + lines[i];
else if (lines[i].substring(0, total) === str) {
lines[i] = lines[i].substring(total);
is = true;
} else if (lines[i] && !is)
break;
}
return lines.join('\n');
};
PATH.fs.readdir('widgets', function(err, response) {
var arr = [];
var evaluate = function(code) {
var obj = {};
new Function('exports', code)(obj);
return obj;
};
response.wait(function(filename, next) {
PATH.fs.readFile('widgets/' + filename, function(err, response) {
if (err) {
next();
return;
}
var data = {};
data.id = filename.substring(0, filename.length - 5);
response = response.toString('utf8');
var author = response.match(/exports\.author.*?;\n/);
var name = response.match(/exports\.name.*?;\n/);
var preview = response.match(/exports\.preview.*?;\n/);
data.name = name ? evaluate(name[0]).name : '';
data.preview = preview ? evaluate(preview[0]).preview : '';
data.url = 'https://cdn.totaljs.com/cms/' + filename;
data.author = author ? evaluate(author[0]).author : '';
arr.push(data);
next();
});
}, function() {
arr.quicksort('name');
PATH.fs.writeFile('db.json', JSON.stringify(arr, null, '\t'), NOOP);
});
});