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

refactoring of zip functions

parent 38771f07
No related branches found
No related tags found
No related merge requests found
......@@ -6,7 +6,6 @@ var execSync = require('child_process').execSync;
var fs = require('fs');
var path = require('path');
var fsReaddir = require('fs-readdir');
var Zip = require('node-zip');
var JSZip = require('jszip');
var exphbs = require('express-handlebars');
......@@ -20,6 +19,7 @@ const downloadNames = Object.keys(downloadFiles);
//load functions for frontend and backend build
let backendFunctions = require('./backendFunctions.js');
let frontendFunctions = require('./frontendFunctions.js');
let zipFunctions = require('./zipFunctions.js');
var app = express();
......@@ -120,93 +120,10 @@ app.use(bodyParser.json());
};
finalizeZip();
//TODO put all zip-things in zipFunctions.js;
//problem: createZip has to know the res from /download-route and if just the other functions will be exported, the zip has to be passed to createZip with resolve()
// create a file to stream archive data to.
const zip = new JSZip();
zip.file("README.txt", "text for README");
let finishedArray=[];
function streamFilesToZip(){
return new Promise((resolve,reject)=> {
Promise.all(promiseArray).then(files => {
files.forEach(file => {
if (file.zipFileName === 'backend.war'){
fs.readFileAsync = function(filePath){
return new Promise(function (resolve, reject) {
fs.readFile(filePath, function(err, data) {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
}
function addBackendToZip(data){
return new Promise(function(resolve,reject){
zip.file(file.zipFileName, data);
resolve();
})
}
console.log('in backend.war');
backendContent = fs.readFileAsync(file.path);
backendContent.then(function(data){
addBackendToZip(data).then(function(){
finishedArray.push('backend finished');
if(finishedArray.length === promiseArray.length){
resolve(zip);
}
})
})
}
if(file.zipFileName === 'frontend'){
const frontendStream = fsReaddir(file.path)
.on('error', function(err) {
console.log('error:', err);
}).on('data', function(data){
for (let i=0; i<data.length; i++) {
const frontendReadStream = fs.createReadStream(data[i]);
zip.file(data[i], frontendReadStream);
}
}).on('finish', function(){
finishedArray.push('frontend finished');
if(finishedArray.length === promiseArray.length){
resolve(zip);
}
console.log('finishedArray in frontend:', finishedArray);
})
}
})
})
})
}
function finalizeZip(){
return streamFilesToZip().then(function(zip){
return zipFunctions.streamFilesToZip(promiseArray).then(function(zip){
return createZip(zip)
}).catch(function(error) {let response =
'There was an error during your build. ' +
......
var express = require('express');
var fs = require('fs');
var fsReaddir = require('fs-readdir');
var JSZip = require('jszip');
const zip = new JSZip();
zip.file("README.txt", "text for README");
let finishedArray=[];
function streamFilesToZip(promiseArray){
return new Promise((resolve,reject)=> {
Promise.all(promiseArray).then(files => {
files.forEach(file => {
if (file.zipFileName === 'backend.war'){
fs.readFileAsync = function(filePath){
return new Promise(function (resolve, reject) {
fs.readFile(filePath, function(err, data) {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
}
function addBackendToZip(data){
return new Promise(function(resolve,reject){
zip.file(file.zipFileName, data);
resolve();
})
}
console.log('in backend.war');
backendContent = fs.readFileAsync(file.path);
backendContent.then(function(data){
addBackendToZip(data).then(function(){
finishedArray.push('backend finished');
if(finishedArray.length === promiseArray.length){
resolve(zip);
}
})
})
}
if(file.zipFileName === 'frontend'){
const frontendStream = fsReaddir(file.path)
.on('error', function(err) {
console.log('error:', err);
}).on('data', function(data){
for (let i=0; i<data.length; i++) {
const frontendReadStream = fs.createReadStream(data[i]);
zip.file(data[i], frontendReadStream);
}
}).on('finish', function(){
finishedArray.push('frontend finished');
if(finishedArray.length === promiseArray.length){
resolve(zip);
}
console.log('finishedArray in frontend:', finishedArray);
})
}
})
})
})
}
module.exports= {
streamFilesToZip
}
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