archivo del paquete→ .shtukbase/packages/lenses/kit/bin/shtukbase-icons.js
kit/bin/shtukbase-icons.js
134 líneas, 8.0 KB.
El archivo tal como está en el paquete: el código y sus comentarios no se traducen.
- #!/usr/bin/env node
- // shtukbase-icons - card pictures in the style of the kit (patterns/pictures.md). Works under Bun and Node 20+.
- // new <file.svg> [--color blue] put a picture template (canvas 96×96, two shapes from the palette)
- // check <file.svg | folder>... check the style: viewBox 96×96, no text and no raster, colors only from the palette
- // sheet <folder> [-o <file.html>] overview sheet: all pictures of the folder in three card sizes (48, 84, 108)
- import fs from "node:fs";
- import path from "node:path";
- // The style palette: the main Notion colors and their shades for details (highlight, shadow, second face). There are no other colors.
- export const PALETTE = {
- red: { main: "#FF7369", light: ["#FFC2BD", "#FFE4E1"], dark: [] },
- orange: { main: "#FFA344", light: [], dark: ["#E8842A"] },
- yellow: { main: "#FFDC49", light: ["#FFF0A8"], dark: ["#CB912F", "#9A6700"] },
- green: { main: "#4DAB9A", light: ["#E4F1EE", "#9CC8BC", "#8FD0C3", "#7CE0B0", "#6FE3C1"], dark: [] },
- blue: { main: "#529CCA", light: ["#DCEEF7", "#A8D3EC"], dark: ["#337EA9"] },
- purple: { main: "#9A6DD7", light: ["#EDE4F7"], dark: ["#7A52B8"] },
- pink: { main: "#E255A1", light: [], dark: [] },
- neutral: { main: null, light: ["#FFFFFF", "#B4B2AC"], dark: ["#3D4A66"] },
- };
- const MAIN = new Set(Object.values(PALETTE).map((c) => c.main).filter(Boolean));
- const ALLOWED = new Set(Object.values(PALETTE).flatMap((c) => [c.main, ...c.light, ...c.dark]).filter(Boolean));
- const HELP = `shtukbase-icons - card pictures in the style of the kit (apps/standard/packages/lenses/kit/patterns/pictures.md)
- new <file.svg> [--color <color>] template: canvas 96×96, a main shape and a detail; color is red, orange, yellow,
- green, blue, purple or pink; blue by default
- check <file.svg | folder>... check the style; exit code 1 if there are errors
- sheet <folder> [-o <file.html>] overview sheet of the pictures of a folder in card sizes (<folder>/_sheet.html by default)`;
- function template(color) {
- const c = PALETTE[color] || PALETTE.blue;
- const light = c.light[0] || "#FFFFFF";
- const other = color === "yellow" ? PALETTE.red.main : PALETTE.yellow.main;
- return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 96 96" width="96" height="96">
- <!-- Style: apps/standard/packages/lenses/kit/patterns/pictures.md. Flat, no background tile and no outline, 2-4 palette colors, no text. -->
- <!-- Main shape: large, rounded corners, fills the field from about 8 to 88. -->
- <rect x="14" y="22" width="68" height="56" rx="12" fill="${c.main}"/>
- <!-- Detail in a light shade of the same color: a highlight or a second face. -->
- <rect x="24" y="32" width="30" height="10" rx="5" fill="${light}"/>
- <!-- Second palette color: an accent that tells what the thing is. -->
- <circle cx="66" cy="62" r="12" fill="${other}"/>
- <!-- Thin white details only inside colored shapes. -->
- <path d="M60 62h12" stroke="#FFFFFF" stroke-width="4" fill="none" stroke-linecap="round"/>
- </svg>
- `;
- }
- /** Checks one SVG: {errors: [], warnings: []}. */
- export function checkSvg(text) {
- const errors = [], warnings = [];
- const root = /<svg\b[^>]*>/i.exec(text);
- if (!root) return { errors: ["no root <svg>"], warnings };
- if (!/viewBox="0 0 96 96"/.test(root[0])) errors.push('viewBox is not "0 0 96 96"');
- if (/<text\b/i.test(text)) errors.push("text inside the picture (<text>): the caption belongs on the card, not in the picture");
- if (/<image\b/i.test(text)) errors.push("a raster insert (<image>): pictures are drawn with shapes only");
- if (/<(linearGradient|radialGradient|filter)\b/i.test(text)) errors.push("gradient or filter: the style is flat");
- const body = text.replace(/<!--[\s\S]*?-->/g, "");
- const colors = [...body.matchAll(/#[0-9a-fA-F]{6}\b|#[0-9a-fA-F]{3}\b/g)].map((m) => m[0].toUpperCase());
- const named = [...body.matchAll(/(?:fill|stroke)="(?!none|#|url)([a-zA-Z]+)"/g)].map((m) => m[1]);
- const bad = [...new Set([...colors.filter((c) => !ALLOWED.has(c)), ...named])];
- if (bad.length) errors.push(`colors not from the palette: ${bad.join(", ")}`);
- const mains = new Set(colors.filter((c) => MAIN.has(c)));
- if (mains.size < 2) warnings.push(`the picture has ${mains.size} main palette colors; the style has several (2-4)`);
- // background tile: a light rectangle over almost the whole canvas under the picture
- for (const m of body.matchAll(/<rect\b[^>]*>/g)) {
- const a = (k) => Number((new RegExp(`\\b${k}="([\\d.]+)"`).exec(m[0]) || [])[1] || 0);
- if (a("width") >= 76 && a("height") >= 76) warnings.push(`a ${a("width")}×${a("height")} rectangle covers almost the whole canvas: remove it if it is a background tile; keep it if it is the thing itself (a window, a board)`);
- }
- return { errors, warnings };
- }
- function svgFiles(paths) {
- const out = [];
- for (const p of paths) {
- if (!fs.existsSync(p)) { out.push({ path: p, missing: true }); continue; }
- if (fs.statSync(p).isDirectory()) {
- for (const f of fs.readdirSync(p).filter((f) => f.endsWith(".svg")).sort()) out.push({ path: path.join(p, f) });
- } else out.push({ path: p });
- }
- return out;
- }
- function main(argv) {
- const cmd = argv.shift();
- if (!cmd || cmd === "-h" || cmd === "--help") { console.log(HELP); return cmd ? 0 : 1; }
- if (cmd === "new") {
- const ci = argv.indexOf("--color");
- let color = ci >= 0 ? argv.splice(ci, 2)[1] : "blue";
- const file = argv[0];
- if (!file) { console.error("new: a path to an .svg file is needed"); return 2; }
- if (!PALETTE[color] || color === "neutral") { console.error(`new: no color ${color}`); return 2; }
- if (fs.existsSync(file)) { console.error(`new: ${file} already exists, not overwriting`); return 1; }
- fs.mkdirSync(path.dirname(file), { recursive: true });
- fs.writeFileSync(file, template(color));
- console.log(`Template: ${file}. Draw the thing with large shapes, then: shtukbase-icons check ${file}`);
- return 0;
- }
- if (cmd === "check") {
- if (!argv.length) { console.error("check: a file or folder is needed"); return 2; }
- let nErr = 0, nWarn = 0, n = 0;
- for (const f of svgFiles(argv)) {
- if (f.missing) { console.log(`ERROR ${f.path}: no such file`); nErr++; continue; }
- n++;
- const { errors, warnings } = checkSvg(fs.readFileSync(f.path, "utf8"));
- for (const e of errors) console.log(`ERROR ${f.path}: ${e}`);
- for (const w of warnings) console.log(`WARNING ${f.path}: ${w}`);
- nErr += errors.length; nWarn += warnings.length;
- }
- console.log(`Pictures: ${n}; errors: ${nErr}; warnings: ${nWarn}`);
- return nErr ? 1 : 0;
- }
- if (cmd === "sheet") {
- const oi = argv.indexOf("-o");
- const out = oi >= 0 ? argv.splice(oi, 2)[1] : null;
- const dir = argv[0];
- if (!dir || !fs.existsSync(dir)) { console.error("sheet: a folder with pictures is needed"); return 2; }
- const file = out || path.join(dir, "_sheet.html");
- const rel = (f) => path.relative(path.dirname(path.resolve(file)), path.resolve(f)).split(path.sep).join("/");
- const rows = svgFiles([dir]).map((f) => {
- const src = rel(f.path), name = path.basename(f.path, ".svg");
- return `<tr><td>${name}</td>${[36, 48, 84, 108].map((s) => `<td><img src="${src}" width="${s}" height="${s}" alt=""></td>`).join("")}</tr>`;
- });
- fs.writeFileSync(file, `<!doctype html><meta charset="utf-8"><title>Pictures: ${path.basename(path.resolve(dir))}</title>
- <style>body{font:14px system-ui;margin:24px;background:#F7F7F5;color:#37352F}table{border-collapse:separate;border-spacing:12px}td{background:#fff;border-radius:10px;padding:8px;vertical-align:middle}</style>
- <p>Sizes: the "Names" strip 36, small card 48, medium 84, large 108.</p>
- <table>${rows.join("\n")}</table>\n`);
- console.log(`Overview sheet: ${file} (pictures: ${rows.length}). The file is auxiliary, do not commit it to the base.`);
- return 0;
- }
- console.error(`No command ${cmd}.\n\n${HELP}`);
- return 2;
- }
- if (import.meta.url === `file://${process.argv[1]}` || process.argv[1]?.endsWith("shtukbase-icons.js")) process.exitCode = main(process.argv.slice(2));