add new json subcommand

This commit is contained in:
Starbeamrainbowlabs 2022-12-06 16:54:36 +00:00
parent e613e3a90c
commit b91691b99a
Signed by: sbrl
GPG Key ID: 1BE5172E637709C2
2 changed files with 51 additions and 0 deletions

View File

@ -0,0 +1,38 @@
"use strict";
import fs from 'fs';
import nexline from 'nexline';
import a from '../../Helpers/Ansi.mjs';
import l from '../../Helpers/Log.mjs';
import Terrain50 from 'terrain50';
import { write_safe, end_safe } from '../../Helpers/StreamHelpers.mjs';
async function process_filename(filepath, stream_out) {
const stream_in = fs.createReadStream(filepath);
let i = 0;
for await(const frame of Terrain50.ParseStream(stream_in)) {
const obj = frame.to_json();
await write_safe(stream_out, JSON.stringify(obj) + "\n");
i++;
}
return i;
}
export default async function(settings) {
const stream_out = process.stdout;
if(settings.cli.output !== "-")
stream_out = fs.createWriteStream(settings.cli.output);
if(!(settings.cli.input instanceof Array))
settings.cli.input = [ settings.cli.input ];
let frames = 0;
for(let filepath of settings.cli.input) {
l.log(`Processing ${a.hicol}${filepath}${a.reset} from CLI argument`);
frames += await process_filename(filepath, stream_out);
}
l.log(`${a.hicol}${a.fgreen}Done${a.reset}, serialised ${a.hicol}${a.fgreen}${frames}${a.reset} frames in total`);
}

View File

@ -0,0 +1,13 @@
description = "Convert 1 or more Terrain50 files to JSON (or JSONL if there are multiple frames in the input). Any logging messages are sent to stderr."
[[arguments]]
name = "input"
description = "The input Terrain50 file(s) to operate on. May be specified multiple times. Default: stdin (can be specified explicitly with single dash '-' (without quotes)."
# default_value = ""
type = "string_multi"
[[arguments]]
name = "output"
description = "The output to write the Terrain50 instance(s) to. Defaults to stdout (represented by a single dash '-' without quotes)."
default_value = "-"
type = "string"