import { createWriteStream } from "fs" ; import { FStreamEntry , Reader } from "fstream" ; import { Pack } from "tar" ; import * as zlib from "zlib" ; import { streamDone } from "./io" ; export function gzip ( input ) { return input . pipe ( zlib . createGzip ( ) ) ; } export function unGzip ( input ) { const output = zlib . createGunzip ( ) ; input . pipe ( output ) ; return output ; } export function writeTgz ( inputDirectory , outFileName ) { return new Promise < void > ( ( resolve , reject ) => { resolve ( streamDone ( createTgz ( inputDirectory , reject ) . pipe ( createWriteStream ( outFileName ) ) ) ) ; } ) ; } export function createTgz ( dir , onError ) { return gzip ( createTar ( dir , onError ) ) ; } function createTar ( dir , onError ) { const packer = Pack ( { noProprietary : true } ) . on ( "error" , onError ) ; return Reader ( { path : dir , type : "Directory" , filter : addDirectoryExecutablePermission } ) . on ( "error" , onError ) . pipe ( packer ) ; } /**
 * Work around a bug where directories bundled on Windows do not have executable permission when extracted on Linux.
 * https://github.com/npm/node-tar/issues/7#issuecomment-17572926
 */ function addDirectoryExecutablePermission ( entry ) { if ( entry . props . type === "Directory" ) { entry . props . mode = addExecutePermissionsFromReadPermissions ( entry . props . mode ) ; } return true ; } function addExecutePermissionsFromReadPermissions ( mode ) { const allExecutePermissions = 0o111 ; const readPermissionsAsExecutePermissions = ( mode >>> 2 ) & allExecutePermissions ; return mode | readPermissionsAsExecutePermissions ; }