🐛(tokens) fix scss generator

scss maps expect keys between quotes like:
$map = (
  'key': value,
)
This commit is contained in:
Romain Le Cellier
2023-04-27 14:38:00 +02:00
parent 1b9fb93bda
commit b9d2f2955f
3 changed files with 27 additions and 10 deletions

View File

@@ -0,0 +1,5 @@
---
"@openfun/cunningham-tokens": minor
---
Fix SassGenerator

View File

@@ -26,26 +26,26 @@ describe("SassGenerator", () => {
expect(fs.existsSync(sassFile)).toEqual(true); expect(fs.existsSync(sassFile)).toEqual(true);
expect(fs.readFileSync(sassFile).toString()).toMatchInlineSnapshot(` expect(fs.readFileSync(sassFile).toString()).toMatchInlineSnapshot(`
"$colors: ( "$colors: (
primary: #055FD2, 'primary': #055FD2,
secondary: #DA0000, 'secondary': #DA0000,
ternary: ( 'ternary': (
900: #022858 '900': #022858
) )
); );
$fontFamilies: ( $fontFamilies: (
base: Roboto 'base': Roboto
); );
$fontSizes: ( $fontSizes: (
m: 1rem 'm': 1rem
); );
$fontWeights: ( $fontWeights: (
medium: 400 'medium': 400
); );
$spacings: ( $spacings: (
s: 1rem 's': 1rem
); );
$transitions: ( $transitions: (
ease: linear 'ease': linear
); );
" "
`); `);

View File

@@ -55,7 +55,19 @@ const generateTransitionsSassMap = (tokens: Tokens) => {
}; };
function JSONToSassMap(json: Object) { function JSONToSassMap(json: Object) {
return JSON.stringify(json, null, 2) function deepQuoteObjectKeys(object: Object) {
return Object.entries(object).reduce(
(acc, [key, value]): Record<string, any> => ({
...acc,
[`'${key}'`]:
typeof value === "object" ? deepQuoteObjectKeys(value) : value,
}),
{}
);
}
const jsonWithQuotedKeys = deepQuoteObjectKeys(json);
return JSON.stringify(jsonWithQuotedKeys, null, 2)
.replace(/{/g, "(") .replace(/{/g, "(")
.replace(/}/g, ")") .replace(/}/g, ")")
.replace(/"/g, ""); .replace(/"/g, "");