import * as repl from "repl" ; import { runJXACode } from "@jxa/run" ; import * as util from "util" ; export class JXARepl { private replServer ! : repl . REPLServer ; start ( ) { this . replServer = repl . start ( { prompt : '> ' , eval : myEval , writer : myWriter } ) ; const cmdStack = [ ] ; this . replServer . defineCommand ( "clear" , ( ) => { cmdStack . length = 0 ; ( this . replServer as any ) . clearBufferedCommand ( ) ; } ) ; function myEval ( cmd , _context , _filename , callback ) { if ( cmd . length === 0 ) { return callback ( null ) ; } const existCode = cmdStack . join ( "\n" ) ; const code = existCode + "\n" + cmd ; runJXACode ( code ) . then ( output => { cmdStack . push ( cmd ) ; callback ( null , util . inspect ( output ) ) ; } ) . catch ( error => { callback ( error ) ; } ) ; } function myWriter ( output ) { return output ; } } stop ( ) { this . replServer . close ( ) ; } }