Skip to content
Snippets Groups Projects
Commit a907921e authored by Josefine Wegert's avatar Josefine Wegert
Browse files

project structure refactored

parent 10e29946
No related branches found
No related tags found
No related merge requests found
......@@ -2,24 +2,14 @@ const express = require('express');
const bodyParser = require('body-parser');
const exec = require('child_process').exec;
const fs = require('fs');
const path = require('path');
const fsReaddir = require('fs-readdir');
const JSZip = require('jszip');
const exphbs = require('express-handlebars');
//initializer
const initGitRepos = require('./initializers/gitRepos.js');
//load names of available download files
const downloadFiles = require('./downloadFiles.json');
const downloadNames = Object.keys(downloadFiles);
//load functions for frontend and backend build
const clientFolder = require('./clientFolder.js');
const backendFunctions = require('./backendFunctions.js');
const frontendFunctions = require('./frontendFunctions.js');
const zipFunctions = require('./zipFunctions.js');
const app = express();
......@@ -32,157 +22,36 @@ app.use(bodyParser.json());
app.engine('.hbs', exphbs({extname: '.hbs'}));
app.set('view engine', '.hbs');
//clientCounter holds the number of clients that wants to download files
//load names of available download files
const downloadFiles = require('./public/data/downloadFiles.json');
const downloadNames = Object.keys(downloadFiles);
app.set('downloadFiles', downloadFiles);
app.set('downloadNames', downloadNames);
//load functions for frontend and backend build
const clientFolder = require('./public/javascripts/clientFolder.js');
const backendFunctions = require('./public/javascripts/backendFunctions.js');
const frontendFunctions = require('./public/javascripts/frontendFunctions.js');
const zipFunctions = require('./public/javascripts/zipFunctions.js');
app.set('clientFolder', clientFolder);
app.set('backendFunctions', backendFunctions);
app.set('frontendFunctions', frontendFunctions);
app.set('zipFunctions', zipFunctions);
//clientCounter holds the number of clients who wants to download files
let clientCounter = 0;
app.set('clientCounter', clientCounter);
//routes
//app.use('/', home);
require('./routes/home.js')(app);
require('./routes/download.js')(app);
//TODO add spinner for telling user to wait until initialization is done
initGitRepos.initialize(downloadFiles);
//This route shows the homepage.
app.get('/', function(req, res){
res.render('home', {downloadFiles: downloadNames});
});
//This route takes the checkedFiles the user selected on the homepage, shows the download page on which the actual download is triggered.
app.post('/', function(req, res, next){
const checkedFiles = Object.keys(req.body);
if(typeof checkedFiles !== 'undefined' && checkedFiles.length > 0){
clientCounter= clientCounter + 1;
res.render('download', {checkedFiles: checkedFiles, downloadFiles: downloadNames});
}
else {
res.render('home', {downloadFiles: downloadNames});
}
});
//This route gets the files the user checked for downloading, builds the custom build and delivers the zip file as a download.
app.get('/download', function(req, res, next){
const checkedFiles = req.query.checkedFiles.split(',');
let clientName;
const zip = new JSZip();
zip.file("README.txt", "text for README");
//holds promises for backend and/or frontend for synchronizing
const promiseArray = []
//TODO handle multiple clients
if (clientCounter > 5){
res.render('tooManyClients');
}else{
clientName = 'client_' + clientCounter;
clientFolder.makeClientFolder(clientName);
}
backendChecked = checkedFiles.find(function(file){
return file === 'explorviz-ui-backend';
});
frontendChecked = checkedFiles.find(function(file){
return file === 'explorviz-ui-frontend';
});
//The user is not allowed to check plugins without having checked frontend or backend core.
//If he has not checked at least one of those, he will be redirected to the homepage.
if(!(backendChecked || frontendChecked)){
res.render('home', {downloadFiles: downloadNames});
}
if (backendChecked){
const fileName = backendChecked;
function backendAll(){
return backendFunctions.prepareClientFolder(clientName,fileName).then(function(){
return backendFunctions.backendClean(clientName, fileName).then(function(){
return backendFunctions.backendPull(clientName, fileName).then(function(){
return backendFunctions.backendInstallAddons(checkedFiles,downloadFiles,clientName).then(function(){
return backendFunctions.backendBuild(checkedFiles, clientName, fileName)
})
})
})
}).catch(function(error){
let response =
'There was an error during your backend build. ' +
'Please contact "explorviz-developers-request@listserv.dfn.de" ' +
'and add the following information: ' + error;
res.send(response);
})
}
promiseArray.push(backendAll());
}
if (frontendChecked){
const fileName = frontendChecked;
const downloadURL = downloadFiles[frontendChecked];
function frontendAll() {
return frontendFunctions.prepareClientFolder(clientName,fileName).then(function(){
return frontendFunctions.frontendClean(clientName,fileName).then(function(){
return frontendFunctions.frontendPull(clientName,fileName).then(function(){
return frontendFunctions.frontendInstallAddons(clientName,fileName, checkedFiles, downloadFiles).then(function(){
return frontendFunctions.frontendBuild(clientName,fileName)
})
})
})
}).catch(function(error){
let response =
'There was an error during your frontend build. ' +
'Please contact "explorviz-developers-request@listserv.dfn.de" ' +
'and add the following information: ' + error;
res.send(response);
})
}
promiseArray.push(frontendAll());
};
finalizeZip();
console.log('promiseArray:', promiseArray);
function finalizeZip(){
return zipFunctions.streamFilesToZip(promiseArray,zip, clientName).then(function(zip){
return createZip(zip)
}).catch(function(error) {let response =
'There was an error during your build. ' +
'Please contact "explorviz-developers-request@listserv.dfn.de" ' +
'and add the following information: ' + error;
res.send(response);
})
}
function createZip(zip){
return new Promise((resolve,reject) => {
zip.generateNodeStream({type:'nodebuffer',streamFiles:true}).pipe(fs.createWriteStream('explorviz-builds.zip')).on('error', function(error){
console.log('error in createZip:', error);
}).on('finish', function () {
console.log('explorviz-builds.zip written.');
//client finishes everything except getting the download
clientCounter = clientCounter - 1;
res.download('explorviz-builds.zip', function(error){
if(error){
let response =
'There was an error during your build. ' +
'Please contact "explorviz-developers-request@listserv.dfn.de" ' +
'and add the following information: ' + error;
res.send(response);
}
});
});
})
}
});
//TODO add spinner for telling user to wait until initialization is done
//initGitRepos.initialize(downloadFiles);
app.listen(3000);
......
{ "explorviz-ui-frontend": "https://github.com/ExplorViz/explorviz-ui-frontend.git",
"explorviz-frontend-plugin-colorpicker":"https://github.com/ExplorViz/explorviz-frontend-plugin-colorpicker.git",
"explorviz-frontend-plugin-example": "https://github.com/ExplorViz/explorviz-frontend-plugin-example.git",
"explorviz-ui-backend": "https://github.com/ExplorViz/explorviz-ui-backend.git",
"explorviz-backend-plugin-example": "https://github.com/ExplorViz/explorviz-backend-plugin-example.git"
}
\ No newline at end of file
File moved
File moved
File moved
File moved
const express = require('express');
const JSZip = require('jszip');
const fs = require('fs');
module.exports= function(app){
const downloadNames = app.get('downloadNames');
const downloadFiles = app.get('downloadFiles');
let clientCounter = app.get('clientCounter');
const clientFolder = app.get('clientFolder');
const backendFunctions = app.get('backendFunctions');
const frontendFunctions = app.get('frontendFunctions');
const zipFunctions = app.get('zipFunctions');
//This route gets the files the user checked for downloading, builds the custom build and delivers the zip file as a download.
app.get('/download', function(req, res, next){
const checkedFiles = req.query.checkedFiles.split(',');
let clientName;
const zip = new JSZip();
zip.file("README.txt", "text for README");
//holds promises for backend and/or frontend for synchronizing
const promiseArray = []
console.log('clientCounter, download: ', clientCounter);
//TODO handle multiple clients
if (clientCounter > 5){
res.render('tooManyClients');
}else{
clientName = 'client_' + clientCounter;
console.log('clientCounter, download.makeClientFolder: ', clientCounter);
clientFolder.makeClientFolder(clientName);
}
backendChecked = checkedFiles.find(function(file){
return file === 'explorviz-ui-backend';
});
frontendChecked = checkedFiles.find(function(file){
return file === 'explorviz-ui-frontend';
});
//The user is not allowed to check plugins without having checked frontend or backend core.
//If he has not checked at least one of those, he will be redirected to the homepage.
if(!(backendChecked || frontendChecked)){
res.render('home', {downloadFiles: downloadNames});
}
if (backendChecked){
const fileName = backendChecked;
function backendAll(){
return backendFunctions.prepareClientFolder(clientName,fileName).then(function(){
return backendFunctions.backendClean(clientName, fileName).then(function(){
return backendFunctions.backendPull(clientName, fileName).then(function(){
return backendFunctions.backendInstallAddons(checkedFiles,downloadFiles,clientName).then(function(){
return backendFunctions.backendBuild(checkedFiles, clientName, fileName)
})
})
})
}).catch(function(error){
let response =
'There was an error during your backend build. ' +
'Please contact "explorviz-developers-request@listserv.dfn.de" ' +
'and add the following information: ' + error;
res.send(response);
})
}
promiseArray.push(backendAll());
}
if (frontendChecked){
const fileName = frontendChecked;
const downloadURL = downloadFiles[frontendChecked];
function frontendAll() {
return frontendFunctions.prepareClientFolder(clientName,fileName).then(function(){
return frontendFunctions.frontendClean(clientName,fileName).then(function(){
return frontendFunctions.frontendPull(clientName,fileName).then(function(){
return frontendFunctions.frontendInstallAddons(clientName,fileName, checkedFiles, downloadFiles).then(function(){
return frontendFunctions.frontendBuild(clientName,fileName)
})
})
})
}).catch(function(error){
let response =
'There was an error during your frontend build. ' +
'Please contact "explorviz-developers-request@listserv.dfn.de" ' +
'and add the following information: ' + error;
res.send(response);
})
}
promiseArray.push(frontendAll());
};
finalizeZip();
console.log('promiseArray:', promiseArray);
function finalizeZip(){
return zipFunctions.streamFilesToZip(promiseArray,zip, clientName).then(function(zip){
return createZip(zip)
}).catch(function(error) {let response =
'There was an error during your build. ' +
'Please contact "explorviz-developers-request@listserv.dfn.de" ' +
'and add the following information: ' + error;
res.send(response);
})
}
function createZip(zip){
return new Promise((resolve,reject) => {
zip.generateNodeStream({type:'nodebuffer',streamFiles:true}).pipe(fs.createWriteStream('explorviz-builds.zip')).on('error', function(error){
console.log('error in createZip:', error);
}).on('finish', function () {
console.log('explorviz-builds.zip written.');
//client finishes everything except getting the download
clientCounter = clientCounter - 1;
res.download('explorviz-builds.zip', function(error){
if(error){
let response =
'There was an error during your build. ' +
'Please contact "explorviz-developers-request@listserv.dfn.de" ' +
'and add the following information: ' + error;
res.send(response);
}
});
});
})
}
});
}
const express = require('express');
module.exports= function(app){
const downloadNames = app.get('downloadNames');
let clientCounter = app.get('clientCounter');
console.log('clientCounter, home: ', clientCounter);
//This route shows the homepage.
app.get('/', function(req, res){
res.render('home', {downloadFiles: downloadNames});
});
//This route takes the checkedFiles the user selected on the homepage, shows the download page on which the actual download is triggered.
app.post('/', function(req, res, next){
const checkedFiles = Object.keys(req.body);
if(typeof checkedFiles !== 'undefined' && checkedFiles.length > 0){
let increasedClientCounter = clientCounter + 1;
app.set('clientCounter', increasedClientCounter);
console.log('clientCounter, home +1: ', clientCounter);
res.render('download', {checkedFiles: checkedFiles, downloadFiles: downloadNames});
}
else {
res.render('home', {downloadFiles: downloadNames});
}
});
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment