Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

New generate engine

Also starting to use underscore
  • Loading branch information...
commit 60dfa6647d75ebd3909127ebddfb328960ccf21f 1 parent 8767716
@Ziv-Barber authored
Showing with 61 additions and 76 deletions.
  1. +56 −75 lib/basicgen.js
  2. +5 −1 package.json
View
131 lib/basicgen.js
@@ -39,6 +39,10 @@ var Transform = require('stream').Transform || require('readable-stream/transfor
var archiver = require('archiver');
var fs = require('fs');
var PassThrough = require('stream').PassThrough || require('readable-stream/passthrough');
+var _ = require('underscore');
+var async = require('async');
+var startpoint = require('startpoint');
+// var sanitizer = require('sanitizer');
// Globals:
@@ -273,90 +277,67 @@ officegen = function ( options ) {
archive.pipe ( output_stream );
} // Endif.
- ///
- /// @brief Add the next resource into the zip stream.
- ///
- /// This function adding the next resource into the zip stream.
- ///
- function generateNextResource ( cur_index )
- {
- var resStream;
-
- if ( cur_index < gen_private.resources.length ) {
- if ( typeof gen_private.resources[cur_index] != 'undefined' ) {
- switch ( gen_private.resources[cur_index].type ) {
- // Generate the resource text data by calling to provided function:
- case 'buffer':
- resStream = gen_private.resources[cur_index].callback ( gen_private.resources[cur_index].data );
- break;
-
- // Just copy the file as is:
- case 'file':
- resStream = fs.createReadStream ( gen_private.resources[cur_index].data || gen_private.resources[cur_index].name );
- break;
-
- // Just use this stream:
- case 'stream':
- resStream = gen_private.resources[cur_index].data;
- break;
-
- // Officegen object:
- case 'officegen':
- resStream = new PassThrough ();
- gen_private.resources[cur_index].data.generate ( resStream );
- break;
-
- // Custom parser:
- default:
- for ( var cur_parserType in int_officegen_globals.resParserTypes ) {
- if ( (cur_parserType == gen_private.resources[cur_index].type) && int_officegen_globals.resParserTypes[cur_parserType] && int_officegen_globals.resParserTypes[cur_parserType].parserFunc ) {
- resStream = int_officegen_globals.resParserTypes[cur_parserType].parserFunc (
- genobj,
- gen_private.resources[cur_index].name,
- gen_private.resources[cur_index].callback, // Can be used as the template source for template engines.
- gen_private.resources[cur_index].data, // The data for the template engine.
- int_officegen_globals.resParserTypes[cur_parserType].extra_data
- );
- break;
- } // Endif.
- } // End of for loop.
- } // End of switch.
-
- if ( typeof resStream != 'undefined' ) {
- if ( int_officegen_globals.settings.verbose ) {
- console.log ( 'Adding "' + gen_private.resources[cur_index].name + '" (' + gen_private.resources[cur_index].type + ')...' );
- } // Endif.
-
- archive.append ( resStream, { name: gen_private.resources[cur_index].name }, function () {
- setImmediate ( function() { generateNextResource ( cur_index + 1 ); });
- });
-
- } else {
- setImmediate ( function() { generateNextResource ( cur_index + 1 ); });
+ var streamTransformers = {
+ 'buffer': function ( obj ) {
+ return startpoint ( obj.callback ( obj.data ) );
+ },
+ 'file': function ( obj ) {
+ return fs.createReadStream ( obj.data || obj.name );
+ },
+ 'stream': function ( obj ) {
+ return obj.data;
+ },
+ 'officegen': function ( obj ) {
+ resStream = new PassThrough ();
+ obj.data.generate ( resStream );
+ },
+ 'custom': function ( obj ) {
+ for ( var cur_parserType in int_officegen_globals.resParserTypes ) {
+ if ( (obj.subType == obj.type) && int_officegen_globals.resParserTypes[cur_parserType] && int_officegen_globals.resParserTypes[cur_parserType].parserFunc ) {
+ resStream = int_officegen_globals.resParserTypes[cur_parserType].parserFunc (
+ genobj,
+ obj.name,
+ obj.callback, // Can be used as the template source for template engines.
+ obj.data, // The data for the template engine.
+ int_officegen_globals.resParserTypes[cur_parserType].extra_data
+ );
+ break;
} // Endif.
+ } // End of for loop.
+ }
+ };
- } else {
- // Removed resource - just ignore it:
- setImmediate ( function() { generateNextResource ( cur_index + 1 ); });
+ var workQueue = async.queue ( function ( item, done ) {
+ var result = streamTransformers[item.type]( item );
+ if ( result != undefined ) {
+ // The internal verbose mode to help debugging:
+ if ( int_officegen_globals.settings.verbose ) {
+ console.log ( 'Adding "' + item.name + '" (' + item.type + ')...' );
} // Endif.
+ archive.append ( result, { name: item.name }, function ( err ) {
+ _.delay ( done, 1 ); // fix issue in node 10.x with corrupt files.
+ });
+
} else {
- // No more resources to add - close the archive:
- archive.finalize ( function ( err, written ) {
- // Event to the type generator:
- genobj.emit ( 'afterGen', gen_private, err, written );
+ _.delay ( done, 1 ); // fix issue in node 10.x with corrupt files.
+ } // Endif.
+ });
- if ( err ) {
- onArchiveError ( err );
- } // Endif.
+ workQueue.drain = function () {
+ archive.finalize ( function ( err, written ) {
+ // Event to the type generator:
+ genobj.emit ( 'afterGen', gen_private, err, written );
- genobj.emit ( 'finalize', written );
- });
- } // Endif.
+ if ( err ) {
+ onArchiveError ( err );
+ } // Endif.
+
+ genobj.emit ( 'finalize', written );
+ });
};
- // Start the process of generating the output zip stream:
- generateNextResource ( 0 );
+ _.each ( gen_private.resources, function ( d ) { workQueue.push ( d ); });
};
///
View
6 package.json
@@ -42,7 +42,11 @@
"setimmediate": ">= 1.0.1",
"readable-stream": "~1.0.0",
"archiver": ">= 0.4.4",
- "fast-image-size": "*"
+ "fast-image-size": "*",
+ "underscore": "x",
+ "stream-buffers": "x",
+ "async": "x",
+ "startpoint": "x"
},
"author": {
"name": "Ziv Barber",
Please sign in to comment.
Something went wrong with that request. Please try again.