|
| 1 | +/** |
| 2 | + * A simple console for testing connection to, and configuration of, |
| 3 | + * executors. |
| 4 | + * |
| 5 | + * Usage: |
| 6 | + * |
| 7 | + * ```bash |
| 8 | + * ts-node console |
| 9 | + * ``` |
| 10 | + * |
| 11 | + * For full debug level log messages use: |
| 12 | + * |
| 13 | + * ```bash |
| 14 | + * ts-node console --debug |
| 15 | + * ``` |
| 16 | + * |
| 17 | + * Sends requests to execute a `CodeChunk` with `programmingLanguage: 'sh'` |
| 18 | + * to the VM and prints it's `outputs` to the console. |
| 19 | + */ |
| 20 | + |
| 21 | +import { |
| 22 | + getLogger, |
| 23 | + LogLevel, |
| 24 | + replaceHandlers, |
| 25 | + defaultHandler |
| 26 | +} from '@stencila/logga' |
| 27 | +import minimist from 'minimist' |
| 28 | +import * as readline from 'readline' |
| 29 | +import { ClientType } from './base/Client' |
| 30 | +import Executor, { Manifest, Method } from './base/Executor' |
| 31 | +import TcpClient from './tcp/TcpClient' |
| 32 | +import discoverTcp from './tcp/discover' |
| 33 | +import { CodeChunk } from '@stencila/schema' |
| 34 | + |
| 35 | +const { _, ...options } = minimist(process.argv.slice(2)) |
| 36 | + |
| 37 | +const log = getLogger('executa:console') |
| 38 | + |
| 39 | +const red = '\u001b[31;1m' |
| 40 | +const blue = '\u001b[34;1m' |
| 41 | +const reset = '\u001b[0m' |
| 42 | + |
| 43 | +/** |
| 44 | + * Configure log handler to only show `info` and `debug` events |
| 45 | + * when `--debug` flag is set |
| 46 | + */ |
| 47 | +replaceHandlers(data => { |
| 48 | + const { level } = data |
| 49 | + if (level <= (options.debug !== undefined ? LogLevel.debug : LogLevel.warn)) { |
| 50 | + process.stderr.write(`--- `) |
| 51 | + defaultHandler(data, { level: LogLevel.debug }) |
| 52 | + } |
| 53 | +}) |
| 54 | + |
| 55 | +// eslint-disable-next-line |
| 56 | +;(async () => { |
| 57 | + // Collect manifest of the peer `TcpServer` |
| 58 | + const manifests: Manifest[] = await discoverTcp() |
| 59 | + const clientTypes: ClientType[] = [TcpClient as ClientType] |
| 60 | + log.debug(`Obtained manifests: ${JSON.stringify(manifests, null, ' ')}`) |
| 61 | + |
| 62 | + // Create executor (no need to start it, since it has no servers) |
| 63 | + const executor = new Executor(manifests, clientTypes) |
| 64 | + |
| 65 | + // Create the REPL with the starting prompt |
| 66 | + const repl = readline.createInterface({ |
| 67 | + input: process.stdin, |
| 68 | + output: process.stdout, |
| 69 | + prompt: '>>> ' |
| 70 | + }) |
| 71 | + repl.prompt() |
| 72 | + |
| 73 | + // For each line the user enters... |
| 74 | + repl.on('line', async (line: string) => { |
| 75 | + // When user enters a line, execute a `CodeChunk` |
| 76 | + const result = (await executor.execute({ |
| 77 | + type: 'CodeChunk', |
| 78 | + programmingLanguage: 'python', |
| 79 | + text: line |
| 80 | + })) as CodeChunk |
| 81 | + |
| 82 | + // Display any errors |
| 83 | + if (result.errors !== undefined && result.errors.length > 0) { |
| 84 | + process.stderr.write(red) |
| 85 | + for (const error of result.errors) |
| 86 | + if (error.message !== undefined) process.stderr.write(error.message) |
| 87 | + process.stderr.write(reset) |
| 88 | + } |
| 89 | + |
| 90 | + // Display any outputs |
| 91 | + if (result.outputs !== undefined && result.outputs.length > 0) { |
| 92 | + process.stdout.write(blue) |
| 93 | + for (const output of result.outputs) process.stdout.write(`${output}`) |
| 94 | + process.stdout.write(reset) |
| 95 | + } |
| 96 | + |
| 97 | + // Provide a new prompt |
| 98 | + repl.prompt() |
| 99 | + }) |
| 100 | +})() |
0 commit comments