import { File , Project , Directory } from "../models" ; import * as JSZip from "jszip" ; export async function downloadProject ( project , uri ? ) { const zipFile = new JSZip ( ) ; let zipName = "wasm-project.zip" ; if ( uri !== undefined ) { zipName = `wasm-project-${ uri }.zip` ; } const queue = [ ] ; project . mapEachFile ( ( f ) => queue . push ( { filePrefix : "" , file : f } ) ) ; while ( queue . length > 0 ) { const { filePrefix , file } = queue . shift ( ) ; const fileName = filePrefix + file . name ; if ( file instanceof Directory ) { file . mapEachFile ( f => queue . push ( { filePrefix : fileName + "/" , file : f } ) ) ; zipFile . folder ( fileName ) ; continue ; } zipFile . file ( fileName , file . data ) ; } await zipFile . generateAsync ( { type : "blob" , mimeType : "application/zip" } ) . then ( ( blob ) => { const link = document . createElement ( "a" ) ; link . download = zipName ; link . href = URL . createObjectURL ( blob ) ; document . body . appendChild ( link ) ; link . click ( ) ; document . body . removeChild ( link ) ; } ) ; }