mirror of
https://github.com/sbrl/terrain50-cli.git
synced 2024-11-26 07:33:00 +00:00
add new json subcommand
This commit is contained in:
parent
e613e3a90c
commit
b91691b99a
2 changed files with 51 additions and 0 deletions
38
src/Subcommands/json/index.mjs
Normal file
38
src/Subcommands/json/index.mjs
Normal 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`);
|
||||||
|
}
|
13
src/Subcommands/json/meta.toml
Normal file
13
src/Subcommands/json/meta.toml
Normal 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"
|
Loading…
Reference in a new issue