forked from kiliman/shadcn-custom-theme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
95 lines (82 loc) · 2.43 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// This tool will generate a custom shadcn/ui theme
// You can specify the primary, secondary, accent, and gray colors
const path = require("path");
const twColors = require("tailwindcss/colors");
const hex2hsl = require("hex-to-hsl");
let template = require("./theme.json");
let customColors = {
primary: null,
secondary: "gray",
accent: "gray",
gray: "gray",
};
function main() {
if (process.argv.length < 3) {
console.error(
`USAGE: shadcn-custom-theme primary=COLOR [secondary=COLOR] [accent=COLOR] [gray=COLOR] [template=TEMPLATE.json]`
);
process.exit(1);
}
process.argv.slice(2).forEach((arg) => {
const [key, value] = arg.split("=");
if (!key || !value) return;
if (key === "template") {
template = require(path.join(process.cwd(), value));
} else {
customColors[key] = value;
}
});
if (customColors.primary === null) {
console.error("primary color is required");
process.exit(1);
}
let theme = {
light: generateTheme("light", customColors),
dark: generateTheme("dark", customColors),
};
console.log("@layer base {");
console.log(" :root {");
dumpTheme(theme.light);
console.log(" }\n");
console.log(" .dark {");
dumpTheme(theme.dark);
console.log(" }");
console.log("}");
}
function dumpTheme(theme) {
Object.entries(theme).forEach(([key, value]) => {
console.log(` ${key}: ${value};`);
});
}
function generateTheme(theme, customColors) {
let customTheme = {};
Object.entries(template[theme]).forEach(([key, value]) => {
// if not a tailwind color, just return it directly
if (!value.match(/black|white|\w+-\d+/)) {
customTheme[key] = value;
return;
}
let [color, tint] = value.split("-");
if (!tint) {
customTheme[key] = getHsl(color);
return;
}
let customColor = customColors[color];
// hard-coded color like red-500 so just return it directly
if (!customColor) {
let newColor = twColors[color][tint];
customTheme[key] = getHsl(newColor, value);
return;
}
let newColor = twColors[customColor][tint];
customTheme[key] = getHsl(newColor, `${customColor}-${tint}`);
});
return customTheme;
}
function getHsl(color, twColor) {
if (color === "white") return "0 0% 100% /* white */";
if (color === "black") return "0 0% 0% /* black */";
const [h, s, l] = hex2hsl(color);
return `${h} ${s}% ${l}% /* ${twColor} */`;
}
module.exports = { main };