56 lines
1.3 KiB
JavaScript
Executable File
56 lines
1.3 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
const commandLineArgs = require ("command-line-args")
|
|
const temml = require ("temml")
|
|
const fs = require ("node:fs")
|
|
const path = require ("node:path")
|
|
const { DecoderStream, EncoderStream } = require ("cbor-x")
|
|
const { Transform } = require('node:stream')
|
|
|
|
const cli_spec =
|
|
[ { name: "preamble" }
|
|
]
|
|
|
|
function load_preambles (preamble) {
|
|
const data = fs.readFileSync (preamble, "utf8") .toString ()
|
|
return temml.definePreamble (data)
|
|
}
|
|
|
|
let macros = {}
|
|
|
|
function is_render_command (cmd) {
|
|
return cmd instanceof Array
|
|
&& cmd.length === 1
|
|
&& typeof cmd[0] === "string"
|
|
}
|
|
|
|
function do_command (cmd) {
|
|
try {
|
|
if (typeof cmd === "string") {
|
|
return temml.renderToString (cmd, {macros})
|
|
} else if (is_render_command (cmd)) {
|
|
return temml.renderToString (cmd[0], {displayMode: true,macros})
|
|
} else {
|
|
return null
|
|
}
|
|
} catch (e) {
|
|
console.error (e)
|
|
return {type: "error", error: e}
|
|
}
|
|
}
|
|
|
|
function main () {
|
|
const options = commandLineArgs (cli_spec)
|
|
macros = load_preambles (options.preamble)
|
|
const decoder = new DecoderStream ()
|
|
const encoder = new EncoderStream ()
|
|
process.stdin.pipe (decoder)
|
|
const command_responses = decoder.map (do_command)
|
|
decoder
|
|
.map (do_command)
|
|
.pipe (encoder)
|
|
.pipe (process.stdout)
|
|
}
|
|
|
|
main ()
|