import { dirname } from "path" ; import { maxBy , map , filter } from "lodash" ; import { readFile } from "fs-extra" ; import { URI } from "vscode-uri" ; import globby from "globby" ; import { FileChangeType } from "vscode-languageserver" ; import { getLogger } from "./logger" ; type AbsolutePath = string ; type ManifestData = Record < AbsolutePath , { flexEnabled : boolean ; } > ; const manifestData = Object . create ( null ) ; export function isManifestDoc ( uri ) { return uri . endsWith ( "manifest.json" ) ; } export async function initializeManifestData ( workspaceFolderPath ) { const manifestDocuments = await findAllManifestDocumentsInWorkspace ( workspaceFolderPath ) ; const readManifestPromises = map ( manifestDocuments , async ( manifestDoc ) => { const isFlexEnabled = await readFlexEnabledFlagFromManifestFile ( manifestDoc ) ; if ( isFlexEnabled !== "INVALID" ) { manifestData [ manifestDoc ] = { flexEnabled : isFlexEnabled } ; } } ) ; getLogger ( ) . info ( "manifest data initialized" , { manifestDocuments } ) ; return Promise . all ( readManifestPromises ) ; } export function getFlexEnabledFlagForXMLFile ( xmlPath ) { const manifestFilesForCurrentFolder = filter ( Object . keys ( manifestData ) , ( manifestPath ) => xmlPath . startsWith ( dirname ( manifestPath ) ) ) ; const closestManifestPath = maxBy ( manifestFilesForCurrentFolder , ( manifestPath ) => manifestPath . length ) ; if ( closestManifestPath === undefined ) { return false ; } return manifestData [ closestManifestPath ] . flexEnabled ; } export async function updateManifestData ( manifestUri , changeType ) { getLogger ( ) . debug ( "`updateManifestData` function called" , { manifestUri , changeType , } ) ; const manifestPath = URI . parse ( manifestUri ) . fsPath ; switch ( changeType ) { case 1 : case 2 : { const isFlexEnabled = await readFlexEnabledFlagFromManifestFile ( manifestUri ) ; if ( isFlexEnabled !== "INVALID" ) { manifestData [ manifestPath ] = { flexEnabled : isFlexEnabled } ; } return ; } case 3 : delete manifestData [ manifestPath ] ; return ; } } async function findAllManifestDocumentsInWorkspace ( workspaceFolderPath ) { return globby ( `${ workspaceFolderPath }/**/manifest.json` ) ; } async function readFlexEnabledFlagFromManifestFile ( manifestUri ) { const manifestContent = await readFile ( URI . parse ( manifestUri ) . fsPath , "utf-8" ) ; let manifestJsonObject ; try { manifestJsonObject = JSON . parse ( manifestContent ) ; } catch ( err ) { return "INVALID" ; } const ui5Object = manifestJsonObject [ "sap.ui5" ] ?? { flexEnabled : false } ; const isFlexEnabled = ui5Object . flexEnabled ; return isFlexEnabled ; }