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

work in progress promises for jszip

parent 3994a6a0
No related branches found
No related tags found
No related merge requests found
...@@ -2,11 +2,12 @@ var express = require('express'); ...@@ -2,11 +2,12 @@ var express = require('express');
var bodyParser = require('body-parser'); var bodyParser = require('body-parser');
var exec = require('child_process').exec; var exec = require('child_process').exec;
var execSync = require('child_process').execSync;
var fs = require('fs'); var fs = require('fs');
var path = require('path'); var path = require('path');
var fsReaddir = require('fs-readdir');
var Zip = require('node-zip'); var Zip = require('node-zip');
var archiver = require('archiver'); var JSZip = require('jszip');
var exphbs = require('express-handlebars'); var exphbs = require('express-handlebars');
var logger = require('morgan'); var logger = require('morgan');
...@@ -49,20 +50,15 @@ if(typeof checkedFiles !== 'undefined' && checkedFiles.length > 0){ ...@@ -49,20 +50,15 @@ if(typeof checkedFiles !== 'undefined' && checkedFiles.length > 0){
app.get('/download', function(req, res){ app.get('/download', function(req, res){
var promises=[];
const checkedFiles = req.query.checkedFiles.split(','); const checkedFiles = req.query.checkedFiles.split(',');
var zip = new Zip(); var zip = new JSZip();
// create a file to stream archive data to. // create a file to stream archive data to.
var output = fs.createWriteStream(__dirname + 'explorviz-builds.zip'); zip.file("README.txt", "text for README");
var archive = archiver('zip', {
base64: false,
compression:'DEFLATE',
store: true // Sets the compression method to STORE.
});
archive.append(fs.readFileSync(path.join(__dirname, 'README.txt')), {name: 'README.txt'}); var frontendFinished = false;
var backendFinished = false;
//----------------------------------------------------------------backend //----------------------------------------------------------------backend
backendChecked = checkedFiles.find(function(file){ backendChecked = checkedFiles.find(function(file){
...@@ -70,9 +66,14 @@ return file === 'explorviz-ui-backend'; ...@@ -70,9 +66,14 @@ return file === 'explorviz-ui-backend';
}); });
if (backendChecked){ if (backendChecked){
const fileName = backendChecked; const fileName = backendChecked;
const downloadURL = downloadFiles[backendChecked]; const downloadURL = downloadFiles[backendChecked];
exec('git clone ' + downloadURL + ' ' + fileName + ' && cd ' + fileName + ' && mvn compile war:war ', (error, stdout, stderr) => {
//exec('git clone ' + downloadURL + ' ' + fileName + ' && cd ' + fileName + ' && mvn compile war:war ', (error, stdout, stderr) => {
exec('git pull ' + ' && cd ' + fileName + ' && mvn compile war:war ', (error, stdout, stderr) => {
promises.push(new Promise(addBackend()));
var response = var response =
'There was an error during your build. ' + 'There was an error during your build. ' +
...@@ -86,12 +87,9 @@ if (backendChecked){ ...@@ -86,12 +87,9 @@ if (backendChecked){
console.log(stdout); console.log(stdout);
console.log(stderr); console.log(stderr);
}); //.then(addBackend()), würde auch gehen (npm packet: exec-then), aber hilft nicht dabei, zu wissen, wann zip gebaut werden darf
archive.file(fileName + '/target/explorviz-ui-backend-1.0-SNAPSHOT.war', {name:'backend/explorviz-backend.war'});
});
} }
//--------------------------------------------------------------------------------frontend //--------------------------------------------------------------------------------frontend
...@@ -103,29 +101,31 @@ if (frontendChecked){ ...@@ -103,29 +101,31 @@ if (frontendChecked){
const fileName = frontendChecked; const fileName = frontendChecked;
const downloadURL = downloadFiles[frontendChecked]; const downloadURL = downloadFiles[frontendChecked];
exec('git clone ' + downloadURL + ' ' + fileName + ' && cd '+ fileName + ' && npm install', (error, stdout, stderr) => { //exec('git clone ' + downloadURL + ' ' + fileName + ' && cd '+ fileName + ' && npm install', (error, stdout, stderr) => {
console.log('downloadURL', downloadURL); exec('git pull ', (error, stdout, stderr) => {
console.log('fileName', fileName); installAddons();
var response = var response =
'There was an error during your build. ' + 'There was an error during your build. ' +
'Please contact "explorviz-developers-request@listserv.dfn.de" ' + 'Please contact "explorviz-developers-request@listserv.dfn.de" ' +
'and add the following information: ' + error; 'and add the following information: ' + error;
console.log('stdout', stdout);
console.log('stderr', stderr);
if (error) { if (error) {
res.send(response); res.send(response);
return; return;
} }
});
//Plugins to install? };
checkedPlugins = checkedFiles.filter(function(file){ function installAddons(){
checkedPlugins = checkedFiles.filter(function(file){
return /\bexplorviz-frontend-plugin-\w*/.exec(file);
}); });
checkedPlugins.forEach((pluginName) => { checkedPlugins.forEach((pluginName) => {
exec('ember install'+ downloadFiles[pluginName], (error, stdout, stderr) => { exec('cd ' + 'explorviz-ui-frontend ' + 'ember install ' + downloadFiles[pluginName], (error, stdout, stderr) => {
var response = var response =
'There was an error during your build. ' + 'There was an error during your build. ' +
...@@ -143,16 +143,24 @@ console.log(stderr); ...@@ -143,16 +143,24 @@ console.log(stderr);
}); });
}); });
buildFrontend();
};
function buildFrontend(){
console.log('buildFrontend()');
//build everything you have //build everything you have
exec('ember build --environment production', (error, stdout, stderr) => { exec('cd explorviz-ui-frontend && ember build --environment=production', (error, stdout, stderr) => {
//exec('cd explorviz-ui-frontend', (error, stdout, stderr) => {
var frontendP = new Promise(addFrontend(resolve,reject));
promises.push(frontendP);
var response = var response =
'There was an error during your build. ' + 'There was an error during your build. ' +
'Please contact "explorviz-developers-request@listserv.dfn.de" ' + 'Please contact "explorviz-developers-request@listserv.dfn.de" ' +
'and add the following information: ' + error; 'and add the following information: ' + error;
console.log('stdout', stdout);
console.log('stderr', stderr);
if (error) { if (error) {
res.send(response); res.send(response);
return; return;
...@@ -162,34 +170,90 @@ console.log(stdout); ...@@ -162,34 +170,90 @@ console.log(stdout);
console.log(stderr); console.log(stderr);
}); // ------------------------------------------ember build
}); });
archive.directory('dist/', {name: 'frontend/dist'}); }; // ------------------------------------------ember build
}
//create zip and download it
// listen for all archive data to be written function addBackend(){
output.on('close', function() { // read a file as a stream and add it to a zip
console.log(archive.pointer() + ' total bytes'); console.log('addBackend()');
console.log('archiver has been finalized and the output file descriptor has closed.'); const backendStream = fs.createReadStream("explorviz-ui-backend/target/explorviz-ui-backend-1.0-SNAPSHOT.war");
}); zip.file("backend.war", backendStream);
backendFinished = true;
createZip();
};
// good practice to catch this error explicitly function addFrontend(resolve,reject){
archive.on('error', function(err) { console.log("in addFrontend");
throw err;
});
archive.pipe(output); var stream = fsReaddir('./explorviz-ui-frontend/dist/')
archive.finalize(); .on('error', function(err) {
console.log('error:', err);
}).on('data', function(data){
for (var i=0; i<data.length; i++) {
const frontendStream = fs.createReadStream(data[i]);
zip.file(data[i], frontendStream);
}
}).on('finish',function(){
frontendFinished = true;
//return printFinished();
});
console.log('stream',stream);
};
function printFinished(resolve,reject){
console.log('frontendFinished');
}
console.log('promises' ,promises);
console.log('promises.length', promises.length);
if (promises.length){
Promise.all(promises).then(function createZip(){
console.log('frontendFinished', frontendFinished);
console.log('backendFinished', backendFinished);
//if(frontendFinished){
// if (frontendChecked){
// if (backendChecked){
// if(frontendFinished && backendFinished){
// console.log('both');
//create zip and download it
zip.generateNodeStream({type:'nodebuffer',streamFiles:true}).pipe(fs.createWriteStream('explorviz-builds.zip')).on('finish', function () {
// JSZip generates a readable stream with a "end" event,
// but is piped here in a writable stream which emits a "finish" event.
console.log("explorviz-builds.zip written.");
res.download('explorviz-builds.zip'); res.download('explorviz-builds.zip');
});
//}
}); });
}
// } else if (frontendFinished) {//!backendChecked
// console.log('frontend only');
// zip.generateNodeStream({type:'nodebuffer',streamFiles:true}).pipe(fs.createWriteStream('explorviz-builds.zip')).on('finish', function () {
// // JSZip generates a readable stream with a "end" event,
// // but is piped here in a writable stream which emits a "finish" event.
// console.log("explorviz-builds.zip frontend only written.");
// res.download('explorviz-builds.zip');
// });}
// } else if (backendChecked){//!frontendChecked
// if (backendFinished){
// console.log('backend only');
// //create zip and download it
// zip.generateNodeStream({type:'nodebuffer',streamFiles:true}).pipe(fs.createWriteStream('explorviz-builds.zip')).on('finish', function () {
// // JSZip generates a readable stream with a "end" event,
// // but is piped here in a writable stream which emits a "finish" event.
// console.log("explorviz-builds.zip backend only written.");
// res.download('explorviz-builds.zip');
// });
// }
// }
// }
});
app.listen(8080); app.listen(8080);
console.log("Downloader started on port 8080."); console.log("Downloader started on port 8080.");
\ No newline at end of file
...@@ -9,17 +9,19 @@ ...@@ -9,17 +9,19 @@
"author": "explorviz-developers-request@listserv.dfn.de", "author": "explorviz-developers-request@listserv.dfn.de",
"license": "Apache-2.0", "license": "Apache-2.0",
"dependencies": { "dependencies": {
"adm-zip": "^0.4.7",
"archiver": "^1.3.0",
"body-parser": "^1.17.1", "body-parser": "^1.17.1",
"child_process": "^1.0.2", "child_process": "^1.0.2",
"cookie-parser": "^1.4.3", "cookie-parser": "^1.4.3",
"express": "^4.15.2", "express": "^4.15.2",
"express-handlebars": "^3.0.0", "express-handlebars": "^3.0.0",
"fs": "0.0.1-security", "fs": "0.0.1-security",
"fs-readdir": "0.0.3",
"jszip": "^3.1.3",
"morgan": "^1.8.2", "morgan": "^1.8.2",
"node-zip": "^1.1.1", "node-zip": "^1.1.1",
"path": "^0.12.7", "path": "^0.12.7"
"archiver": "^1.3.0" },
"devDependencies": {
"ember-cli-test-plugin": "git+https://github.com/ExplorViz/explorviz-frontend-plugin-example.git"
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment