forked from fossasia/open-event-wsgen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generator.js
426 lines (385 loc) · 17.9 KB
/
generator.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
'use strict';
var exports = module.exports = {};
var logger = require('./buildlogger.js');
var gulp = require('./gulpfile.js');
const fs = require('fs-extra');
const handlebars = require('handlebars');
const async = require('async');
const archiver = require('archiver');
const sass = require('node-sass');
const jsonfile = require('jsonfile');
const minify = require('html-minifier').minify;
const distHelper = require(__dirname + '/dist.js');
const fold = require(__dirname + '/fold.js');
const mailer = require('./mailer');
const ftpDeployer = require('./ftpdeploy');
const navbar = handlebars.compile(fs.readFileSync(__dirname + '/templates/partials/navbar.hbs').toString('utf-8'));
const footer = handlebars.compile(fs.readFileSync(__dirname + '/templates/partials/footer.hbs').toString('utf-8'));
const scroll = handlebars.compile(fs.readFileSync(__dirname + '/templates/partials/scroll.hbs').toString('utf-8'));
const subnavbar = handlebars.compile(fs.readFileSync(__dirname + '/templates/partials/subnavbar.hbs').toString('utf-8'));
const social = handlebars.compile(fs.readFileSync(__dirname + '/templates/partials/social.hbs').toString('utf-8'));
handlebars.registerPartial('navbar', navbar);
handlebars.registerPartial('footer', footer);
handlebars.registerPartial('scroll', scroll);
handlebars.registerPartial('subnavbar', subnavbar);
handlebars.registerPartial('social', social);
const tracksTpl = handlebars.compile(fs.readFileSync(__dirname + '/templates/tracks.hbs').toString('utf-8'));
const scheduleTpl = handlebars.compile(fs.readFileSync(__dirname + '/templates/schedule.hbs').toString('utf-8'));
const roomstpl = handlebars.compile(fs.readFileSync(__dirname + '/templates/rooms.hbs').toString('utf-8'));
const speakerstpl = handlebars.compile(fs.readFileSync(__dirname + '/templates/speakers.hbs').toString('utf-8'));
const eventtpl = handlebars.compile(fs.readFileSync(__dirname + '/templates/event.hbs').toString('utf-8'));
if (!String.linkify) {
String.prototype.linkify = function() {
var urlPattern = /\b(?:https?|ftp):\/\/[a-z0-9-+&@#\/%?=~_|!:,.;]*[a-z0-9-+&@#\/%=~_|]/gim;
var pseudoUrlPattern = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
var emailAddressPattern = /[\w.]+@[a-zA-Z_-]+?(?:\.[a-zA-Z]{2,6})+/gim;
return this
.replace(urlPattern, '<a href="$&">$&</a>')
.replace(pseudoUrlPattern, '$1<a href="http://$2">$2</a>')
.replace(emailAddressPattern, '<a href="mailto:$&">$&</a>');
};
}
handlebars.registerHelper('linkify', function(options) {
var content = options.fn(this);
return new handlebars.SafeString(content.linkify());
});
handlebars.registerHelper('ifvalue', function (conditional, options) {
if (conditional !== options.hash.notequals){
return options.fn(this);
}
else{
return options.inverse(this);
}
});
function minifyHtml(file) {
var result = minify(file, {
removeAttributeQuotes: true,
minifyCSS: true,
minifyJS: true,
minifyURLs: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true
});
return result;
}
function transformData(sessions, speakers, event, sponsors, tracksData, roomsData, reqOpts, next) {
fold.foldByTrack(sessions, speakers, tracksData, reqOpts, function(tracks) {
const days = fold.foldByDate(tracks);
const sociallinks = fold.createSocialLinks(event);
fold.extractEventUrls(event, speakers, sponsors, reqOpts, function(eventurls){
const copyright = fold.getCopyrightData(event);
fold.foldByLevel(sponsors, reqOpts, function(sponsorpics){
const roomsinfo = fold.foldByRooms(roomsData, sessions, speakers, tracksData);
fold.foldBySpeakers(speakers, sessions, tracksData, reqOpts, function(speakerslist){
const apptitle = fold.getAppName(event);
const timeList = fold.foldByTime(sessions, speakers, tracksData);
const metaauthor = fold.getOrganizerName(event);
const tracknames = fold.returnTracknames(sessions, tracksData);
next({
tracks, days, sociallinks,
eventurls, copyright, sponsorpics,
roomsinfo, apptitle, speakerslist, timeList, metaauthor, tracknames
});
});
});
});
});
}
function getJsonData(reqOpts, next) {
try {
const appFolder = reqOpts.email + '/' + fold.slugify(reqOpts.name);
const distJsonsPath = distHelper.distPath + '/' + appFolder + '/json';
const sessionsData = jsonfile.readFileSync(distJsonsPath + '/sessions');
const speakersData = jsonfile.readFileSync(distJsonsPath + '/speakers');
const eventData = jsonfile.readFileSync(distJsonsPath + '/event');
const sponsorsData = jsonfile.readFileSync(distJsonsPath + '/sponsors');
const tracksData = jsonfile.readFileSync(distJsonsPath + '/tracks');
const roomsData = jsonfile.readFileSync(distJsonsPath + '/microlocations');
return transformData(sessionsData, speakersData, eventData, sponsorsData, tracksData, roomsData, reqOpts, function(data) {
next(null, data);
});
} catch (err) {
return next(err);
}
}
exports.stopBuild = function(socket){
if (statusMap[socket.connId]) {
statusMap[socket.connId] = false;
}
else{
socket.emit('Cancel_Build');
}
}
exports.enableBuild = function(socket){
statusMap[socket.connId] = true;
}
exports.uploadJsonZip = function(fileData, socket) {
distHelper.uploadWithProgress(fileData.singlefileUpload, fileData.zipLength, socket)
};
exports.finishZipUpload = function(file, id) {
console.log('=============================ZIP SAVED\n');
console.log(file.base);
console.log(file.pathName);
distHelper.moveZip(file.pathName, id);
};
exports.startZipUpload = function(id) {
console.log('========================ZIP UPLOAD START\n\n');
distHelper.makeUploadsDir(id);
distHelper.cleanUploads(id);
};
exports.createDistDir = function(req, socket, callback) {
console.log(req.body);
// since we don't give the name of the app, we use a dummy value 'tempProject' in place of it
req.body.name = 'tempProject' + socket.connId; // temporary name for the project till the time we get the actual name of the event
const theme = req.body.theme || 'light';
var appFolder = req.body.email + '/' + fold.slugify(req.body.name);
let emit = false;
if (socket.constructor.name == 'Socket') {
emit = true;
}
// the below variable will store the actual name of the event
var eventName;
async.series([
(done) => {
console.log('================================CLEANING TEMPORARY FOLDERS\n');
logger.addLog('Info', 'Cleaning up the previously existing temporary folders', socket);
if (emit) socket.emit('live.process', {donePercent: 5, status: "Cleaning temporary folder"});
fs.remove(distHelper.distPath + '/' + appFolder, (err) => {
if(err !== null) {
logger.addLog('Error', 'Failed to clean up the previously existing temporary folders', socket, err);
console.log(err);
}
logger.addLog('Success', 'Successfully cleaned up the temporary folders', socket);
done(null, 'clean');
});
},
(done) => {
console.log('================================MAKING\n');
logger.addLog('Info', 'Making the dist folder', socket);
if (emit) socket.emit('live.process', {donePercent: 10, status: "Making dist folder" });
distHelper.makeDistDir(appFolder, socket);
done(null, 'make');
},
(done) => {
if (emit) socket.emit('live.process', { donePercent: 20, status: "Copying assets" });
logger.addLog('Info', 'Copying Assets', socket);
distHelper.copyAssets(appFolder, (copyerr) => {
console.log('================================COPYING\n');
if (copyerr !== null) {
console.log(copyerr);
logger.addLog('Error', 'Error occured while copying assets into the appFolder', socket, copyerr);
return socket.emit('live.error', {donePercent: 30, status: "Error in Copying assets" });
}
logger.addLog('Success', 'Assets were successfully copied', socket);
done(null, 'copy');
});
},
(done) => {
if (emit) socket.emit('live.process', { donePercent: 40, status: "Cleaning dependencies folder" });
logger.addLog('Info', 'Cleaning dependencies folder created as a part of copying assets inside the appFolder', socket);
distHelper.removeDependency(appFolder, socket, (copyerr) => {
console.log('============================Moving contents from dependency folder and deleting the dependency folder');
if (copyerr !== null) {
logger.addLog('Error', 'Error while reading directory', socket, copyerr);
console.log(copyerr);
return socket.emit('live.error', {donePercent: 45, status: "Error in moving files from dependency folder" });
}
return gulp.minifyJs(distHelper.distPath + '/' + appFolder, function() {
logger.addLog('Success', 'Dependencies folder cleaned successfully', socket);
return done(null, 'move');
})
});
},
(done) => {
console.log('================================COPYING JSONS\n');
logger.addLog('Info', 'Copying Jsons', socket);
if (emit) socket.emit('live.process', {donePercent: 50, status: "Copying the JSONs" });
switch (req.body.datasource) {
case 'jsonupload':
logger.addLog('Info','Jsons have been uploaded by the user', socket);
distHelper.copyUploads(appFolder, socket, function(err) {
if(err) {
console.log(err);
done(err);
}
done(null, 'copyUploads');
});
break;
case 'eventapi':
console.log('================================FETCHING JSONS\n');
logger.addLog('Info', 'Fetching Jsons from the internet', socket);
distHelper.fetchApiJsons(appFolder, req.body.apiendpoint, socket, (err) => {
if(err !== null) {
console.log(err);
}
logger.addLog('Success', 'All jsons have been successfully downloaded', socket);
done(null, 'fetchApiJsons');
});
break;
case 'mockjson':
default:
distHelper.copyMockJsons(appFolder);
done(null, 'cleanuploads');
break;
}
},
(done) => {
console.log('===============================COMPILING SASS\n');
if (emit) socket.emit('live.process', {donePercent: 60, status: "Compiling the SASS files" });
sass.render({
file: __dirname + '/_scss/_themes/_' + theme + '-theme/_' + theme + '.scss',
outFile: distHelper.distPath + '/' + appFolder + '/css/schedule.css'
}, function(err, result) {
if (!err) {
logger.addLog('Success', 'SASS file compiled successfully', socket);
fs.writeFile(distHelper.distPath + '/' + appFolder + '/css/schedule.css', result.css, (writeErr) => {
if (writeErr !== null) {
logger.addLog('Error', 'Error in writing css file', socket, writeErr);
console.log(writeErr);
return socket.emit('live.error', { status: "Error in Writing css file" });
}
return gulp.minifyCss(distHelper.distPath + '/' + appFolder, function () {
logger.addLog('Success', 'css file was written successfully', socket);
return done(null, 'sass');
});
});
}
else {
logger.addLog('Error', 'Error in compiling SASS', socket, err);
console.log(err);
if (emit) socket.emit('live.error', { status: "Error in Compiling SASS" });
}
});
},
(done) => {
logger.addLog('Info', 'Extracting data from the uploaded jsons', socket);
console.log('================================WRITING\n');
if (emit) socket.emit('live.process', {donePercent: 70, status: "Compiling the HTML pages from templates" });
getJsonData(req.body, function(error, data) {
if (error) {
console.log('Error Invalid Zip');
logger.addLog('Error', 'Invalid Zip', socket, error);
if (emit) {
socket.emit('live.error', {status: 'Error in read contents of zip'});
}
return done(error);
}
logger.addLog('Success', 'Json data extracted', socket);
logger.addLog('Info', 'Name of the event found from the event json file', socket);
logger.addLog('Info', 'Compiling the html pages from the templates', socket);
const jsonData = data;
eventName = jsonData.eventurls.name;
if(req.body.datasource == 'eventapi') {
var backPath = distHelper.distPath + '/' + appFolder + '/' + jsonData.eventurls.background_path;
var basePath = distHelper.distPath + '/' + appFolder + '/images';
distHelper.optimizeBackground(backPath, socket, function() {
distHelper.resizeSponsors(basePath, socket, function() {
distHelper.resizeSpeakers(basePath, socket, function() {
templateGenerate();
});
});
});
}
else {
templateGenerate();
}
function templateGenerate() {
try {
fs.writeFileSync(distHelper.distPath + '/' + appFolder + '/tracks.html', minifyHtml(tracksTpl(jsonData)));
fs.writeFileSync(distHelper.distPath + '/' + appFolder + '/schedule.html', minifyHtml(scheduleTpl(jsonData)));
fs.writeFileSync(distHelper.distPath + '/' + appFolder + '/rooms.html', minifyHtml(roomstpl(jsonData)));
fs.writeFileSync(distHelper.distPath + '/' + appFolder + '/speakers.html', minifyHtml(speakerstpl(jsonData)));
fs.writeFileSync(distHelper.distPath + '/' + appFolder + '/index.html', minifyHtml(eventtpl(jsonData)));
} catch (err) {
console.log(err);
logger.addLog('Error', 'Error in compiling/writing templates', socket, err);
if (emit) {
socket.emit('live.error', {status: 'Error in Compiling/Writing templates'});
}
return done(err);
}
return distHelper.generateThumbnails(distHelper.distPath + '/' + appFolder, function() {
logger.addLog('Success', 'HTML pages were succesfully compiled from the templates', socket);
return done(null, 'write');
});
};
});
},
(done) => {
logger.addLog('Info', 'Cleaning up remaining folder of the same name as that of the event', socket);
console.log("============Cleaning up remaining folders of the same name\n");
if (emit) socket.emit('live.process', {donePercent: 75, status: "Cleaning up folders of the same name" });
distHelper.removeDuplicateEventFolders(eventName, req.body.email, socket, (remerr) => {
if (remerr !== null) {
logger.addLog('Error', 'Error occured while removing the duplicate event folders', socket, remerr);
console.log(remerr);
if (emit) socket.emit('live.error', {status: "Error in removing the duplicate event folders of the same name" });
}
return gulp.minifyHtml(distHelper.distPath + '/' + appFolder, function() {
logger.addLog('Success', 'Duplicated events removed successfully', socket);
return done(null, 'remove');
});
});
},
(done) => {
logger.addLog('Info', 'Renaming temporary folder to the actual event folder', socket);
console.log("============Renaming temporary folder to the actual event folder");
if (emit) socket.emit('live.process', {donePercent: 80, status: "Generating the event folder" });
const eventFolderSource = __dirname + '/../../dist/';
fs.move(eventFolderSource + appFolder, eventFolderSource + req.body.email + '/' + eventName, (moveerr) => {
if (moveerr !== null) {
logger.addLog('Error', 'Error in moving files to the event folders', socket, moverr);
console.log(moveerr);
if (emit) socket.emit('live.error', {status: "Error in moving files to the event directory" });
}
logger.addLog('Success', 'Changed the temporary name of the project to its actual name', socket);
appFolder = req.body.email + '/' + eventName;
done(null, 'move');
});
},
(done) => {
logger.addLog('Info', 'Creating zip file of the event', socket);
console.log("==================================Creating zip file");
if (emit) socket.emit('live.process', {donePercent:90, status: "Website is being generated"});
var output = fs.createWriteStream(distHelper.distPath + '/' + req.body.email + '/event.zip');
var archive = archiver('zip', {store: true});
output.on('close', function() {
logger.addLog('Success', 'Zip file has been created', socket);
done(null, 'zip');
});
archive.on('error', function(err) {
logger.addLog('Error', 'Error occured while zipping the file', socket, err)
});
archive.pipe(output);
archive.directory(distHelper.distPath + '/' + appFolder, '/').finalize();
},
(done) => {
logger.addLog('Info', 'Sending mail to the user', socket);
console.log('=================================SENDING MAIL\n');
if (emit) socket.emit('live.process', {donePercent: 95, status: "Website is being generated" });
if (req.body.ftpdetails) {
setTimeout(() => {
ftpDeployer.deploy(req.body.ftpdetails, appFolder, () => {
//Send call back to orga server
})
}, 30000);
}
mailer.uploadAndsendMail(req.body.email, eventName, socket, (url) => {
logger.addLog('Success', 'Mail sent succesfully', socket);
callback(appFolder, url);
done(null, 'write');
});
}
]);
};
exports.pipeZipToRes = function(email, appName, res) {
const appFolder = email + '/' + appName;
console.log('================================ZIPPING\n');
const zipfile = archiver('zip');
zipfile.on('error', (err) => {
throw err;
});
res.setHeader('Content-Type', 'application/zip');
zipfile.pipe(res);
zipfile.directory(distHelper.distPath + '/' + appFolder, '/').finalize();
};