ShtukbaseStandard
package file→ .shtukbase/packages/lenses/kit/bin/shtukbase-icons.js

kit/bin/shtukbase-icons.js

134 lines, 8.0 KB.

  1. #!/usr/bin/env node
  2. // shtukbase-icons - card pictures in the style of the kit (patterns/pictures.md). Works under Bun and Node 20+.
  3. // new <file.svg> [--color blue] put a picture template (canvas 96×96, two shapes from the palette)
  4. // check <file.svg | folder>... check the style: viewBox 96×96, no text and no raster, colors only from the palette
  5. // sheet <folder> [-o <file.html>] overview sheet: all pictures of the folder in three card sizes (48, 84, 108)
  6. import fs from "node:fs";
  7. import path from "node:path";
  8. // The style palette: the main Notion colors and their shades for details (highlight, shadow, second face). There are no other colors.
  9. export const PALETTE = {
  10. red: { main: "#FF7369", light: ["#FFC2BD", "#FFE4E1"], dark: [] },
  11. orange: { main: "#FFA344", light: [], dark: ["#E8842A"] },
  12. yellow: { main: "#FFDC49", light: ["#FFF0A8"], dark: ["#CB912F", "#9A6700"] },
  13. green: { main: "#4DAB9A", light: ["#E4F1EE", "#9CC8BC", "#8FD0C3", "#7CE0B0", "#6FE3C1"], dark: [] },
  14. blue: { main: "#529CCA", light: ["#DCEEF7", "#A8D3EC"], dark: ["#337EA9"] },
  15. purple: { main: "#9A6DD7", light: ["#EDE4F7"], dark: ["#7A52B8"] },
  16. pink: { main: "#E255A1", light: [], dark: [] },
  17. neutral: { main: null, light: ["#FFFFFF", "#B4B2AC"], dark: ["#3D4A66"] },
  18. };
  19. const MAIN = new Set(Object.values(PALETTE).map((c) => c.main).filter(Boolean));
  20. const ALLOWED = new Set(Object.values(PALETTE).flatMap((c) => [c.main, ...c.light, ...c.dark]).filter(Boolean));
  21. const HELP = `shtukbase-icons - card pictures in the style of the kit (apps/standard/packages/lenses/kit/patterns/pictures.md)
  22. new <file.svg> [--color <color>] template: canvas 96×96, a main shape and a detail; color is red, orange, yellow,
  23. green, blue, purple or pink; blue by default
  24. check <file.svg | folder>... check the style; exit code 1 if there are errors
  25. sheet <folder> [-o <file.html>] overview sheet of the pictures of a folder in card sizes (<folder>/_sheet.html by default)`;
  26. function template(color) {
  27. const c = PALETTE[color] || PALETTE.blue;
  28. const light = c.light[0] || "#FFFFFF";
  29. const other = color === "yellow" ? PALETTE.red.main : PALETTE.yellow.main;
  30. return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 96 96" width="96" height="96">
  31. <!-- Style: apps/standard/packages/lenses/kit/patterns/pictures.md. Flat, no background tile and no outline, 2-4 palette colors, no text. -->
  32. <!-- Main shape: large, rounded corners, fills the field from about 8 to 88. -->
  33. <rect x="14" y="22" width="68" height="56" rx="12" fill="${c.main}"/>
  34. <!-- Detail in a light shade of the same color: a highlight or a second face. -->
  35. <rect x="24" y="32" width="30" height="10" rx="5" fill="${light}"/>
  36. <!-- Second palette color: an accent that tells what the thing is. -->
  37. <circle cx="66" cy="62" r="12" fill="${other}"/>
  38. <!-- Thin white details only inside colored shapes. -->
  39. <path d="M60 62h12" stroke="#FFFFFF" stroke-width="4" fill="none" stroke-linecap="round"/>
  40. </svg>
  41. `;
  42. }
  43. /** Checks one SVG: {errors: [], warnings: []}. */
  44. export function checkSvg(text) {
  45. const errors = [], warnings = [];
  46. const root = /<svg\b[^>]*>/i.exec(text);
  47. if (!root) return { errors: ["no root <svg>"], warnings };
  48. if (!/viewBox="0 0 96 96"/.test(root[0])) errors.push('viewBox is not "0 0 96 96"');
  49. if (/<text\b/i.test(text)) errors.push("text inside the picture (<text>): the caption belongs on the card, not in the picture");
  50. if (/<image\b/i.test(text)) errors.push("a raster insert (<image>): pictures are drawn with shapes only");
  51. if (/<(linearGradient|radialGradient|filter)\b/i.test(text)) errors.push("gradient or filter: the style is flat");
  52. const body = text.replace(/<!--[\s\S]*?-->/g, "");
  53. const colors = [...body.matchAll(/#[0-9a-fA-F]{6}\b|#[0-9a-fA-F]{3}\b/g)].map((m) => m[0].toUpperCase());
  54. const named = [...body.matchAll(/(?:fill|stroke)="(?!none|#|url)([a-zA-Z]+)"/g)].map((m) => m[1]);
  55. const bad = [...new Set([...colors.filter((c) => !ALLOWED.has(c)), ...named])];
  56. if (bad.length) errors.push(`colors not from the palette: ${bad.join(", ")}`);
  57. const mains = new Set(colors.filter((c) => MAIN.has(c)));
  58. if (mains.size < 2) warnings.push(`the picture has ${mains.size} main palette colors; the style has several (2-4)`);
  59. // background tile: a light rectangle over almost the whole canvas under the picture
  60. for (const m of body.matchAll(/<rect\b[^>]*>/g)) {
  61. const a = (k) => Number((new RegExp(`\\b${k}="([\\d.]+)"`).exec(m[0]) || [])[1] || 0);
  62. 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)`);
  63. }
  64. return { errors, warnings };
  65. }
  66. function svgFiles(paths) {
  67. const out = [];
  68. for (const p of paths) {
  69. if (!fs.existsSync(p)) { out.push({ path: p, missing: true }); continue; }
  70. if (fs.statSync(p).isDirectory()) {
  71. for (const f of fs.readdirSync(p).filter((f) => f.endsWith(".svg")).sort()) out.push({ path: path.join(p, f) });
  72. } else out.push({ path: p });
  73. }
  74. return out;
  75. }
  76. function main(argv) {
  77. const cmd = argv.shift();
  78. if (!cmd || cmd === "-h" || cmd === "--help") { console.log(HELP); return cmd ? 0 : 1; }
  79. if (cmd === "new") {
  80. const ci = argv.indexOf("--color");
  81. let color = ci >= 0 ? argv.splice(ci, 2)[1] : "blue";
  82. const file = argv[0];
  83. if (!file) { console.error("new: a path to an .svg file is needed"); return 2; }
  84. if (!PALETTE[color] || color === "neutral") { console.error(`new: no color ${color}`); return 2; }
  85. if (fs.existsSync(file)) { console.error(`new: ${file} already exists, not overwriting`); return 1; }
  86. fs.mkdirSync(path.dirname(file), { recursive: true });
  87. fs.writeFileSync(file, template(color));
  88. console.log(`Template: ${file}. Draw the thing with large shapes, then: shtukbase-icons check ${file}`);
  89. return 0;
  90. }
  91. if (cmd === "check") {
  92. if (!argv.length) { console.error("check: a file or folder is needed"); return 2; }
  93. let nErr = 0, nWarn = 0, n = 0;
  94. for (const f of svgFiles(argv)) {
  95. if (f.missing) { console.log(`ERROR ${f.path}: no such file`); nErr++; continue; }
  96. n++;
  97. const { errors, warnings } = checkSvg(fs.readFileSync(f.path, "utf8"));
  98. for (const e of errors) console.log(`ERROR ${f.path}: ${e}`);
  99. for (const w of warnings) console.log(`WARNING ${f.path}: ${w}`);
  100. nErr += errors.length; nWarn += warnings.length;
  101. }
  102. console.log(`Pictures: ${n}; errors: ${nErr}; warnings: ${nWarn}`);
  103. return nErr ? 1 : 0;
  104. }
  105. if (cmd === "sheet") {
  106. const oi = argv.indexOf("-o");
  107. const out = oi >= 0 ? argv.splice(oi, 2)[1] : null;
  108. const dir = argv[0];
  109. if (!dir || !fs.existsSync(dir)) { console.error("sheet: a folder with pictures is needed"); return 2; }
  110. const file = out || path.join(dir, "_sheet.html");
  111. const rel = (f) => path.relative(path.dirname(path.resolve(file)), path.resolve(f)).split(path.sep).join("/");
  112. const rows = svgFiles([dir]).map((f) => {
  113. const src = rel(f.path), name = path.basename(f.path, ".svg");
  114. return `<tr><td>${name}</td>${[36, 48, 84, 108].map((s) => `<td><img src="${src}" width="${s}" height="${s}" alt=""></td>`).join("")}</tr>`;
  115. });
  116. fs.writeFileSync(file, `<!doctype html><meta charset="utf-8"><title>Pictures: ${path.basename(path.resolve(dir))}</title>
  117. <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>
  118. <p>Sizes: the "Names" strip 36, small card 48, medium 84, large 108.</p>
  119. <table>${rows.join("\n")}</table>\n`);
  120. console.log(`Overview sheet: ${file} (pictures: ${rows.length}). The file is auxiliary, do not commit it to the base.`);
  121. return 0;
  122. }
  123. console.error(`No command ${cmd}.\n\n${HELP}`);
  124. return 2;
  125. }
  126. if (import.meta.url === `file://${process.argv[1]}` || process.argv[1]?.endsWith("shtukbase-icons.js")) process.exitCode = main(process.argv.slice(2));