// Regenerates src/lint/tailwind-palette.ts from the installed Tailwind CSS // default theme, so palette lookups match the real values exactly. import fs from 'node:fs'; import path from 'node:path'; import { createRequire } from 'node:module'; const root = path.resolve(import.meta.dirname, '..'); const require = createRequire(path.join(root, 'examples/shadcn-demo/package.json')); const pkgPath = require.resolve('tailwindcss/package.json'); const { version } = JSON.parse(fs.readFileSync(pkgPath, 'utf8')); const css = fs.readFileSync(path.join(path.dirname(pkgPath), 'theme.css'), 'utf8'); const palette = {}; for (const [, name, shade, value] of css.matchAll(/--color-([a-z]+)-(\d+):\s*([^;]+);/g)) { (palette[name] ??= {})[shade] = value.trim(); } // `--color-white` and `--color-black` have no shade: `bg-white`, `text-black`. const keywords = {}; for (const [, name, value] of css.matchAll(/--color-(white|black):\s*([^;]+);/g)) { keywords[name] = value.trim(); } const body = Object.entries(palette) .map(([name, shades]) => { const entries = Object.entries(shades) .map(([shade, value]) => ` ${shade}: '${value}',`) .join('\n'); return ` ${name}: {\n${entries}\n },`; }) .join('\n'); const out = `// Generated by scripts/build-palette.mjs from tailwindcss@${version}/theme.css. Do not edit. /** Tailwind's default color palette: name → shade → OKLCH value. */ export const TAILWIND_PALETTE: Record> = { ${body} }; /** Tailwind's colors without shades: \`white\` and \`black\`. */ export const TAILWIND_KEYWORD_COLORS: Record = { ${Object.entries(keywords) .map(([name, value]) => ` ${name}: '${value}',`) .join('\n')} }; export const TAILWIND_VERSION = '${version}'; `; fs.writeFileSync(path.join(root, 'src/lint/tailwind-palette.ts'), out); console.log( `wrote src/lint/tailwind-palette.ts (${Object.keys(palette).length} colors, ${Object.keys(keywords).join(' and ')}, tailwindcss@${version})`, );