initial commit
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: Iaade5d67478a437673f2bc2a18e9fb676a6a6964
This commit is contained in:
commit
eb03499933
7 changed files with 1553 additions and 0 deletions
242
scripts/build.mjs
Normal file
242
scripts/build.mjs
Normal file
|
|
@ -0,0 +1,242 @@
|
|||
import StyleDictionary from 'style-dictionary';
|
||||
|
||||
// Raw ramps as custom properties and semantic light/dark layers for CSS
|
||||
StyleDictionary.registerFormat({
|
||||
name: 'frozendev/css',
|
||||
format: ({ dictionary }) => {
|
||||
const ramp = dictionary.allTokens
|
||||
.filter(t => t.path[0] === 'color')
|
||||
.map(t => ` --fd-${t.path.slice(1).join('-')}: ${t.value};`)
|
||||
.join('\n');
|
||||
|
||||
const light = dictionary.allTokens
|
||||
.filter(t => t.path[0] === 'semantic' && t.path[1] === 'light')
|
||||
.map(t => ` --fd-${t.path.slice(2).join('-')}: ${t.value};`)
|
||||
.join('\n');
|
||||
|
||||
const dark = dictionary.allTokens
|
||||
.filter(t => t.path[0] === 'semantic' && t.path[1] === 'dark')
|
||||
.map(t => ` --fd-${t.path.slice(2).join('-')}: ${t.value};`)
|
||||
.join('\n');
|
||||
|
||||
return [
|
||||
`/* FrozenDev Electronics - @generated, do not edit */`,
|
||||
`/* Raw ramps */`,
|
||||
`:root {\n${ramp}\n}`,
|
||||
``,
|
||||
`/* Semantic aliases, light (default) */`,
|
||||
`:root, [data-theme="light"] {\n${light}\n}`,
|
||||
``,
|
||||
`/* Semantic aliases, dark */`,
|
||||
`[data-theme="dark"] {\n${dark}\n}`,
|
||||
].join('\n');
|
||||
},
|
||||
});
|
||||
|
||||
// Ramp variables + semantic maps for SCSS
|
||||
StyleDictionary.registerFormat({
|
||||
name: 'frozendev/scss',
|
||||
format: ({ dictionary }) => {
|
||||
const rampVars = dictionary.allTokens
|
||||
.filter(t => t.path[0] === 'color')
|
||||
.map(t => `$fd-${t.path.slice(1).join('-')}: ${t.value};`)
|
||||
.join('\n');
|
||||
|
||||
const lightMap = dictionary.allTokens
|
||||
.filter(t => t.path[0] === 'semantic' && t.path[1] === 'light')
|
||||
.map(t => ` '${t.path.slice(2).join('-')}': ${t.value},`)
|
||||
.join('\n');
|
||||
|
||||
const darkMap = dictionary.allTokens
|
||||
.filter(t => t.path[0] === 'semantic' && t.path[1] === 'dark')
|
||||
.map(t => ` '${t.path.slice(2).join('-')}': ${t.value},`)
|
||||
.join('\n');
|
||||
|
||||
return [
|
||||
`// FrozenDev Electronics - @generated, do not edit`,
|
||||
``,
|
||||
`// Raw ramps`,
|
||||
rampVars,
|
||||
``,
|
||||
`// Semantic maps`,
|
||||
`$fd-theme-light: (\n${lightMap}\n);`,
|
||||
``,
|
||||
`$fd-theme-dark: (\n${darkMap}\n);`,
|
||||
].join('\n');
|
||||
},
|
||||
});
|
||||
|
||||
// JS/TS ESM
|
||||
StyleDictionary.registerFormat({
|
||||
name: 'frozendev/js-esm',
|
||||
format: ({ dictionary }) => {
|
||||
const ramps = {};
|
||||
dictionary.allTokens
|
||||
.filter(t => t.path[0] === 'color')
|
||||
.forEach(t => {
|
||||
const [, ramp, stop] = t.path;
|
||||
if (!ramps[ramp]) ramps[ramp] = {};
|
||||
ramps[ramp][stop] = t.value;
|
||||
});
|
||||
|
||||
const semantic = { light: {}, dark: {} };
|
||||
dictionary.allTokens
|
||||
.filter(t => t.path[0] === 'semantic')
|
||||
.forEach(t => {
|
||||
const [, theme, ...key] = t.path;
|
||||
semantic[theme][key.join('-')] = t.value;
|
||||
});
|
||||
|
||||
return [
|
||||
`// FrozenDev Electronics - @generated, do not edit`,
|
||||
`export const ramps = ${JSON.stringify(ramps, null, 2)};`,
|
||||
``,
|
||||
`export const semantic = ${JSON.stringify(semantic, null, 2)};`,
|
||||
``,
|
||||
`export const tokens = { ramps, semantic };`,
|
||||
`export default tokens;`,
|
||||
].join('\n');
|
||||
},
|
||||
});
|
||||
|
||||
// Tailwind config snippet
|
||||
StyleDictionary.registerFormat({
|
||||
name: 'frozendev/tailwind',
|
||||
format: ({ dictionary }) => {
|
||||
const colors = {};
|
||||
dictionary.allTokens
|
||||
.filter(t => t.path[0] === 'color')
|
||||
.forEach(t => {
|
||||
const [, ramp, stop] = t.path;
|
||||
const camel = ramp.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
|
||||
if (!colors[camel]) colors[camel] = {};
|
||||
colors[camel][stop] = t.value;
|
||||
});
|
||||
|
||||
// Semantic CSS var references for Tailwind
|
||||
const semanticLight = {};
|
||||
dictionary.allTokens
|
||||
.filter(t => t.path[0] === 'semantic' && t.path[1] === 'light')
|
||||
.forEach(t => {
|
||||
semanticLight[t.path.slice(2).join('-')] = `var(--fd-${t.path.slice(2).join('-')})`;
|
||||
});
|
||||
|
||||
return [
|
||||
`// FrozenDev Electronics - @generated, do not edit`,
|
||||
`// Usage: spread into your Tailwind config's theme.colors`,
|
||||
``,
|
||||
`export const frozendevRamps = ${JSON.stringify(colors, null, 2)};`,
|
||||
``,
|
||||
`export const frozendevSemantic = ${JSON.stringify(semanticLight, null, 2)};`,
|
||||
``,
|
||||
`// In tailwind.config.js:`,
|
||||
`// import { frozendevRamps, frozendevSemantic } from '@frozendev/tokens/tailwind';`,
|
||||
`// theme: { extend: { colors: { ...frozendevRamps, fd: frozendevSemantic } } }`,
|
||||
].join('\n');
|
||||
},
|
||||
});
|
||||
|
||||
// Android XML
|
||||
StyleDictionary.registerFormat({
|
||||
name: 'frozendev/android-xml',
|
||||
format: ({ dictionary }) => {
|
||||
const items = dictionary.allTokens
|
||||
.filter(t => t.path[0] === 'color')
|
||||
.map(t => {
|
||||
const name = `fd_${t.path.slice(1).join('_')}`;
|
||||
// Android color format: #AARRGGBB; our tokens are opaque, so prepend FF
|
||||
const hex = t.value.replace('#', '#FF');
|
||||
return ` <color name="${name}">${hex}</color>`;
|
||||
})
|
||||
.join('\n');
|
||||
|
||||
return `<?xml version="1.0" encoding="utf-8"?>\n<!-- FrozenDev Electronics - @generated, do not edit -->\n<resources>\n${items}\n</resources>`;
|
||||
},
|
||||
});
|
||||
|
||||
// iOS Swift
|
||||
StyleDictionary.registerFormat({
|
||||
name: 'frozendev/ios-swift',
|
||||
format: ({ dictionary }) => {
|
||||
const props = dictionary.allTokens
|
||||
.filter(t => t.path[0] === 'color')
|
||||
.map(t => {
|
||||
const name = t.path.slice(1)
|
||||
.join('-')
|
||||
.replace(/-([a-z0-9])/g, (_, c) => c.toUpperCase());
|
||||
const hex = t.value.replace('#', '');
|
||||
const r = parseInt(hex.slice(0,2),16)/255;
|
||||
const g = parseInt(hex.slice(2,4),16)/255;
|
||||
const b = parseInt(hex.slice(4,6),16)/255;
|
||||
return ` static let ${name} = Color(red: ${r.toFixed(4)}, green: ${g.toFixed(4)}, blue: ${b.toFixed(4)})`;
|
||||
})
|
||||
.join('\n');
|
||||
|
||||
return [
|
||||
`// FrozenDev Electronics - @generated, do not edit`,
|
||||
`import SwiftUI`,
|
||||
``,
|
||||
`public struct FrozenDevColors {`,
|
||||
props,
|
||||
`}`,
|
||||
].join('\n');
|
||||
},
|
||||
});
|
||||
|
||||
// Build configuration
|
||||
// See:
|
||||
// <https://styledictionary.com/reference/config/>
|
||||
const sd = new StyleDictionary({
|
||||
source: ['tokens/**/*.json'],
|
||||
platforms: {
|
||||
css: {
|
||||
transformGroup: 'css',
|
||||
buildPath: 'outputs/css/',
|
||||
files: [{
|
||||
destination: 'tokens.css',
|
||||
format: 'frozendev/css',
|
||||
}],
|
||||
},
|
||||
scss: {
|
||||
transformGroup: 'scss',
|
||||
buildPath: 'outputs/scss/',
|
||||
files: [{
|
||||
destination: '_tokens.scss',
|
||||
format: 'frozendev/scss',
|
||||
}],
|
||||
},
|
||||
js: {
|
||||
transformGroup: 'js',
|
||||
buildPath: 'outputs/js/',
|
||||
files: [
|
||||
{
|
||||
destination: 'tokens.mjs',
|
||||
format: 'frozendev/js-esm',
|
||||
},
|
||||
{
|
||||
destination: 'tailwind.mjs',
|
||||
format: 'frozendev/tailwind',
|
||||
},
|
||||
],
|
||||
},
|
||||
android: {
|
||||
transformGroup: 'android',
|
||||
buildPath: 'outputs/android/',
|
||||
files: [{
|
||||
destination: 'frozendev_colors.xml',
|
||||
format: 'frozendev/android-xml',
|
||||
}],
|
||||
},
|
||||
ios: {
|
||||
transformGroup: 'ios',
|
||||
buildPath: 'outputs/ios/',
|
||||
files: [{
|
||||
destination: 'FrozenDevColors.swift',
|
||||
format: 'frozendev/ios-swift',
|
||||
}],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await sd.buildAllPlatforms();
|
||||
console.log('\n✓ FrozenDev tokens built.\n');
|
||||
201
scripts/preview.mjs
Normal file
201
scripts/preview.mjs
Normal file
|
|
@ -0,0 +1,201 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
|
||||
const input = process.argv[2] ?? "tokens";
|
||||
const output = process.argv[3] ?? "color-preview.html";
|
||||
|
||||
function isColor(value) {
|
||||
return (
|
||||
typeof value === "string" &&
|
||||
(
|
||||
/^#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(value) ||
|
||||
/^rgb\(/i.test(value) ||
|
||||
/^rgba\(/i.test(value) ||
|
||||
/^hsl\(/i.test(value) ||
|
||||
/^hsla\(/i.test(value) ||
|
||||
/^oklch\(/i.test(value) ||
|
||||
/^color\(/i.test(value)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function unwrapToken(node) {
|
||||
if (!node || typeof node !== "object") return node;
|
||||
if ("$value" in node) return node.$value;
|
||||
if ("value" in node) return node.value;
|
||||
return node;
|
||||
}
|
||||
|
||||
async function walkFiles(dir) {
|
||||
const out = [];
|
||||
const entries = await fs.readdir(dir, { withFileTypes: true });
|
||||
|
||||
for (const entry of entries) {
|
||||
const full = path.join(dir, entry.name);
|
||||
|
||||
if (entry.isDirectory()) {
|
||||
out.push(...await walkFiles(full));
|
||||
} else if (/\.(json|tokens\.json)$/i.test(entry.name)) {
|
||||
out.push(full);
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
function collectColors(obj, prefix = [], source = "unknown") {
|
||||
const colors = [];
|
||||
|
||||
if (!obj || typeof obj !== "object") return colors;
|
||||
|
||||
for (const [key, raw] of Object.entries(obj)) {
|
||||
const value = unwrapToken(raw);
|
||||
const name = [...prefix, key];
|
||||
|
||||
if (isColor(value)) {
|
||||
colors.push({
|
||||
name: name.join("."),
|
||||
value,
|
||||
source
|
||||
});
|
||||
} else if (value && typeof value === "object") {
|
||||
colors.push(...collectColors(value, name, source));
|
||||
}
|
||||
}
|
||||
|
||||
return colors;
|
||||
}
|
||||
|
||||
function htmlEscape(s) {
|
||||
return String(s)
|
||||
.replaceAll("&", "&")
|
||||
.replaceAll("<", "<")
|
||||
.replaceAll(">", ">")
|
||||
.replaceAll('"', """);
|
||||
}
|
||||
|
||||
function makeHtml(colors) {
|
||||
const cards = colors.map(c => `
|
||||
<section class="card">
|
||||
<div class="swatch" style="background: ${htmlEscape(c.value)}"></div>
|
||||
<div class="meta">
|
||||
<code>${htmlEscape(c.name)}</code>
|
||||
<button data-copy="${htmlEscape(c.value)}">${htmlEscape(c.value)}</button>
|
||||
<small>${htmlEscape(c.source)}</small>
|
||||
</div>
|
||||
</section>
|
||||
`).join("\n");
|
||||
|
||||
return `<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Color Preview</title>
|
||||
<style>
|
||||
:root {
|
||||
font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
|
||||
color: #111;
|
||||
background: #f5f5f5;
|
||||
}
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
padding: 32px;
|
||||
}
|
||||
|
||||
h1 {
|
||||
margin: 0 0 24px;
|
||||
font-size: 28px;
|
||||
}
|
||||
|
||||
.grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.card {
|
||||
overflow: hidden;
|
||||
border-radius: 14px;
|
||||
background: white;
|
||||
box-shadow: 0 1px 4px rgb(0 0 0 / 0.12);
|
||||
}
|
||||
|
||||
.swatch {
|
||||
height: 120px;
|
||||
border-bottom: 1px solid rgb(0 0 0 / 0.08);
|
||||
}
|
||||
|
||||
.meta {
|
||||
display: grid;
|
||||
gap: 8px;
|
||||
padding: 14px;
|
||||
}
|
||||
|
||||
code {
|
||||
font-size: 13px;
|
||||
word-break: break-word;
|
||||
}
|
||||
|
||||
button {
|
||||
width: fit-content;
|
||||
border: 1px solid #ccc;
|
||||
border-radius: 999px;
|
||||
background: #fafafa;
|
||||
padding: 6px 10px;
|
||||
font: inherit;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
small {
|
||||
color: #666;
|
||||
word-break: break-word;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Color Preview</h1>
|
||||
<div class="grid">
|
||||
${cards || "<p>No colors found.</p>"}
|
||||
</div>
|
||||
|
||||
<script>
|
||||
document.addEventListener("click", async event => {
|
||||
const button = event.target.closest("button[data-copy]");
|
||||
if (!button) return;
|
||||
|
||||
await navigator.clipboard.writeText(button.dataset.copy);
|
||||
const old = button.textContent;
|
||||
button.textContent = "Copied";
|
||||
setTimeout(() => button.textContent = old, 900);
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>`;
|
||||
}
|
||||
|
||||
const stat = await fs.stat(input).catch(() => null);
|
||||
|
||||
if (!stat) {
|
||||
console.error(`Input not found: ${input}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const files = stat.isDirectory() ? await walkFiles(input) : [input];
|
||||
|
||||
const colors = [];
|
||||
|
||||
for (const file of files) {
|
||||
const json = JSON.parse(await fs.readFile(file, "utf8"));
|
||||
colors.push(...collectColors(json, [], file));
|
||||
}
|
||||
|
||||
colors.sort((a, b) => a.name.localeCompare(b.name));
|
||||
|
||||
await fs.mkdir(path.dirname(output), { recursive: true });
|
||||
await fs.writeFile(output, makeHtml(colors), "utf8");
|
||||
|
||||
console.log(`Wrote ${output}`);
|
||||
console.log(`Found ${colors.length} colors.`);
|
||||
Loading…
Add table
Add a link
Reference in a new issue