UNPKG

@stencila/basha

Version:

Bash interpreter for executable documents

113 lines (112 loc) 3.32 kB
#!/usr/bin/env node import { Capabilities, Listener, schema, Server, Claims } from '@stencila/executa'; import * as pty from 'node-pty'; export declare class Basha extends Listener { /** * Programming language names supported by this * interpreter. */ readonly programmingLanguages: string[]; /** * The pseudo-terminal in which bash is executed. */ protected terminal?: pty.IPty; /** * The prompt used to identify when the pseudo- * terminal has finished executing and is ready * for more input. */ readonly prompt = "\uD83D\uDD28 BASHA > "; /** * Output from entering the last Bash input. * * Used to buffer output from the pseudo-terminal. */ private output?; /** * Is Bash ready for more input? */ private isReady; /** * A lock to prevent async event loops from attempting to enter * code into the terminal at the same time. */ private lock; /** * Function to call when Bash is ready */ private whenReady?; /** * Flag to mute log errors when this interpreter * is explicitly `stop()`ed */ private isStopping; /** * The id of the current job. * * Used to enable cancellation of jobs by * checking that the job being cancelled is * the current one. */ protected job?: string; constructor(servers?: Server[]); /** * @override Override of `Executor.capabilities` to * define this interpreter's capabilities. */ capabilities(): Promise<Capabilities>; /** * @override Override of `Executor.execute` that executes Bash code. * * Calculates the duration of the execution to the nearest microsecond. */ execute<Type>(node: Type, session?: schema.SoftwareSession, claims?: Claims, job?: string): Promise<Type>; /** * @override Override of `Executor.cancel` that cancels the * current job only. */ cancel(job: string): Promise<boolean>; /** * Start a Bash shell process. * * Creates a pseudo-terminal and registers * event handles on it to capture output and * handle unexpected exit. */ startBash(): pty.IPty; /** * Enter Bash code into the terminal and set up handler to * process output. * * @param code Code to enter. * @returns A promise resolving to the output. */ enterCode(code: string): Promise<string | undefined>; /** * Execute Bash code. * * This method enters the code, parses the output and * checks the exit code. If the exit code is non-zero * it throws an error with the output. * * @param code Code to execute * @returns A promise resolving to the output from the command. */ executeCode(code: string): Promise<unknown | undefined>; /** * Parse output from a command. * * Attempts to parse the output as JSON. * In the future, more advanced parsing * such as parsing of fixed-width tables * may be done. * * @param output Output string to parse */ parseOutput(output: string): unknown; /** * @override Override of `Listener.stop` to * stop the pseudo-terminal as well as servers. */ stop(): Promise<void>; }