Skip to content

Commit 71c33bc

Browse files
committed
feat(Console): Add simple console script
1 parent 72936ba commit 71c33bc

3 files changed

Lines changed: 103 additions & 7 deletions

File tree

package-lock.json

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"main": "index.js",
66
"scripts": {
77
"serve": "ts-node src/serve.ts --tcp=7300",
8+
"serve:dev": "ts-node-dev src/serve.ts --tcp=7300",
9+
"console": "ts-node src/console.ts",
810
"format": "npx prettier --write './**/*.{js,json,md,ts,yaml}'",
911
"lint": "eslint 'src/**/*.{ts,js}' --fix",
1012
"test": "jest",
@@ -21,7 +23,7 @@
2123
"jest": "^24.9.0",
2224
"ts-jest": "^24.1.0",
2325
"ts-node": "^8.4.1",
24-
"ts-node-dev": "^1.0.0-pre.42",
26+
"ts-node-dev": "^1.0.0-pre.43",
2527
"typescript": "^3.6.3"
2628
},
2729
"dependencies": {

src/console.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Comments
 (0)