diff --git a/app.js b/app.js index 624429d0a0860b432d45e980f1de6b7e05531406..f3dc3f3ed961ee1bca79d31fdd20e91fb397ba22 100644 --- a/app.js +++ b/app.js @@ -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. ' + diff --git a/zipFunctions.js b/zipFunctions.js new file mode 100644 index 0000000000000000000000000000000000000000..bbb793cf455cfddffa63c2a3ddde311cafeda5a8 --- /dev/null +++ b/zipFunctions.js @@ -0,0 +1,91 @@ +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 +} +