11import { generateBreakpointsTsCode } from "./breakpoints" ;
22import { generateGetColorDecisionsTsCode } from "./colorDecisions" ;
3- import { generateGetColorOptionsTsCode } from "./colorOptions" ;
3+ import { generateGetColorOptionsHexTsCode , generateColorOptionsTsCode } from "./colorOptions" ;
44import { getProjectRoot } from "../../../src/bin/tools/getProjectRoot" ;
55import { generateTypographyTsCode } from "./typography" ;
66import { generateSpacingTsCode } from "./spacing" ;
@@ -26,17 +26,32 @@ export function cssToTs(params: {
2626 ) } , please don't edit.`
2727 ] . join ( "\n" ) ;
2828
29- const targetOptionFilePath = pathJoin ( generatedDirPath , "getColorOptions .ts" ) ;
29+ const targetGetColorOptionsHexFilePath = pathJoin ( generatedDirPath , "getColorOptionsHex .ts" ) ;
3030
3131 fs . writeFileSync (
32- targetOptionFilePath ,
32+ targetGetColorOptionsHexFilePath ,
33+ Buffer . from (
34+ [ warningMessage , `` , generateGetColorOptionsHexTsCode ( rawDsfrCssCode ) , `` ] . join ( "\n" ) ,
35+ "utf8"
36+ )
37+ ) ;
38+
39+ const targetColorOptionsFilePath = pathJoin ( generatedDirPath , "colorOptions.ts" ) ;
40+
41+ fs . writeFileSync (
42+ targetColorOptionsFilePath ,
3343 Buffer . from (
3444 [
3545 warningMessage ,
3646 `` ,
37- generateGetColorOptionsTsCode ( rawDsfrCssCode ) ,
47+ `import type { getColorOptionsHex } from "./${ pathBasename (
48+ targetGetColorOptionsHexFilePath
49+ ) . replace ( / \. t s $ / , "" ) } ";`,
3850 `` ,
39- `export type ColorOptions = ReturnType<typeof getColorOptions>;` ,
51+ generateColorOptionsTsCode ( rawDsfrCssCode ) ,
52+ `` ,
53+ `export type ColorOptions<Format extends "css var" | "hex"= "css var"> = ` ,
54+ ` Format extends "css var" ? typeof colorOptions : ReturnType<typeof getColorOptionsHex>;` ,
4055 ``
4156 ] . join ( "\n" ) ,
4257 "utf8"
@@ -48,14 +63,37 @@ export function cssToTs(params: {
4863 Buffer . from (
4964 [
5065 warningMessage ,
51- `import type { ColorOptions } from "./${ pathBasename ( targetOptionFilePath ) . replace (
52- / \. t s $ / ,
53- ""
54- ) } ";`,
66+ `import type { ColorOptions } from "./${ pathBasename (
67+ targetColorOptionsFilePath
68+ ) . replace ( / \. t s $ / , "" ) } ";`,
69+ `` ,
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+ `};` ,
85+ `` ,
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>>;` ,
5589 `` ,
56- generateGetColorDecisionsTsCode ( rawDsfrCssCode ) ,
90+ `export function getColorDecisions<Format extends "css var" | "hex">(params: {` ,
91+ ` colorOptions: ColorOptions<Format>;` ,
5792 `` ,
58- `export type ColorDecisions = ReturnType<typeof getColorDecisions>;` ,
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+ `}` ,
5997 ``
6098 ] . join ( "\n" ) ,
6199 "utf8"
0 commit comments