Added basic command-line flags for width and output file name

feature-bot-script
Mike Lynch 2025-01-27 16:04:26 +11:00
parent 54adcacdb1
commit 224e1180d1
1 changed files with 10 additions and 3 deletions

View File

@ -2,6 +2,8 @@ import { Resvg } from "@resvg/resvg-js";
import { promises } from "fs"; import { promises } from "fs";
import { JSDOM } from "jsdom"; import { JSDOM } from "jsdom";
import * as d3 from "d3"; import * as d3 from "d3";
import yargs from "yargs/yargs";
import { hideBin } from "yargs/helpers";
import random from "random"; import random from "random";
@ -86,19 +88,24 @@ function poptimal_svg() {
} }
async function main() { 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 = { const opts = {
background: 'rgba(255, 255, 255, 1.0)', background: 'rgba(255, 255, 255, 1.0)',
fitTo: { fitTo: {
mode: 'width', mode: 'width',
value: 1200, value: argv.w,
}, },
}; };
const resvg = new Resvg(svg, opts) const resvg = new Resvg(svg, opts)
const pngData = resvg.render() const pngData = resvg.render()
const pngBuffer = pngData.asPng() const pngBuffer = pngData.asPng()
await promises.writeFile('test.png', pngBuffer); await promises.writeFile(argv.o, pngBuffer);
} }