-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.ts
More file actions
132 lines (112 loc) · 3.6 KB
/
utils.ts
File metadata and controls
132 lines (112 loc) · 3.6 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import fs from "fs";
import { extname } from "path";
import globTool from "glob-promise";
import sass from "sass";
type KeyValuePair = { [key: string]: string };
interface Config {
[key: string]: {
element: string;
css: string;
variants: {
[key: string]: KeyValuePair;
};
compoundVariants: KeyValuePair[];
};
}
export const extractStyles = (path: string) => {
const fileContent =
extname(path) === ".scss"
? sass.compile(path).css
: fs.readFileSync(path).toString();
return fileContent.match(
/(h[1-6]|[a-zA-Z_]*)(?:[.]{1})([a-zA-Z_]+[\w\-_]*)(?:[\s\.\,\{\>#\:]{0})/gim
);
};
export const stylesToConfig = (styles: string[]) => {
const config: Config = {};
styles.forEach((item) => {
const parts = item.split(".");
const element = parts[0];
const className = parts[1];
const chunks = className.split("_");
if (chunks.length === 2) return;
if (chunks.length >= 1) {
const component = chunks[0];
if (!config[component]) {
config[component] = {
variants: {},
compoundVariants: [],
css: component,
element,
};
}
if (chunks.length === 3 || chunks.length === 4) {
const variant = chunks[1];
const option = chunks[2];
if (!config[component].variants[variant]) {
config[component].variants[variant] = {};
}
config[component].variants[variant][option] = className;
} else if (chunks.length > 4) {
const variants = chunks.slice(1, chunks.length);
const vars = variants.reduce((acc, cur, i, arr) => {
if (i % 2 !== 0 || i + 1 >= arr.length) return acc;
acc[cur] = arr[i + 1];
return acc;
}, {} as KeyValuePair);
config[component].compoundVariants.push({
...vars,
css: className,
});
}
}
});
return config;
};
export const generateOutput = (config: Config, cssFilename: string) => {
let s = "";
s += `import { styled } from "@phntms/css-components";\n\n`;
s += `import css from "./${cssFilename}";\n\n`;
Object.keys(config).forEach((key) => {
const hasVariants = Object.keys(config[key].variants).length > 0;
const hasCompoundVariants = config[key].compoundVariants.length > 0;
const componentName = key.charAt(0).toUpperCase() + key.slice(1);
s += `export const ${componentName} = styled("${config[key].element}", {\n`;
s += ` css: css.${key},\n`;
if (hasVariants) {
s += ` variants: {\n`;
Object.keys(config[key].variants).forEach((variant) => {
s += ` ${variant}: {\n`;
Object.keys(config[key].variants[variant]).forEach((option) => {
s += ` ${option}: css.${config[key].variants[variant][option]},\n`;
});
s += ` },\n`;
});
s += ` },\n`;
}
if (hasCompoundVariants) {
s += ` compoundVariants: [\n`;
config[key].compoundVariants.forEach((variant) => {
s += ` {\n`;
Object.keys(variant).forEach((key) => {
s += ` ${key}: css.${variant[key]},\n`;
});
s += ` },\n`;
});
s += ` ],\n`;
}
if (hasVariants) {
s += ` defaultVariants: {\n`;
Object.keys(config[key].variants).forEach((variant) => {
Object.keys(config[key].variants[variant]).forEach((option) => {
if (config[key].variants[variant][option].endsWith("default"))
s += ` ${variant}: "${option}",\n`;
});
});
s += ` },\n`;
}
s += `});\n`;
});
return s;
};
export const findFiles = (glob: string) => globTool(glob);