import * as fse from "fs-extra" ; import * as md5 from "md5" ; import { getPathToTempFolder } from "./contextUtils" ; export interface ICommandHistory { pomPath : string ; timestamps : { [ command ] : number ; } ; } export interface ICommandHistoryEntry { command : string ; pomPath : string ; timestamp : number ; } export async function updateLRUCommands ( command , pomPath ) { const historyFilePath = getCommandHistoryCachePath ( pomPath ) ; await fse . ensureFile ( historyFilePath ) ; const content = ( await fse . readFile ( historyFilePath ) ) . toString ( ) ; let historyObject ; try { historyObject = JSON . parse ( content ) as ICommandHistory ; historyObject . pomPath = pomPath ; historyObject . timestamps [ command ] = Date . now ( ) ; } catch ( error ) { historyObject = { pomPath , timestamps : { } } ; historyObject . timestamps [ command ] = Date . now ( ) ; } await fse . writeFile ( historyFilePath , JSON . stringify ( historyObject ) ) ; } export async function getLRUCommands ( pomPath ) { const filepath = getCommandHistoryCachePath ( pomPath ) ; if ( await fse . pathExists ( filepath ) ) { const content = ( await fse . readFile ( filepath ) ) . toString ( ) ; let historyObject ; try { historyObject = JSON . parse ( content ) as ICommandHistory ; } catch ( error ) { historyObject = { pomPath , timestamps : { } } ; } const timestamps = historyObject . timestamps ; const commandList = Object . keys ( timestamps ) . sort ( ( a , b ) => timestamps [ b ] - timestamps [ a ] ) ; return commandList . map ( command => Object . assign ( { command , pomPath , timestamp : timestamps [ command ] } ) as ICommandHistoryEntry ) ; } return [ ] ; } function getCommandHistoryCachePath ( pomXmlFilePath ) { return getPathToTempFolder ( md5 ( pomXmlFilePath ) , "commandHistory.json" ) ; }