1
0
Fork 0
mirror of https://github.com/sbrl/terrain50-cli.git synced 2024-11-22 06:53:01 +00:00

image: add --stream argument, and stdin support to --input

This commit is contained in:
Starbeamrainbowlabs 2021-01-14 17:55:16 +00:00
parent 2787701f1c
commit 9e47a12b12
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
3 changed files with 51 additions and 12 deletions

View file

@ -14,6 +14,11 @@ npm install --save terrain50-cli
-----
## v1.7 (unreleased)
- `image`: Add `--stream` argument for mass-converting large numbers of images from a single stream of multiple terrain50 files
- `image`: Add support for reading from stdin with `--input -`
## v1.6.4
- Update `terrain50`

View file

@ -8,6 +8,8 @@ import a from '../../Helpers/Ansi.mjs';
import Terrain50 from 'terrain50';
import Terrain50Renderer from './Terrain50Renderer.mjs';
import { write_safe, end_safe } from '../../Helpers/StreamHelpers.mjs';
export default async function(settings) {
if(typeof settings.cli.input !== "string") {
l.error("Error: No input file specified (try --filename path/to/file.asc)");
@ -18,19 +20,45 @@ export default async function(settings) {
process.exit(1);
}
let terrain50 = Terrain50.Parse(
await fs.promises.readFile(settings.cli.input, "utf-8")
);
if(!fs.existsSync(settings.cli.input)) {
l.error(`Error: The specified input file '${settings.cli.input}' does not exist (check the spelling and permissions).`);
process.exit(1);
}
let renderer = new Terrain50Renderer(settings.scale_factor | 1);
let png_buffer = await renderer.render(terrain50);
if(!(png_buffer instanceof Buffer))
throw new Error(`Error: Renderer did not return Buffer (found unexpected ${png_buffer} instead)`);
await fs.promises.writeFile(
settings.cli.output,
png_buffer
);
l.log(`Written to ${a.hicol}${settings.cli.output}${a.reset}`);
if(settings.cli.stream) {
if(!fs.existsSync(settings.cli.output))
await fs.promises.mkdir(settings.cli.output, { recursive: true, mode: 0o755 });
let reader = process.stdin;
if(settings.cli.input !== "-")
reader = fs.createReadStream(settings.cli.input, "utf-8");
let i = 0;
for await(let next of Terrain50.ParseStream(reader)) {
process.stderr.write(`${a.fgreen}>>>>> ${a.hicol} Item ${i} ${a.reset}${a.fgreen} <<<<<${a.reset}`);
await fs.promises.writeFile(
path.join(settings.cli.output, `${i}.png`),
await renderer.render(next)
);
}
l.log(`Written ${a.hicol}${i}${a.reset} items to ${a.hicol}${a.fgreen}${settings.cli.output}${a.reset}`);
}
else {
let terrain50 = Terrain50.Parse(
await fs.promises.readFile(settings.cli.input, "utf-8")
);
let png_buffer = await renderer.render(terrain50);
if(!(png_buffer instanceof Buffer))
throw new Error(`Error: Renderer did not return Buffer (found unexpected ${png_buffer} instead)`);
await fs.promises.writeFile(
settings.cli.output,
png_buffer
);
l.log(`Written to ${a.hicol}${settings.cli.output}${a.reset}`);
}
}

View file

@ -11,3 +11,9 @@ name = "output"
description = "The output file to write to"
# default_value = ""
type = "string"
[[arguments]]
name = "stream"
description = "Treat the input as a stream of Terrain50 objects. The path specified by the --output argument will be a directory containing all the extracted objects, 1 image file per object."
default_value = false
type = "boolean"