-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.js
401 lines (370 loc) · 14.1 KB
/
gen.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
var fs = require('fs-extra');
var path = require('path');
var ejs = require('ejs');
var ncp = require('ncp').ncp;
var request = require('request');
var marked = require('marked');
var siteDir = '../ehouais.github.io';
var toolsDir = siteDir+'/tools'
var templatesDir = './templates';
var cipherKeyStorageId = 'cipherKey';
var props = function(obj, cb) {
Object.keys(obj).forEach(function(key) {
cb(key, obj[key]);
});
};
var cdnjs = function(path) {
return '//cdnjs.cloudflare.com/ajax/libs'+path;
};
var datalibVersion = 'v0.10.0';
var datalib = 'js-data-libs/'+datalibVersion;
var uiutilslibVersion = 'v0.4.2';
var uiutilslib = 'js-ui-utils/'+uiutilslibVersion;
// Start from scratch
console.log('Emptying destination folder...');
fs.readdirSync(siteDir).forEach(function(name) {
if (name != '.git') {
fs.removeSync(siteDir+'/'+name);
}
})
// Assets
var assetsDir = siteDir+'/assets';
var assets = [];
console.log('Copying assets...');
fs.copySync('assets', assetsDir);
var fetchAsset = function(filepath, forceSource) {
if (assets.indexOf(filepath) != -1) {
return;
}
assets.push(filepath);
var source = forceSource || 'https://raw.githubusercontent.com/ehouais/'+filepath;
fs.ensureDirSync(path.dirname(assetsDir+'/'+filepath));
console.log('Fetching asset "'+source+'"...');
if (source.substr(0, 4) == 'http') {
request(source, function (error, response, body) {
if (error) { console.log(error); return false }
fs.writeFile(assetsDir+'/'+filepath, body, function(err) {
if (err) { console.log(err); return false }
return true;
});
});
} else {
fs.copy(source, assetsDir+'/'+filepath);
}
};
// charts
//fetchAsset('js-data-libs/'+datalibVersion+'/mlab.js', '../js-data-libs/mlab.js');
var chartsDir = toolsDir+'/charts';
var chartTemplate = templatesDir+'/chart.ejs';
var chartslib = 'charts/v0.3.2';
fs.ensureDirSync(chartsDir);
fs.readFile(chartTemplate, 'utf8', function(err, data) {
if (err) { console.log(err); return false }
var template = ejs.compile(data, {filename: 'dummy'});
var libs = {
d3: cdnjs('/d3/4.2.8/d3.min'),
leaflet: cdnjs('/leaflet/1.0.3/leaflet'),
'snap.svg': cdnjs('/snap.svg/0.5.1/snap.svg-min'),
timescale: chartslib+'/timescale',
twopassresize: chartslib+'/twopassresize',
datatable: datalib+'/datatable',
};
var charts = {
diagram: {
stylesheets: ['../condensed-font.css'],
requirements: ['snap.svg', 'twopassresize', 'ext_parser'],
exports: {ext_parser: 'parser'}
},
hbars: {
stylesheets: ['../condensed-font.css', 'default.css'],
requirements: ['d3', 'twopassresize', 'datatable']
},
lines: {
stylesheets: ['../condensed-font.css', 'default.css'],
requirements: ['d3', 'timescale', 'twopassresize', 'datatable']
},
map: {
stylesheets: ['/leaflet/1.0.3/leaflet.css', 'default.css'],
requirements: ['leaflet', 'datatable']
},
pie: {
stylesheets: ['../condensed-font.css', 'default.css'],
requirements: ['d3', 'twopassresize', 'datatable']
},
timeline: {
stylesheets: ['../condensed-font.css', 'default.css'],
requirements: ['d3', 'timescale', 'twopassresize', 'ext_parser'],
exports: {ext_parser: 'parser'}
},
vbars: {
stylesheets: ['../condensed-font.css', 'default.css'],
requirements: ['d3', 'twopassresize', 'datatable']
}
};
console.log('Generating charts...');
props(charts, function(id, chart) {
var html;
var config = {
baseUrl: '/assets',
paths: {
http: datalib+'/http',
text: cdnjs('/require-text/2.0.12/text.min'),
'gist': datalib+'/gist',
'mlab': datalib+'/mlab',
tabletop: cdnjs('/tabletop.js/1.5.2/tabletop.min'),
'on-demand': datalib+'/on-demand',
sjcl: cdnjs('/sjcl/1.0.6/sjcl.min'),
chart: chartslib+'/'+id+'/chart',
datasource: datalib+'/datasource'
},
shim: {
sjcl: {
exports: 'sjcl'
}
}
};
fetchAsset(datalib+'/http.js');
fetchAsset(datalib+'/gist.js');
fetchAsset(datalib+'/mlab.js');
fetchAsset(datalib+'/on-demand.js');
fetchAsset(chartslib+'/'+id+'/chart.js');
fetchAsset(datalib+'/datasource.js');
(chart.requirements || []).forEach(function(require) {
var path = libs[require] || chartslib+'/'+id+'/'+require;
config.paths[require] = path;
if (path.substr(0, 2) != '//') {
fetchAsset(path+'.js');
}
});
props(chart.exports || {}, function(id, exp) {
config.shim[id] = {exports: exp};
});
html = template({
stylesheets: chart.stylesheets.map(function(filepath) {
var apath;
if (filepath.substr(0, 2) == '..') {
apath = chartslib+'/'+filepath.substr(3);
} else {
apath = chartslib+'/'+id+'/'+filepath;
}
fetchAsset(apath);
return '/assets/'+apath;
}),
config: config,
type: id,
cipherKeyStorageId: cipherKeyStorageId
});
fs.writeFile(chartsDir+'/'+id+'.html', html, function(err) {
if (err) { console.log(err); return false }
return true;
});
});
});
// webviews
var webviewTemplate = templatesDir+'/webview.ejs';
var webviewslib = 'webviews/v0.5.0';
(function() {
var libs = {
tablesort: {
url: cdnjs('/tablesort/5.0.0/tablesort.min'),
exports: 'Tablesort'
},
'ui-utils': {
url: uiutilslib+'/ui-utils'
}
};
var webviews = {
dashboard: 'index',
notepad: 'index',
tabs: {
stylesheets: ['style.css'],
requirements: ['ui-utils']
},
table: {
stylesheets: ['style.css'],
requirements: ['tablesort']
},
};
fs.readFile(webviewTemplate, 'utf8', function(err, data) {
if (err) { console.log(err); return false }
var template = ejs.compile(data, {filename: 'dummy'});
console.log('Generating webviews...');
fs.ensureDirSync(toolsDir);
props(webviews, function(id, webview) {
var html,
destPath = toolsDir+'/'+id+'.html';
if (id == 'notepad' || id == 'dashboard') {
fetchAsset(uiutilslib+'/overlay.js');
fetchAsset(uiutilslib+'/form.js');
ejs.renderFile(templatesDir+'/webviews.ejs', {
datalibVersion,
cipherKeyStorageId: cipherKeyStorageId
}, {}, function(err, str) {
var url = 'https://raw.githubusercontent.com/ehouais/'+webviewslib+'/'+id+'/index.html';
console.log('Fetching "'+url+'"...');
request(url, function (error, response, body) {
if (error) { console.log(error); return false }
html = body.replace('<script src="../webviews.js"></script>', str)
fs.writeFile(destPath, html, function(err) {
if (err) { console.log(err); return false }
return true;
});
});
});
} else {
var config = {
baseUrl: '/assets',
paths: {
http: datalib+'/http',
text: cdnjs('/require-text/2.0.12/text.min'),
sjcl: cdnjs('/sjcl/1.0.6/sjcl.min'),
'gist': datalib+'/gist',
'mlab': datalib+'/mlab',
'on-demand': datalib+'/on-demand',
renderer: webviewslib+'/'+id+'/renderer',
datasource: datalib+'/datasource'
},
shim: {
sjcl: {
exports: 'sjcl'
}
}
};
fetchAsset(datalib+'/http.js');
fetchAsset(datalib+'/gist.js');
fetchAsset(datalib+'/mlab.js');
fetchAsset(datalib+'/on-demand.js');
fetchAsset(webviewslib+'/'+id+'/renderer.js');
fetchAsset(datalib+'/datasource.js');
(webview.requirements || []).forEach(function(require) {
var info = libs[require];
config.paths[require] = info.url;
if (info.url.substr(0, 2) != '//') {
fetchAsset(info.url+'.js');
}
if (info.exports) {
config.shim[require] = {exports: info.exports};
}
});
props(webview.exports || {}, function(id, exp) {
config.shim[id] = {exports: exp};
});
html = template({
stylesheets: webview.stylesheets.map(function(filepath) {
var apath;
if (filepath.substr(0, 2) == '..') {
apath = webviewslib+'/'+filepath.substr(3);
} else {
apath = webviewslib+'/'+id+'/'+filepath;
}
fetchAsset(apath);
return '/assets/'+apath;
}),
config: config,
type: id,
cipherKeyStorageId: cipherKeyStorageId
});
fs.writeFile(destPath, html, function(err) {
if (err) { console.log(err); return false }
return true;
});
}
});
});
})();
// js1k
var js1kDir = siteDir+'/js1k';
var js1kTemplate = templatesDir+'/js1k.ejs';
var js1klib = 'js1k/6470cd8';
fs.mkdir(js1kDir, function() {
fs.readFile(js1kTemplate, 'utf8', function(err, data) {
if (err) { console.log(err); return false }
var template = ejs.compile(data);
console.log('Generating JS1k demos...');
[
'2012-Mine[love]craft',
'2013-Strange_crystals_II',
'2014-Buggy_island',
'2015-Islands',
'2016-Gliese_581_c'
].forEach(function(id) {
var path = js1klib+'/'+id+'/plain.js';
html = template({
demo: id,
url: '/assets/'+path
});
fetchAsset(path);
fs.writeFile(js1kDir+'/'+id+'.html', html, function(err) {
if (err) { console.log(err); return false }
return true;
});
});
});
});
// apps
// playground, selfservice, page generator ?
/*var appsDir = siteDir+'/apps';
fs.mkdir(appsDir);*/
// posts
var postTemplate = templatesDir+'/post.ejs',
postSource = 'https://raw.githubusercontent.com/ehouais/blog-posts/master',
getAllMatches = function(regx, string) {
var match,
matches = [];
while (match = regx.exec(string)) {
matches.push(match);
}
return matches
};
fs.readFile(postTemplate, 'utf8', function(err, data) {
var template = ejs.compile(data);
request(postSource+'/nav.md', function (error, response, md) {
if (error) { console.log(error); return false }
var nav = marked(md);
console.log('Generating blog posts...');
getAllMatches(RegExp('href="/([0-9]{4}/[^"]+)?"', 'g'), nav).forEach(function(match) {
var shortpath = '/'+(match[1] || 'index');
var postUrl = postSource+shortpath+'.md';
console.log('Fetching post "'+postUrl+'"...');
request(postUrl, function(error, response, md) {
var filepath = siteDir+shortpath+'.html';
fs.ensureDirSync(path.dirname(filepath));
fs.writeFileSync(filepath, template({
nav: nav,
post: marked(md)
.replace(RegExp('/cdn/', 'g'), '/assets/')
.replace(RegExp('//ehouais.net/blog/wp-content/uploads/[^"]+/', 'g'), '/assets/img/')
}));
});
});
});
});
// database GUI
var dbTemplate = templatesDir+'/db.ejs';
var dbFile = toolsDir+'/db.html';
fetchAsset(datalib+'/observable.js');
fetchAsset(datalib+'/streams.js');
fetchAsset(datalib+'/mlab.js');
fetchAsset(datalib+'/on-demand.js');
fs.readFile(dbTemplate, 'utf8', function(err, data) {
console.log('Generating DB app...');
fs.ensureDirSync(toolsDir);
fs.writeFile(dbFile, ejs.render(data, {
datalibVersion,
uiutilslibVersion,
cipherKeyStorageId: cipherKeyStorageId
}), function(err) {
if (err) { console.log(err); return false }
return true;
});
});
// 404.html
var notfoundTemplate = templatesDir+'/404.ejs';
var notfoundFile = siteDir+'/404.html';
fs.readFile(notfoundTemplate, 'utf8', function(err, data) {
console.log('Generating 404.html file...');
fs.writeFile(notfoundFile, data, function(err) {
if (err) { console.log(err); return false }
return true;
});
});