import { getCacheConfig } from "./cacheConfig" ; import { logger } from "../logger" ; import { salt } from "./salt" ; import * as backfill from "backfill/lib/api" ; import { CacheOptions } from "../types/CacheOptions" ; import path from 'path' ; export async function cacheHash ( id , cwd , root , cacheOptions , args ) { const cacheConfig = getCacheConfig ( cwd , cacheOptions ) ; const backfillLogger = backfill . makeLogger ( "error" , process . stdout , process . stderr ) ; const hashKey = salt ( cacheOptions . environmentGlob || [ "lage.config.js" ] , `${ id }|${ JSON . stringify ( args ) }` , path . relative ( root , cwd ) , cacheOptions . cacheKey ) ; backfillLogger . setName ( id ) ; try { return await backfill . computeHash ( cwd , backfillLogger , hashKey , cacheConfig ) ; } catch { } return null ; } export async function cacheFetch ( hash , id , cwd , cacheOptions ) { if ( ! hash ) { return false ; } const cacheConfig = getCacheConfig ( cwd , cacheOptions ) ; const backfillLogger = backfill . makeLogger ( "error" , process . stdout , process . stderr ) ; try { return await backfill . fetch ( cwd , hash , backfillLogger , cacheConfig ) ; } catch ( e ) { logger . error ( `${ id } fetchBackfill ${ e && ( e as any ) . stack || e && ( e as any ) . message || e }` ) ; } return false ; } export async function cachePut ( hash , cwd , cacheOptions ) { if ( ! hash ) { return ; } const cacheConfig = getCacheConfig ( cwd , cacheOptions ) ; const backfillLogger = backfill . makeLogger ( "warn" , process . stdout , process . stderr ) ; try { await backfill . put ( cwd , hash , backfillLogger , cacheConfig ) ; } catch ( e ) { } }