Created
August 12, 2020 12:12
-
-
Save mauicv/d436944db7b4e1ad10853c9c69299819 to your computer and use it in GitHub Desktop.
[Javascript Language Interface] Run with node can be spawned as a subprocess from another language and acts as a wrapper for js code. #interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var state = { | |
prop: undefined | |
doSomething: (envName) => {}, | |
doSomethingElse: (_) => {}, | |
setProp: (v) => { | |
this.prop = v | |
}, | |
} | |
process.stdin.setEncoding('utf-8') | |
process.stdout.setEncoding('utf-8') | |
process.stdin.on('readable', () => { | |
const content = process.stdin.read() | |
if(!!content) { | |
let data = parseContent(content) | |
let response = state[data.cmd](data.args) | |
process.stdout.write(prepareResp(response)) | |
} | |
}) | |
function prepareResp(data){ | |
dataJSON = JSON.stringify(data) | |
return `${dataJSON}\n` | |
} | |
function parseContent(content){ | |
let data = JSON.parse(content) | |
return data | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment