import { parse } from './parser' ; import { IAst } from './ast' ; import { ICommand , ICommands } from './types' ; interface IOptions { input : string ; command : ICommand ; cache ? : Record < string , ICommands > ; } export const resolve = async ( options ) => { let tries = 0 ; const cacheGet = ( key ) : ICommands | null => { if ( options . cache ) { return options . cache [ key ] || null ; } return null ; } ; const cacheSet = ( key , commands ) => { if ( options . cache ) { options . cache [ key ] = commands ; } return null ; } ; const run = async ( ) : Promise < IAst > => { tries ++ ; const ast = parse ( options . input , options . command , cacheGet ) ; if ( ast . pending && options . cache && tries < 50 ) { const { value } = ast . pending . token ; const result = await ast . pending . resolve ( value || undefined ) ; if ( result ) { cacheSet ( ast . pending . key , result ) ; return run ( ) ; } } return ast ; } ; return run ( ) ; } ;