1- import * as childProcess from " child_process"
2- import { getLogger } from '@stencila/logga'
1+ import * as childProcess from ' child_process'
2+ import { getLogger } from '@stencila/logga'
33const lps = require ( 'length-prefixed-stream' )
44const { spawn } = require ( 'child_process' )
55const log = getLogger ( 'engine:backends' )
@@ -10,7 +10,7 @@ export interface ExecutorBackend {
1010 execute ( o : any ) : Promise < any >
1111}
1212
13- export class StencilaPythonBackend implements ExecutorBackend {
13+ abstract class StdioBackend implements ExecutorBackend {
1414 private process ?: childProcess . ChildProcess
1515
1616 private stdin : any
@@ -19,9 +19,10 @@ export class StencilaPythonBackend implements ExecutorBackend {
1919
2020 private executionRequestCount : number = 0
2121
22- setup ( ) : void {
23- this . process = spawn ( 'python3' , [ '-m' , 'stencila.schema' , 'listen' ] )
24- if ( ! this . process ) throw new Error ( 'Spawning python3 failed' )
22+ protected abstract spawn ( ) : childProcess . ChildProcess
23+
24+ public setup ( ) : void {
25+ this . process = this . spawn ( )
2526 if (
2627 this . process . stdout === null ||
2728 this . process . stdin === null ||
@@ -42,15 +43,15 @@ export class StencilaPythonBackend implements ExecutorBackend {
4243 this . stdin . pipe ( this . process . stdin )
4344 }
4445
45- receive ( json : Buffer , raw : boolean = false ) {
46+ private receive ( json : Buffer , raw : boolean = false ) {
4647 const response = JSON . parse ( json . toString ( ) )
4748 const resolve = this . executionRequests [ response . id ]
4849 resolve ( response . body )
4950 delete this . executionRequests [ response . id ]
5051 }
5152
52- async execute ( o : any ) : Promise < any > {
53- if ( ! this . process ) {
53+ public async execute ( o : any ) : Promise < any > {
54+ if ( this . process === undefined ) {
5455 throw new Error ( 'Can not execute before setup' )
5556 }
5657
@@ -72,3 +73,25 @@ export class StencilaPythonBackend implements ExecutorBackend {
7273 return promise
7374 }
7475}
76+
77+ export class StencilaPythonBackend extends StdioBackend {
78+ protected spawn ( ) {
79+ return spawn ( 'python3' , [ '-m' , 'stencila.schema' , 'listen' ] )
80+ }
81+ }
82+
83+ export class StencilaJsBackend extends StdioBackend {
84+ protected spawn ( ) {
85+ return spawn (
86+ 'npx' ,
87+ [
88+ 'ts-node' ,
89+ '/Users/ben/Documents/stencila/schema/ts/interpreter' ,
90+ 'listen'
91+ ] ,
92+ {
93+ cwd : '/Users/ben/Documents/stencila/schema'
94+ }
95+ )
96+ }
97+ }
0 commit comments