diff --git a/app.js b/app.js index d4f33c9b8fecb485b32d553066f6930734689f68..6a5d22b5e1aaa25e24f4c159b6e4ad522cb386d8 100644 --- a/app.js +++ b/app.js @@ -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); diff --git a/public/data/downloadFiles.json b/public/data/downloadFiles.json new file mode 100644 index 0000000000000000000000000000000000000000..a21e3f9095b11baad2609dc3f4fd5e7a6041c885 --- /dev/null +++ b/public/data/downloadFiles.json @@ -0,0 +1,6 @@ +{ "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 diff --git a/backendFunctions.js b/public/javascripts/backendFunctions.js similarity index 100% rename from backendFunctions.js rename to public/javascripts/backendFunctions.js diff --git a/clientFolder.js b/public/javascripts/clientFolder.js similarity index 100% rename from clientFolder.js rename to public/javascripts/clientFolder.js diff --git a/frontendFunctions.js b/public/javascripts/frontendFunctions.js similarity index 100% rename from frontendFunctions.js rename to public/javascripts/frontendFunctions.js diff --git a/zipFunctions.js b/public/javascripts/zipFunctions.js similarity index 100% rename from zipFunctions.js rename to public/javascripts/zipFunctions.js diff --git a/routes/download.js b/routes/download.js new file mode 100644 index 0000000000000000000000000000000000000000..4ae8060694fcc5f6443a403b2dd6b47ecb3d96ef --- /dev/null +++ b/routes/download.js @@ -0,0 +1,144 @@ +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); + } + }); + }); + + }) + + +} + + +}); + +} diff --git a/routes/home.js b/routes/home.js new file mode 100644 index 0000000000000000000000000000000000000000..4864c144402eac96f6526011ad774b122a4dd80b --- /dev/null +++ b/routes/home.js @@ -0,0 +1,30 @@ +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}); + } +}); +}