-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
83 lines (79 loc) · 3.08 KB
/
Copy pathcli.ts
File metadata and controls
83 lines (79 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* cli.ts — render the hexknot mark to an SVG or PNG file from the command line.
*
* Run: npx tsx cli.ts [out.svg|out.png] [--param=value ...]
* Example: npx tsx cli.ts logo.svg --colors=#ff9a00,#e5006d,#3a7bd5
* npx tsx cli.ts logo.svg --gradient=flow
* npx tsx cli.ts logo.svg --animated=true --animationDuration=8
* npx tsx cli.ts logo.svg --cornerRadius=8
* npx tsx cli.ts logo.svg --bandGap=40
* npx tsx cli.ts logo.svg --colors=#333333 (original flat mark)
* npx tsx cli.ts logo.png --pngSize=512 (raster width in px)
* npx tsx cli.ts logo.png --padding=large (or a number, or none|small|medium)
*/
import { writeFileSync } from "node:fs";
import { PADDING_PRESETS, type PaddingPreset } from "./default.ts";
import { DEFAULTS, hexKnotSvg, type HexKnotParams } from "./hexknot.ts";
import { PNG_SIZE, renderPng } from "./png.ts";
function parseArgs(argv: string[]): { out: string; params: HexKnotParams; pngSize: number } {
const params: HexKnotParams = {};
let out = "hexknot.svg";
let pngSize = PNG_SIZE;
for (const arg of argv) {
const match = /^--([A-Za-z]+)=(.+)$/.exec(arg);
if (!match) {
// Only a bare word can be the output file; a malformed option (--key,
// --key=) must not silently become a file named "--key".
if (arg.startsWith("--")) {
console.warn(`[hexknot] ignoring ${arg} — options take a value: --key=value`);
continue;
}
out = arg;
continue;
}
const [, key, raw] = match;
// The output width of a .png, in pixels — not a parameter of the mark itself.
if (key === "pngSize") {
if (Number.isNaN(Number(raw))) {
console.warn(`[hexknot] ignoring --pngSize: "${raw}" is not a number`);
continue;
}
pngSize = Number(raw);
continue;
}
// `padding` also accepts a named preset (--padding=large).
if (key === "padding" && raw in PADDING_PRESETS) {
params.padding = PADDING_PRESETS[raw as PaddingPreset];
continue;
}
if (!(key in DEFAULTS)) {
console.warn(
`[hexknot] ignoring unknown option --${key} (known: ${Object.keys(DEFAULTS).join(", ")}, pngSize)`,
);
continue;
}
const template = DEFAULTS[key as keyof typeof DEFAULTS];
let value: unknown = raw;
if (Array.isArray(template)) {
value = raw
.split(",")
.map((s) => s.trim())
.filter(Boolean);
} else if (typeof template === "number") {
if (Number.isNaN(Number(raw))) {
console.warn(`[hexknot] ignoring --${key}: "${raw}" is not a number`);
continue;
}
value = Number(raw);
} else if (typeof template === "boolean") {
value = raw !== "false";
}
Object.assign(params, { [key]: value });
}
return { out, params, pngSize };
}
const { out, params, pngSize } = parseArgs(process.argv.slice(2));
const svg = hexKnotSvg(params);
const content = out.endsWith(".png") ? renderPng(svg, pngSize) : svg;
writeFileSync(out, content);
console.log(`[hexknot] wrote ${out} (${content.length} bytes)`);