From 224e1180d1daf54fc6f321b7829bdc0c64d18d58 Mon Sep 17 00:00:00 2001 From: Mike Lynch Date: Mon, 27 Jan 2025 16:04:26 +1100 Subject: [PATCH] Added basic command-line flags for width and output file name --- poptimal.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/poptimal.js b/poptimal.js index 2fc1bc0..5232c74 100644 --- a/poptimal.js +++ b/poptimal.js @@ -2,6 +2,8 @@ import { Resvg } from "@resvg/resvg-js"; import { promises } from "fs"; import { JSDOM } from "jsdom"; import * as d3 from "d3"; +import yargs from "yargs/yargs"; +import { hideBin } from "yargs/helpers"; import random from "random"; @@ -86,19 +88,24 @@ function poptimal_svg() { } async function main() { - const svg = poptimal_svg(); + const argv = yargs(hideBin(process.argv)) + .usage("Usage: -w WIDTH -o OUTPUT_PNG") + .default('w', 1200) + .default('o', 'poptimal.png').argv; + + const svg = poptimal_svg(argv.w); const opts = { background: 'rgba(255, 255, 255, 1.0)', fitTo: { mode: 'width', - value: 1200, + value: argv.w, }, }; const resvg = new Resvg(svg, opts) const pngData = resvg.render() const pngBuffer = pngData.asPng() - await promises.writeFile('test.png', pngBuffer); + await promises.writeFile(argv.o, pngBuffer); }