8058
Ir
irvc / files
irvc / web / scripts / schemes.mjs
2.0 KB
1#!/usr/bin/env node 2// Converts a directory of terminal color scheme .yml files into the compact 3// JSON the settings page autocompletes against. 4// 5// node scripts/schemes.mjs ../fruit src/lib/schemes.json 6// 7// Each file is flat "key: value" yaml; the scheme name is the file name. Output 8// is a name-sorted array of { n: name, bg, fg, c: [16 ANSI colors] } with colors 9// as lowercase #rrggbb. 10import { readdirSync, readFileSync, writeFileSync } from "node:fs"; 11import { join } from "node:path"; 12 13const [dir, out] = process.argv.slice(2); 14if (!dir || !out) { 15 console.error("usage: schemes.mjs <yaml-dir> <out.json>"); 16 process.exit(2); 17} 18 19// hex normalizes "#abc", "'AABBCC'", "aabbcc" and 8-digit alpha forms to #rrggbb. 20function hex(v) { 21 const m = /^#?([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i.exec(v); 22 if (!m) return null; 23 let h = m[1].toLowerCase(); 24 if (h.length === 3) h = [...h].map((c) => c + c).join(""); 25 return "#" + h.slice(0, 6); 26} 27 28// value pulls the scalar off a "key: value # comment" line. 29function value(raw) { 30 raw = raw.trim(); 31 const q = /^(["'])(.*?)\1/.exec(raw); 32 return q ? q[2] : raw.split(/\s+#|\s/)[0]; 33} 34 35const schemes = []; 36for (const file of readdirSync(dir) 37 .filter((f) => f.endsWith(".yml")) 38 .sort()) { 39 const kv = {}; 40 for (const line of readFileSync(join(dir, file), "utf8").split(/\r?\n/)) { 41 const m = /^([a-z_0-9]+):\s*(.*)$/.exec(line); 42 if (m) kv[m[1]] = value(m[2]); 43 } 44 const c = Array.from({ length: 16 }, (_, i) => 45 hex(kv[`color_${String(i + 1).padStart(2, "0")}`] ?? ""), 46 ); 47 const bg = hex(kv.background ?? ""); 48 const fg = hex(kv.foreground ?? ""); 49 if (!bg || !fg || c.includes(null)) { 50 console.error(`skipping ${file}: missing or invalid colors`); 51 continue; 52 } 53 schemes.push({ n: file.replace(/\.yml$/, ""), bg, fg, c }); 54} 55 56schemes.sort((a, b) => a.n.localeCompare(b.n, "en", { sensitivity: "base" })); 57writeFileSync(out, JSON.stringify(schemes) + "\n"); 58console.error(`wrote ${schemes.length} schemes to ${out}`);