import axios from 'axios' ; import { exec } from 'child_process' ; import path from 'path' ; import { promisify } from 'util' ; import copy from 'copy' ; import fs from 'fs-extra' ; import JSZip from 'jszip' ; import rimraf from 'rimraf' ; import File from 'vinyl' ; export async function copyAsync ( source , destination ) { if ( isFile ( destination ) ) { await fs . ensureDir ( path . dirname ( destination ) ) ; } else { await fs . ensureDir ( destination ) ; } return new Promise ( ( resolve , reject ) => copy ( source , destination , ( error , files = [ ] ) => ( error ? reject ( error ) : resolve ( files ) ) ) ) ; } export async function downloadFileAsync ( url , baseDir ) { const zipFile = path . join ( baseDir , 'archive.zip' ) ; await fs . ensureDir ( baseDir ) ; await new Promise ( ( resolve , reject ) => { const writer = fs . createWriteStream ( zipFile ) . on ( 'error' , reject ) . on ( 'finish' , resolve ) ; return axios . request ( { method : 'get' , responseType : 'stream' , url , } ) . then ( response => { response . data . pipe ( writer ) ; } ) ; } ) ; await extractAsync ( zipFile , baseDir ) ; await fs . remove ( zipFile ) ; } export async function extractAsync ( zipFile , destination ) { const jszip = new JSZip ( ) ; await fs . ensureDir ( destination ) ; const data = await fs . readFile ( zipFile ) ; const entries = [ ] ; await jszip . loadAsync ( data , { createFolders : true } ) ; jszip . forEach ( ( filePath , entry ) => entries . push ( [ filePath , entry ] ) ) ; const stripEntry = entries [ 0 ] [ 0 ] ; await Promise . all ( entries . map ( async ( [ filePath , entry ] ) => { const resolvedFilePath = path . join ( destination , filePath . replace ( stripEntry , '' ) ) ; if ( entry . dir ) { await fs . ensureDir ( resolvedFilePath ) ; } else { const content = await entry . async ( 'nodebuffer' ) ; await fs . writeFile ( resolvedFilePath , content ) ; } } ) ) ; } export const isFile = ( path ) => /[^.\/\\]+\..+$/ . test ( path ) ; export const rimrafAsync = promisify ( rimraf ) ; export const execAsync = promisify ( exec ) ;