UNPKG

all-node-versions

Version:
40 lines (25 loc) 939 B
import{readFile}from"node:fs/promises"; import{env}from"node:process"; import globalCacheDir from"global-cache-dir"; import writeFileAtomic from"write-file-atomic"; export const getCacheFile=async()=>{ const cacheDir=await globalCacheDir(CACHE_DIR); const cacheFilename=env.TEST_CACHE_FILENAME||CACHE_FILENAME; return`${cacheDir}/${cacheFilename}` }; const CACHE_DIR="nve"; const CACHE_FILENAME="versions_2.json"; export const getCacheFileContent=async(cacheFile)=>{ const cacheFileContent=await readFile(cacheFile); const{lastUpdate,...versionsInfo}=JSON.parse(cacheFileContent); const age=Date.now()-lastUpdate; return{versionsInfo,age} }; export const setCacheFileContent=async(cacheFile,versionsInfo)=>{ const lastUpdate=Date.now(); const cacheContent={lastUpdate,...versionsInfo}; const cacheFileContent=`${JSON.stringify(cacheContent,undefined,2)}\n`; try{ await writeFileAtomic(cacheFile,cacheFileContent) }catch{} };