import fs from 'fs' ; import * as Archiver from 'archiver' ; import * as path from 'path' ; import ignore from 'ignore' ; param dirPath returns export function zipDir ( dirPath , outputPath ) { if ( ! fs . existsSync ( dirPath ) ) { throw new Error ( `Unable to find ${ dirPath }` ) ; } return new Promise ( ( resolve , reject ) => { const output = fs . createWriteStream ( outputPath ) ; const archive = Archiver . create ( 'zip' , { zlib : { level : 7 } } ) ; archive . on ( 'warning' , ( err ) => { reject ( err ) ; } ) ; archive . on ( 'error' , ( err ) => { reject ( err ) ; } ) ; output . on ( 'finish' , ( ) => { resolve ( outputPath ) ; } ) ; archive . pipe ( output ) ; let gIgnoreF = undefined ; if ( getGcloudIgnores ( dirPath ) . length > 0 ) { const gIgnore = ignore ( ) . add ( getGcloudIgnores ( dirPath ) ) ; gIgnoreF = function ( file ) : false | Archiver . EntryData { return ! gIgnore . ignores ( file . name ) ? file : false ; } ; } archive . directory ( dirPath , false , gIgnoreF ) ; archive . finalize ( ) ; } ) ; } param dir returns export function getGcloudIgnores ( dir ) { const gcloudIgnorePath = path . posix . join ( dir , '.gcloudignore' ) ; if ( ! fs . existsSync ( gcloudIgnorePath ) ) { return [ ] ; } return fs . readFileSync ( gcloudIgnorePath , { encoding : 'utf-8' } ) . toString ( ) . split ( /\r?\n/ ) . map ( ( s ) => s . trim ( ) ) ; }