Repository Public
1#!/usr/bin/env node2/**3 * =============================================================================4 * SP BOUCHER | INFRASTRUCTURE & AUTOMATION5 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING6 * =============================================================================7 *8 * Project : MacLustr Command Center (MCC)9 * File : packages/ui-tokens/build.mjs10 * Module : ui-tokens11 * Repository : https://git.spboucher.ai/maclustr-command-center12 * Project Website : https://clustr.spboucher.ai13 * Created : 2026-11-1814 * Last Modified : 2026-11-1815 *16 * Author : Simon-Pierre Boucher17 * Position : Professor of Finance18 * Institution : Université du Québec en Outaouais (UQO)19 * Organization : SP Boucher20 * Website : https://spboucher.ai21 * Git : https://git.spboucher.ai22 *23 * Preferred Contact: admin@spboucher.ai24 * Technical Contact: dev@spboucher.ai25 * Security/Admin : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243// Generates dist/tokens.css (web) and dist/Tokens.swift (Apple) from tokens.json.44// No dependencies. Run: node packages/ui-tokens/build.mjs (--check fails if outputs are stale)45import { readFileSync, writeFileSync, mkdirSync, existsSync } from "node:fs";46import { dirname, join } from "node:path";47import { fileURLToPath } from "node:url";4849const here = dirname(fileURLToPath(import.meta.url));50const t = JSON.parse(readFileSync(join(here, "tokens.json"), "utf8"));51const cssName = (k) => k.replace(/\./g, "-");52const camel = (k) => k.replace(/[.\-_]+(\w)/g, (_, c) => c.toUpperCase()).replace(/^(\d)/, "n$1");5354// WCAG contrast check: text and state colours must reach AA on their backgrounds.55const lum = (hex) => {56 const [r, g, b] = [1, 3, 5].map((i) => parseInt(hex.slice(i, i + 2), 16) / 255)57 .map((c) => (c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4));58 return 0.2126 * r + 0.7152 * g + 0.0722 * b;59};60const contrast = (a, b) => { const [x, y] = [lum(a), lum(b)].sort((p, q) => q - p); return (x + 0.05) / (y + 0.05); };61const problems = [];62for (const [theme, c] of Object.entries(t.color)) {63 const need = (fg, bg, min) => { const r = contrast(c[fg], c[bg]); if (r < min) problems.push(`${theme}: ${fg} on ${bg} = ${r.toFixed(2)} (< ${min})`); };64 for (const bg of ["bg.canvas", "bg.surface", "bg.raised"]) {65 need("text.primary", bg, 7); need("text.secondary", bg, 4.5); need("text.tertiary", bg, 4.5);66 for (const s of ["ok", "warning", "critical", "info", "stale", "maintenance"]) need(`state.${s}`, bg, 4.5);67 need("state.offline", bg, 3); need("accent.default", bg, 4.5); need("border.strong", bg, 1.5);68 }69 for (const s of ["ok", "warning", "critical", "info", "stale", "offline", "maintenance"]) need(`state.${s}`, `state.${s}.muted`, s === "offline" ? 3 : 4.5);70 need("accent.contrast", "accent.default", 4.5);71}72if (problems.length) { console.error("Contrast failures:\n " + problems.join("\n ")); process.exit(1); }7374// ---- CSS75const vars = (c) => Object.entries(c).map(([k, v]) => ` --mcc-color-${cssName(k)}: ${v};`).join("\n");76const scalar = (prefix, obj, unit = "px") => Object.entries(obj).filter(([, v]) => typeof v !== "object")77 .map(([k, v]) => ` --mcc-${prefix}-${cssName(k)}: ${typeof v === "number" && unit ? v + unit : v};`).join("\n");78let css = `/* GENERATED by packages/ui-tokens/build.mjs — do not edit. */\n:root {\n color-scheme: dark;\n${vars(t.color.dark)}\n`;79css += scalar("space", t.space) + "\n" + scalar("radius", t.radius) + "\n" + scalar("size", t.size) + "\n";80css += scalar("font-size", t.font.size) + "\n" + scalar("font-weight", t.font.weight, "") + "\n" + scalar("leading", t.font.leading, "") + "\n";81css += Object.entries(t.font.tracking).map(([k, v]) => ` --mcc-tracking-${k}: ${v}em;`).join("\n") + "\n";82css += ` --mcc-font-sans: ${t.font["family.sans"]};\n --mcc-font-mono: ${t.font["family.mono"]};\n`;83css += Object.entries(t.motion).map(([k, v]) => ` --mcc-motion-${cssName(k)}: ${typeof v === "number" ? v + "ms" : v};`).join("\n") + "\n";84css += Object.entries(t.elevation).map(([k, v]) => ` --mcc-elevation-${k}: ${v};`).join("\n") + "\n}\n";85css += `:root[data-theme="light"] {\n color-scheme: light;\n${vars(t.color.light)}\n}\n`;86css += `@media (prefers-reduced-motion: reduce) {\n :root { --mcc-motion-instant: 0ms; --mcc-motion-fast: 0ms; --mcc-motion-base: 0ms; --mcc-motion-slow: 0ms; }\n}\n`;8788// ---- TypeScript (semantic maps + chart palette for canvas drawing)89let ts = `// GENERATED by packages/ui-tokens/build.mjs — do not edit.\n`;90ts += `export const semantic = ${JSON.stringify(t.semantic, null, 2)} as const;\n`;91ts += `export const colorVar = (token: string) => \`var(--mcc-color-\${token.replace(/\\./g, "-")})\`;\n`;92ts += `export const chartTokens = ["chart.1","chart.2","chart.3","chart.4","chart.5","chart.6"] as const;\n`;9394// ---- Swift95const hex = (h) => { const n = h.slice(1); const a = n.length === 8 ? parseInt(n.slice(6), 16) / 255 : 1;96 return `Color(.sRGB, red: ${(parseInt(n.slice(0, 2), 16) / 255).toFixed(4)}, green: ${(parseInt(n.slice(2, 4), 16) / 255).toFixed(4)}, blue: ${(parseInt(n.slice(4, 6), 16) / 255).toFixed(4)}, opacity: ${a.toFixed(3)})`; };97let sw = `// GENERATED by packages/ui-tokens/build.mjs — do not edit.\nimport SwiftUI\n\n`;98sw += `/// Design tokens shared with the web app. Colours adapt to the effective colour scheme; dark is the product default.\npublic enum Tokens {\n`;99sw += ` public struct Palette: Sendable {\n` + Object.keys(t.color.dark).map((k) => ` public let ${camel(k)}: Color`).join("\n") + `\n }\n\n`;100for (const theme of ["dark", "light"]) {101 sw += ` public static let ${theme} = Palette(\n` + Object.entries(t.color[theme]).map(([k, v]) => ` ${camel(k)}: ${hex(v)}`).join(",\n") + `\n )\n\n`;102}103sw += ` public static func palette(_ scheme: ColorScheme) -> Palette { scheme == .light ? light : dark }\n\n`;104const group = (name, obj, type = "CGFloat") => ` public enum ${name} {\n` + Object.entries(obj).filter(([, v]) => typeof v === "number")105 .map(([k, v]) => ` public static let ${camel(/^\d/.test(k) ? "s" + k : k)}: ${type} = ${v}`).join("\n") + `\n }\n\n`;106sw += group("Space", t.space) + group("Radius", t.radius) + group("FontSize", t.font.size) + group("Size", t.size);107sw += ` public enum Motion {\n` + Object.entries(t.motion).filter(([, v]) => typeof v === "number").map(([k, v]) => ` public static let ${camel(k)}: Double = ${v / 1000}`).join("\n") + `\n }\n`;108sw += `}\n`;109110const outputs = { "dist/tokens.css": css, "dist/tokens.ts": ts, "dist/Tokens.swift": sw };111const check = process.argv.includes("--check");112let stale = false;113mkdirSync(join(here, "dist"), { recursive: true });114for (const [rel, body] of Object.entries(outputs)) {115 const p = join(here, rel);116 if (check) { if (!existsSync(p) || readFileSync(p, "utf8") !== body) { console.error(`stale: ${rel}`); stale = true; } }117 else writeFileSync(p, body);118}119if (stale) process.exit(1);120console.log(check ? "tokens up to date" : `generated ${Object.keys(outputs).join(", ")}`);121