|
const fs = require('fs'); |
|
|
|
const pseudo = ['hover', 'active', 'focus'] |
|
const prefix = ['text', 'bg']; |
|
const colors = ['primary', 'secondary' , 'success' , 'error' , 'warning']; |
|
const intensity = ['50' , '100' , '200' , '300' , '400' , '500' , '600' , '700' , '800' , '900']; |
|
|
|
const fileContent = []; |
|
|
|
const p = (str) => fileContent.push(str); |
|
p('/* eslint-disable */'); |
|
p('// This file has been auto-generated! Do not modify directly!'); |
|
p('\n'); |
|
p('export type ThemeColor = {'); |
|
p(` colorName: '${colors.join("' | '")}';`); |
|
p(` pseudo?: '${pseudo.join("' | '")}';`); |
|
p(` prefix?: '${prefix.join("' | '")}';`); |
|
p(` intensity?: '${intensity.join("' | '")}';`); |
|
p('};'); |
|
p('\n'); |
|
p('/**'); |
|
p(' * Method is needed because of {@link https://tailwindcss.com/docs/content-configuration#dynamic-class-names}.'); |
|
p(' */'); |
|
p('export const pickColor = ({ colorName, prefix, pseudo, intensity }: ThemeColor) => {'); |
|
p(' if (intensity === undefined && pseudo === undefined && prefix === undefined) {'); |
|
p(` switch (colorName) {`); |
|
for (const color of colors) { |
|
p(` case '${color}':`); |
|
p(` return '${color}';`); |
|
} |
|
p(` default: return 'primary';`); |
|
p(` }`); |
|
p(' } else if (intensity !== undefined && pseudo === undefined && prefix === undefined) {'); |
|
p(` switch (colorName) {`); |
|
for (const color of colors) { |
|
p(` case '${color}':${color === 'primary' ? ' default: ': ''}`); |
|
p(` switch (intensity) {`); |
|
for (const i of intensity) { |
|
p(` case '${i}':`); |
|
p(` return '${color}-${i}';`); |
|
} |
|
p(` default: return 'primary'; // we can naively return primary here due to the DEFAULT in the color map`); |
|
p(` }`); |
|
} |
|
p(` }`); |
|
p(' } else if (intensity === undefined && pseudo === undefined && prefix !== undefined) {'); |
|
p(` switch (colorName) {`); |
|
for (const color of colors) { |
|
p(` case '${color}':${color === 'primary' ? ' default: ': ''}`); |
|
p(` switch (prefix) {`); |
|
for (const pre of prefix) { |
|
p(` case '${pre}':`); |
|
p(` return '${pre}-${color}';`); |
|
} |
|
p(` default: throw new Error(\`Runtime error: there is no prefix "\${prefix}"!\`);`); |
|
p(` }`); |
|
} |
|
p(` }`); |
|
p(' } else if (intensity === undefined && pseudo !== undefined && prefix === undefined) {'); |
|
p(` switch (colorName) {`); |
|
for (const color of colors) { |
|
p(` case '${color}':${color === 'primary' ? ' default: ': ''}`); |
|
p(` switch (pseudo) {`); |
|
for (const pseu of pseudo) { |
|
p(` case '${pseu}':`); |
|
p(` return '${pseu}:${color}';`); |
|
} |
|
p(` default: throw new Error(\`Runtime error: there is no pseudo "\${pseudo}"!\`);`); |
|
p(` }`); |
|
} |
|
p(` }`); |
|
p(' } else if (intensity !== undefined && pseudo !== undefined && prefix === undefined) {'); |
|
p(` switch (colorName) {`); |
|
for (const color of colors) { |
|
p(` case '${color}':${color === 'primary' ? ' default: ': ''}`); |
|
p(` switch (intensity) {`); |
|
for (const i of intensity) { |
|
p(` case '${i}':`); |
|
p(` switch (pseudo) {`); |
|
for (const pseu of pseudo) { |
|
p(` case '${pseu}':`); |
|
p(` return '${pseu}:${color}-${i}';`); |
|
} |
|
p(` default: throw new Error(\`Runtime error: there is no pseudo "\${pseudo}"!\`);`); |
|
p(` }`); |
|
} |
|
p(` default:`); |
|
p(` switch (pseudo) {`); |
|
for (const pseu of pseudo) { |
|
p(` case '${pseu}':`); |
|
p(` return '${pseu}:${color}';`); |
|
} |
|
p(` default: throw new Error(\`Runtime error: there is no pseudo "\${pseudo}" for unknown intensity "\${intensity}"!\`);`); |
|
p(` }`); |
|
p(` }`); |
|
} |
|
p(` }`); |
|
p(' } else if (intensity !== undefined && pseudo === undefined && prefix !== undefined) {'); |
|
p(` switch (colorName) {`); |
|
for (const color of colors) { |
|
p(` case '${color}':${color === 'primary' ? ' default: ': ''}`); |
|
p(` switch (intensity) {`); |
|
for (const i of intensity) { |
|
p(` case '${i}':`); |
|
p(` switch (prefix) {`); |
|
for (const pre of prefix) { |
|
p(` case '${pre}':`); |
|
p(` return '${pre}-${color}-${i}';`); |
|
} |
|
p(` default: throw new Error(\`Runtime error: there is no prefix "\${prefix}"!\`);`); |
|
p(` }`); |
|
} |
|
p(` default: throw new Error(\`Runtime error: there is no prefix "\${prefix}" for unknown intensity "\${intensity}"!\`);`); |
|
p(` }`); |
|
} |
|
p(` }`); |
|
p(' } else if (intensity === undefined && pseudo !== undefined && prefix !== undefined) {'); |
|
p(` switch (colorName) {`); |
|
for (const color of colors) { |
|
p(` case '${color}':${color === 'primary' ? ' default: ': ''}`); |
|
p(` switch (prefix) {`); |
|
for (const pre of prefix) { |
|
p(` case '${pre}':`); |
|
p(` switch (pseudo) {`); |
|
for (const pseu of pseudo) { |
|
p(` case '${pseu}':`); |
|
p(` return '${pseu}:${pre}-${color}';`); |
|
} |
|
p(` default: throw new Error(\`Runtime error: there is no pseudo "\${pseudo}"!\`);`); |
|
p(` }`); |
|
} |
|
p(` default: throw new Error(\`Runtime error: there is no pseudo "\${pseudo}" for unknown prefix "\${prefix}"!\`);`); |
|
p(` }`); |
|
} |
|
p(` }`); |
|
p(' } else if (intensity !== undefined && pseudo !== undefined && prefix !== undefined) {'); |
|
p(` switch (colorName) {`); |
|
for (const color of colors) { |
|
p(` case '${color}':${color === 'primary' ? ' default: ': ''}`); |
|
p(` switch (intensity) {`); |
|
for (const i of intensity) { |
|
p(` case '${i}':`); |
|
p(` switch (pseudo) {`); |
|
for (const pseu of pseudo) { |
|
p(` case '${pseu}':`); |
|
p(` switch (prefix) {`); |
|
for (const pre of prefix) { |
|
p(` case '${pre}':`); |
|
p(` return '${pseu}:${pre}-${color}-${i}';`); |
|
} |
|
p(` default: throw new Error(\`Runtime error: there is no prefix "\${prefix}"!\`);`); |
|
p(` }`); |
|
} |
|
p(` default: throw new Error(\`Runtime error: there is no prefix "\${prefix}" for unknown pseudo "\${pseudo}"!\`);`); |
|
p(` }`); |
|
} |
|
p(` default: throw new Error(\`Runtime error: there is no prefix "\${prefix}" and pseudo "\${pseudo}" for unknown intensity "\${intensity}"!\`);`); |
|
p(` }`); |
|
} |
|
p(` }`); |
|
p(` }`); |
|
p('};'); |
|
p(''); |
|
|
|
const generatedFile = fileContent.join('\n'); |
|
fs.writeFileSync('./gen-tailwind-colormap.ts', generatedFile); |