-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcss-props-generator.js
78 lines (64 loc) · 2.31 KB
/
css-props-generator.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
import { writeFileSync } from "node:fs";
import * as prettier from "prettier";
import {default as config} from "./tailwind.config.js";
/*
Converts the tailwind config elements into custom props.
*/
export const generateCSSProps = async () => {
let result = "";
const groups = [
{key: "spacing", prefix: "space"},
{key: "borderWidth", prefix: "border"},
{key: "borderRadius", prefix: "radius"},
{key: "fontSize", prefix: "text"},
{key: "fontFamily", prefix: "font"},
{key: "fontWeight", prefix: "font"},
{key: "maxWidth", prefix: "max-w"}
];
const extendedGroups = [
{key: "colors", prefix: "color"}
];
// Add a note that this is auto generated
result += `
/* VARIABLES GENERATED WITH TAILWIND CONFIG ON ${new Date().toLocaleDateString()}.
Tokens location: ./tailwind.config.js */
:root {
`;
// Loop each group's keys, use that and the associated
// property to define a :root custom prop
groups.forEach(({key, prefix}) => {
const group = config.theme[key];
if (!group) {
return;
}
Object.keys(group).forEach(key => {
if (key === "DEFAULT") {
result += `--${prefix}: ${Array.isArray(group[key]) ? group[key][0] : group[key]};`;
} else {
result += `--${prefix}-${key.replace(".", "_")}: ${Array.isArray(group[key]) ? group[key][0] : group[key]};`;
}
});
});
extendedGroups.forEach(({key, prefix}) => {
const group = config.theme.extend[key];
if (!group) {
return;
}
Object.keys(group).forEach(key => {
if (key === "DEFAULT") {
result += `--${prefix}: ${Array.isArray(group[key]) ? group[key][0] : group[key]};`;
} else {
result += `--${prefix}-${key.replace(".", "_")}: ${Array.isArray(group[key]) ? group[key][0] : group[key]};`;
}
});
});
// Close the :root block
result += `
}
`;
// Make the CSS readable to help people with auto-complete in their editors
result = await prettier.format(result, {parser: "scss", tabWidth: 4});
// Push this file into the CSS dir, ready to go
writeFileSync("./resources/css/_tokens.css", result);
};
generateCSSProps();