|
|
const fs = require( 'fs' ); |
|
|
const path = require( 'path' ); |
|
|
const archiver = require( 'archiver' ); |
|
|
const log = require( '../../lib/logger' )( 'desktop:lib:archiver' ); |
|
|
|
|
|
module.exports = { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
zipContents: ( contents, dst, onZipped ) => { |
|
|
const output = fs.createWriteStream( dst ); |
|
|
const archive = archiver( 'zip', { |
|
|
zlib: { level: 9 }, |
|
|
} ); |
|
|
|
|
|
output.on( 'close', function () { |
|
|
log.debug( 'Archive finalized: %s bytes written', archive.pointer() ); |
|
|
if ( typeof onZipped === 'function' ) { |
|
|
onZipped(); |
|
|
} |
|
|
} ); |
|
|
|
|
|
|
|
|
archive.on( 'warning', function ( err ) { |
|
|
log.warn( 'Unexpected error: ', err ); |
|
|
} ); |
|
|
|
|
|
archive.on( 'error', function ( err ) { |
|
|
throw err; |
|
|
} ); |
|
|
|
|
|
archive.pipe( output ); |
|
|
|
|
|
for ( let i = 0; i < contents.length; i++ ) { |
|
|
const src = contents[ i ]; |
|
|
const zipSubdir = path.basename( dst ).replace( '.zip', '' ); |
|
|
const dstInZipSubdir = path.join( zipSubdir, path.basename( src ) ); |
|
|
|
|
|
const stat = fs.lstatSync( src ); |
|
|
if ( stat.isDirectory() ) { |
|
|
archive.directory( path.join( src, path.sep ), dstInZipSubdir ); |
|
|
continue; |
|
|
} |
|
|
archive.file( src, { name: dstInZipSubdir } ); |
|
|
} |
|
|
|
|
|
archive.finalize(); |
|
|
}, |
|
|
}; |
|
|
|