import {Command, ParseContext, processPath, StrTerm, wrapString} from "./command.js"; import {LexInput} from "../lexer.js"; import {StrVM} from "../vm.js"; import {Awaitable} from "../../util/types.js"; import fs from "node:fs/promises"; export class InFile extends Command<{ path: StrTerm }> { isParseCandidate(token: LexInput): boolean { return this.isKeyword(token, 'infile') } async attemptParse(context: ParseContext): Promise<{ path: StrTerm }> { return { path: await context.popTerm() } } getDisplayName(): string { return 'infile' } execute(vm: StrVM, data: { path: StrTerm }): Awaitable { return vm.replaceContextMatchingTerm(ctx => ({ override: async () => { const path = processPath(ctx.resolveString(data.path)) const content = await fs.readFile(path, 'utf8') return wrapString(content) } })) } }