Skip to content

Commit df70718

Browse files
committed
Color decisions map to the actual decision variable
1 parent eb96e3b commit df70718

File tree

7 files changed

+85
-43
lines changed

7 files changed

+85
-43
lines changed

scripts/build/cssToTs/colorDecisionAndCorrespondingOptions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { parseColorOptions } from "./colorOptions";
2-
import { parseColorDecision } from "./colorDecisions";
2+
import { parseColorDecisions } from "./colorDecisions";
33
import { exclude } from "tsafe/exclude";
44
import type { ColorDecision } from "./colorDecisions";
55
import type { ColorOption } from "./colorOptions";
@@ -12,7 +12,7 @@ export type ColorDecisionAndCorrespondingOption = Omit<ColorDecision, "optionThe
1212
export function generateColorDecisionAndCorrespondingOptionsTsCode(rawCssCode: string): string {
1313
const colorOptions = parseColorOptions(rawCssCode);
1414

15-
const colorDecisionAndCorrespondingOptions = parseColorDecision(rawCssCode)
15+
const colorDecisionAndCorrespondingOptions = parseColorDecisions(rawCssCode)
1616
.map(colorDecision => {
1717
const colorOption = colorOptions.find(
1818
colorOption =>

scripts/build/cssToTs/colorDecisions.ts

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export type ColorDecision = {
166166
optionThemePath: readonly string[];
167167
};
168168

169-
export const parseColorDecision = memoize((rawCssCode: string): ColorDecision[] => {
169+
export const parseColorDecisions = memoize((rawCssCode: string): ColorDecision[] => {
170170
const { parseColorDecisionName } = createParseColorDecisionName(rawCssCode);
171171

172172
const parsedCss = parseCss(rawCssCode);
@@ -217,12 +217,12 @@ export const parseColorDecision = memoize((rawCssCode: string): ColorDecision[]
217217
.filter(exclude(undefined));
218218
});
219219

220-
export function generateGetColorDecisionsTsCode(rawCssCode: string): string {
220+
export function generateGetColorDecisionsHexTsCode(rawCssCode: string): string {
221221
const obj: any = {};
222222

223223
const keyValues: Record<string, string> = {};
224224

225-
parseColorDecision(rawCssCode).forEach(colorDecision => {
225+
parseColorDecisions(rawCssCode).forEach(colorDecision => {
226226
const value = (() => {
227227
const hash = crypto
228228
.createHash("sha256")
@@ -253,9 +253,9 @@ export function generateGetColorDecisionsTsCode(rawCssCode: string): string {
253253
});
254254

255255
return [
256-
`export function getColorDecisions<Format extends "css var" | "hex">(`,
256+
`export function getColorDecisionsHex(`,
257257
` params: {`,
258-
` colorOptions: ColorOptions<Format>;`,
258+
` colorOptions: ColorOptions<"hex">;`,
259259
` }`,
260260
`) {`,
261261
``,
@@ -276,3 +276,43 @@ export function generateGetColorDecisionsTsCode(rawCssCode: string): string {
276276
`}`
277277
].join("\n");
278278
}
279+
280+
export function generateColorDecisionsTsCode(rawCssCode: string) {
281+
const colorDecisions = parseColorDecisions(rawCssCode);
282+
283+
const obj: any = {};
284+
285+
colorDecisions.forEach(colorDecision => {
286+
const value = `var(${colorDecision.colorDecisionName})`;
287+
288+
function req(obj: any, path: readonly string[]): void {
289+
const [propertyName, ...pathRest] = path;
290+
291+
if (pathRest.length === 0) {
292+
obj[propertyName] = value;
293+
return;
294+
}
295+
296+
if (obj[propertyName] === undefined) {
297+
obj[propertyName] = {};
298+
}
299+
300+
req(obj[propertyName], pathRest);
301+
}
302+
303+
req(obj, colorDecision.themePath);
304+
});
305+
306+
return [
307+
``,
308+
`export const colorDecisions= {`,
309+
JSON.stringify(obj, null, 2)
310+
.replace(/^{\n/, "")
311+
.replace(/\n}$/, "")
312+
.split("\n")
313+
.map(line => line.replace(/^[ ]{2}/, ""))
314+
.map(line => ` ${line}`)
315+
.join("\n"),
316+
`} as const;`
317+
].join("\n");
318+
}

scripts/build/cssToTs/cssToTs.ts

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { generateBreakpointsTsCode } from "./breakpoints";
2-
import { generateGetColorDecisionsTsCode } from "./colorDecisions";
2+
import { generateGetColorDecisionsHexTsCode, generateColorDecisionsTsCode } from "./colorDecisions";
33
import { generateGetColorOptionsHexTsCode, generateColorOptionsTsCode } from "./colorOptions";
44
import { getProjectRoot } from "../../../src/bin/tools/getProjectRoot";
55
import { generateTypographyTsCode } from "./typography";
@@ -58,42 +58,43 @@ export function cssToTs(params: {
5858
)
5959
);
6060

61+
const targetGetColorDecisionsHexFilePath = pathJoin(
62+
generatedDirPath,
63+
"getColorDecisionsHex.ts"
64+
);
65+
6166
fs.writeFileSync(
62-
pathJoin(generatedDirPath, "getColorDecisions.ts"),
67+
targetGetColorDecisionsHexFilePath,
6368
Buffer.from(
6469
[
6570
warningMessage,
6671
`import type { ColorOptions } from "./${pathBasename(
6772
targetColorOptionsFilePath
6873
).replace(/\.ts$/, "")}";`,
6974
``,
70-
generateGetColorDecisionsTsCode(rawDsfrCssCode).replace(
71-
"export function getColorDecisions",
72-
"function getColorDecisions_noReturnType"
73-
),
74-
``,
75-
`type IsHex<T> = T extends \`#\${string}\` ? T : never;`,
76-
``,
77-
`type OnlyHex<T> = {`,
78-
` [K in keyof T]: T[K] extends string ? IsHex<T[K]> : OnlyHex<T[K]>`,
79-
`};`,
80-
``,
81-
`type IsCssVar<T> = T extends \`var(--\${string})\` ? T : never;`,
82-
`type OnlyCssVar<T> = {`,
83-
` [K in keyof T]: T[K] extends string ? IsCssVar<T[K]> : OnlyCssVar<T[K]>`,
84-
`};`,
75+
generateGetColorDecisionsHexTsCode(rawDsfrCssCode),
76+
``
77+
].join("\n"),
78+
"utf8"
79+
)
80+
);
81+
82+
const targetColorDecisionsFilePath = pathJoin(generatedDirPath, "colorDecisions.ts");
83+
84+
fs.writeFileSync(
85+
targetColorDecisionsFilePath,
86+
Buffer.from(
87+
[
88+
warningMessage,
8589
``,
86-
`export type ColorDecisions<Format extends "css var" | "hex" = "css var"> =`,
87-
` Format extends "css var" ? OnlyCssVar<ReturnType<typeof getColorDecisions_noReturnType>> :`,
88-
` OnlyHex<ReturnType<typeof getColorDecisions_noReturnType>>;`,
90+
`import type { getColorDecisionsHex } from "./${pathBasename(
91+
targetGetColorDecisionsHexFilePath
92+
).replace(/\.ts$/, "")}";`,
8993
``,
90-
`export function getColorDecisions<Format extends "css var" | "hex">(params: {`,
91-
` colorOptions: ColorOptions<Format>;`,
94+
generateColorDecisionsTsCode(rawDsfrCssCode),
9295
``,
93-
`}): ColorDecisions<Format> {`,
94-
` // ${"@"}ts-expect-error: We are intentionally sacrificing internal type safety for a more accurate type annotation.`,
95-
` return getColorDecisions_noReturnType(params);`,
96-
`}`,
96+
`export type ColorDecisions<Format extends "css var" | "hex"= "css var"> = `,
97+
` Format extends "css var" ? typeof colorDecisions : ReturnType<typeof getColorDecisionsHex>;`,
9798
``
9899
].join("\n"),
99100
"utf8"

src/fr/colors.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { colorOptions, type ColorOptions } from "./generatedFromCss/colorOptions";
22
import { getColorOptionsHex } from "./generatedFromCss/getColorOptionsHex";
3-
import { getColorDecisions, type ColorDecisions } from "./generatedFromCss/getColorDecisions";
3+
import { colorDecisions, type ColorDecisions } from "./generatedFromCss/colorDecisions";
4+
import { getColorDecisionsHex } from "./generatedFromCss/getColorDecisionsHex";
45

56
export type Colors = {
67
options: ColorOptions<"css var">;
@@ -13,12 +14,12 @@ export type Colors = {
1314

1415
export const colors: Colors = {
1516
"options": colorOptions,
16-
"decisions": getColorDecisions({ colorOptions }),
17+
"decisions": colorDecisions,
1718
"getHex": (() => {
1819
const getHex: Colors["getHex"] = ({ isDark }) => {
1920
const options = getColorOptionsHex({ isDark });
2021

21-
const decisions = getColorDecisions({ colorOptions });
22+
const decisions = getColorDecisionsHex({ "colorOptions": options });
2223

2324
return {
2425
options,

src/fr/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { cx } from "./cx";
77
export type { FrCxArg } from "./cx";
88
export type { Colors } from "./colors";
99
export type { ColorOptions } from "./generatedFromCss/colorOptions";
10-
export type { ColorDecisions } from "./generatedFromCss/getColorDecisions";
10+
export type { ColorDecisions } from "./generatedFromCss/colorDecisions";
1111
import { colors } from "./colors";
1212
export type { FrClassName, FrIconClassName, RiIconClassName } from "./generatedFromCss/classNames";
1313
import { typography } from "./generatedFromCss/typography";

test/runtime/scripts/colorDecisions/generateGetColorDecisionsTsCode.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { it, expect } from "vitest";
2-
import { generateGetColorDecisionsTsCode } from "../../../../scripts/build/cssToTs/colorDecisions";
2+
import { generateGetColorDecisionsHexTsCode } from "../../../../scripts/build/cssToTs/colorDecisions";
33

44
it("Generates the correct TS code for breakpoints", () => {
55
const input = `
@@ -35,9 +35,9 @@ it("Generates the correct TS code for breakpoints", () => {
3535
`;
3636

3737
const expected = `
38-
export function getColorDecisions<Format extends "css var" | "hex">(
38+
export function getColorDecisionsHex(
3939
params: {
40-
colorOptions: ColorOptions<Format>;
40+
colorOptions: ColorOptions<"hex">;
4141
}
4242
) {
4343
@@ -72,7 +72,7 @@ export function getColorDecisions<Format extends "css var" | "hex">(
7272
} as const;
7373
}`.replace(/^\n/, "");
7474

75-
const got = generateGetColorDecisionsTsCode(input);
75+
const got = generateGetColorDecisionsHexTsCode(input);
7676

7777
expect(got).toBe(expected);
7878
});

test/runtime/scripts/colorDecisions/parseColorDecision.test.ts renamed to test/runtime/scripts/colorDecisions/parseColorDecisions.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { it, expect } from "vitest";
2-
import { parseColorDecision } from "../../../../scripts/build/cssToTs/colorDecisions";
2+
import { parseColorDecisions } from "../../../../scripts/build/cssToTs/colorDecisions";
33
import type { ColorDecision } from "../../../../scripts/build/cssToTs/colorDecisions";
44

55
it("Color decisions to be successfully parsed", () => {
@@ -35,7 +35,7 @@ it("Color decisions to be successfully parsed", () => {
3535
@media (min-width: 78em) { }
3636
`;
3737

38-
const got = parseColorDecision(rawCssCode);
38+
const got = parseColorDecisions(rawCssCode);
3939

4040
const expected: ColorDecision[] = [
4141
{

0 commit comments

Comments
 (0)