import * as fs from "fs" ; import * as fsExtra from "fs-extra" ; import * as jszip from "jszip" ; import * as path from "path" ; export async function exportMetadataPackage ( output = "" , manifest = "manifest.json" , assets = "assets" ) { const zip = new jszip ( ) ; const manifestPath = path . resolve ( manifest ) ; if ( fs . existsSync ( manifestPath ) ) { const manifestData = fs . readFileSync ( manifestPath ) ; zip . file ( "manifest.json" , manifestData ) ; } else { throw new Error ( `The file '${ manifestPath }' does not exist` ) ; } if ( fs . existsSync ( assets ) ) { const files = fs . readdirSync ( assets ) ; zip . folder ( assets ) ; files . forEach ( ( element ) => { const filePath = path . join ( assets , element ) ; const fileData = fs . readFileSync ( filePath ) ; zip . file ( filePath , fileData ) ; } ) ; } else { throw new Error ( "Need folder of assets referenced by manifest file" ) ; } if ( output === "" ) { output = path . join ( path . dirname ( manifestPath ) , "manifest.zip" ) ; } else { output = path . resolve ( output ) ; } fsExtra . ensureDirSync ( path . dirname ( output ) ) ; await new Promise ( ( fulfill ) => zip . generateNodeStream ( { type : "nodebuffer" , streamFiles : true } ) . pipe ( fs . createWriteStream ( output ) ) . on ( "finish" , fulfill ) ) ; return output ; }