@@ -80,19 +80,27 @@ module.exports.headlessZip = function headlessZip(dirToZip) {
8080 }
8181
8282 var zip = new JSZip ( ) ;
83- fs . readdir ( dirToZip , function ( error , files ) {
84- if ( error ) {
85- onError ( error )
86- return
87- }
88- for ( var i = 0 , len = files . length ; i < len ; i ++ ) {
89- zip . file ( files [ i ] , fs . readFileSync ( path . join ( dirToZip , files [ i ] ) ) , { compression : "DEFLATE" } )
83+ var files = fs . readdirSync ( dirToZip )
84+ for ( var i = 0 , len = files . length ; i < len ; i ++ ) {
85+ zip . file ( files [ i ] , fs . readFileSync ( path . join ( dirToZip , files [ i ] ) ) , { compression : "DEFLATE" } )
86+ }
87+ fs . writeFileSync ( outputFileName , zip . generate ( { type :"nodebuffer" , compression :'DEFLATE' } ) ) ;
88+ deleteDir ( dirToZip )
89+ }
90+
91+ module . exports . tryZipOnExit = function tryZipOnExit ( ) {
92+ var outputDir = dirToWriteTo ;
93+ if ( ! outputDir ) {
94+ ouputDir = process . cwd ( ) . toString ( )
95+ }
96+ var files = fs . readdirSync ( outputDir ) ;
97+ // Search for temporary output directory using pattern matching
98+ for ( var i = 0 , len = files . length ; i < len ; i ++ ) {
99+ if ( / t m p _ ( \w + ) / . test ( files [ i ] . toString ( ) ) ) {
100+ var dirToZip = path . join ( outputDir , files [ i ] )
101+ this . headlessZip ( dirToZip )
102+ return ;
90103 }
91-
92- zip . generateNodeStream ( { type :'nodebuffer' , streamFiles :true } )
93- . pipe ( fs . createWriteStream ( outputFileName ) )
94- . on ( 'finish' , function ( ) {
95- deleteDir ( dirToZip )
96- } )
97- } )
98- }
104+ }
105+ }
106+
0 commit comments