Skip to content

Commit 69c1284

Browse files
committed
generateGetColorDecisionsTsCode
1 parent 028ab47 commit 69c1284

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

src/bin/css_to_ts/colorDecisions.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import css from "css";
77
import { exclude } from "tsafe/exclude";
88
import { is } from "tsafe/is";
99
import { parseColorOptionName, getThemePath as getColorOptionThemePath } from "./colorOptions";
10+
import * as crypto from "crypto";
11+
import { multiReplace } from "../tools/multiReplace";
1012

1113
const contexts = ["background", "text", "border", "artwork"] as const;
1214

@@ -212,3 +214,63 @@ export function createParseColorDecision(params: {
212214

213215
return { parseColorDecision };
214216
}
217+
218+
export function generateGetColorDecisionsTsCode(colorDecisions: ColorDecision[]): string {
219+
const obj: any = {};
220+
221+
const keyValues: Record<string, string> = {};
222+
223+
colorDecisions.forEach(colorDecision => {
224+
const value = (() => {
225+
const hash = crypto
226+
.createHash("sha256")
227+
.update(colorDecision.themePath.join(""))
228+
.digest("hex");
229+
230+
keyValues[`"${hash}"`] = ["colorOptions", ...colorDecision.optionThemePath].join(".");
231+
232+
return hash;
233+
})();
234+
235+
function req(obj: any, path: string[]): void {
236+
const [propertyName, ...pathRest] = path;
237+
238+
if (pathRest.length === 0) {
239+
obj[propertyName] = value;
240+
return;
241+
}
242+
243+
if (obj[propertyName] === undefined) {
244+
obj[propertyName] = {};
245+
}
246+
247+
req(obj[propertyName], pathRest);
248+
}
249+
250+
req(obj, colorDecision.themePath);
251+
});
252+
253+
return [
254+
`export function getColorDecisions(`,
255+
` params: {`,
256+
` colorOptions: ColorOptions;`,
257+
` }`,
258+
`) {`,
259+
``,
260+
` const { colorOptions } = params;`,
261+
``,
262+
` return {`,
263+
multiReplace({
264+
"input": JSON.stringify(obj, null, 2),
265+
keyValues
266+
})
267+
.replace(/^{\n/, "")
268+
.replace(/\n}$/, "")
269+
.split("\n")
270+
.map(line => line.replace(/^[ ]{2}/, ""))
271+
.map(line => ` ${line}`)
272+
.join("\n"),
273+
` } as const;`,
274+
`}`
275+
].join("\n");
276+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { generateGetColorDecisionsTsCode } from "../../../bin/css_to_ts/colorDecisions";
2+
import type { ColorDecision } from "../../../bin/css_to_ts/colorDecisions";
3+
import { assert } from "tsafe/assert";
4+
5+
console.log(`Running test ${__filename}`);
6+
7+
const input: ColorDecision[] = [
8+
{
9+
"themePath": ["background", "default", "normal", "grey", "hover"],
10+
"optionThemePath": ["grey", "_1000_50", "hover"]
11+
},
12+
{
13+
"themePath": ["background", "default", "normal", "grey", "default"],
14+
"optionThemePath": ["grey", "_1000_50", "default"]
15+
},
16+
{
17+
"themePath": ["border", "action", "low", "orangeTerreBattue", "default"],
18+
"optionThemePath": ["orangeTerreBattue", "_850_200", "default"]
19+
},
20+
{
21+
"themePath": ["background", "altRaised", "normal", "grey", "hover"],
22+
"optionThemePath": ["grey", "_975_100", "hover"]
23+
},
24+
{
25+
"themePath": ["background", "contrastOverlap", "normal", "grey", "default"],
26+
"optionThemePath": ["grey", "_950_150", "default"]
27+
}
28+
];
29+
30+
const expected = `
31+
export function getColorDecisions(
32+
params: {
33+
colorOptions: ColorOptions;
34+
}
35+
) {
36+
37+
const { colorOptions } = params;
38+
39+
return {
40+
"background": {
41+
"default": {
42+
"normal": {
43+
"grey": {
44+
"hover": colorOptions.grey._1000_50.hover,
45+
"default": colorOptions.grey._1000_50.default
46+
}
47+
}
48+
},
49+
"altRaised": {
50+
"normal": {
51+
"grey": {
52+
"hover": colorOptions.grey._975_100.hover
53+
}
54+
}
55+
},
56+
"contrastOverlap": {
57+
"normal": {
58+
"grey": {
59+
"default": colorOptions.grey._950_150.default
60+
}
61+
}
62+
}
63+
},
64+
"border": {
65+
"action": {
66+
"low": {
67+
"orangeTerreBattue": {
68+
"default": colorOptions.orangeTerreBattue._850_200.default
69+
}
70+
}
71+
}
72+
}
73+
} as const;
74+
}`.replace(/^\n/, "");
75+
76+
const got = generateGetColorDecisionsTsCode(input);
77+
78+
assert(got === expected);
79+
80+
console.log("PASS");
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
import "./parseColorDecisionName.test";
22
import "./parseColorDecision.test";
3+
import "./generateGetColorDecisionsTsCode.test";

0 commit comments

Comments
 (0)