import { zipObject , keys , map , noop } from "lodash" ; import { resolve } from "path" ; import { writeFile , mkdirs , pathExists } from "fs-extra" ; import fetch from "node-fetch" ; import { TestModelVersion } from "../../api" ; const silent = true ; const log = silent ? noop : ( _ ) => console . log ( _ ) ; const error = silent ? noop : ( _ ) => console . error ( _ ) ; async function getLibs ( version ) { let versionInMetadataURL = version ; if ( versionInMetadataURL !== "1.76.0" ) { versionInMetadataURL = "1.76.0" ; } const url = `https://unpkg.com/@sapui5/distribution-metadata@${ versionInMetadataURL }/metadata.json` ; const response = await fetch ( url ) ; if ( ! response . ok ) { log ( `error fetching from ${ url }` ) ; return [ ] ; } const fileContent = await response . text ( ) ; const librariesMetadata = JSON . parse ( fileContent ) ; return keys ( librariesMetadata . libraries ) ; } export async function addUi5Resources ( version , folder ) { const baseUrl = `https://sapui5.hana.ondemand.com/${ version }/test-resources/` ; const libs = await getLibs ( version ) ; const nameToFile = zipObject ( libs , map ( libs , ( _ ) => `${ baseUrl }${ _ . replace ( /\./g , "/" ) }/designtime/api.json` ) ) ; await mkdirs ( folder ) ; await Promise . all ( map ( nameToFile , ( url , name ) => { return writeUrlToFile ( url , resolve ( folder , `${ name }.designtime.api.json` ) ) ; } ) ) ; } async function writeUrlToFile ( url , file ) { if ( await pathExists ( file ) ) { return ; } log ( `fetching from ${ url }` ) ; const response = await fetch ( url ) ; if ( ! response . ok ) { error ( `error fetching from ${ url }` ) ; return ; } const text = await response . text ( ) ; if ( text === "{}" ) { log ( `empty object returned from ${ url }` ) ; return ; } await writeFile ( file , text ) ; }