diff --git a/.changeset/thick-mayflies-thank.md b/.changeset/thick-mayflies-thank.md new file mode 100644 index 00000000..6eb32c42 --- /dev/null +++ b/.changeset/thick-mayflies-thank.md @@ -0,0 +1,7 @@ +--- +"@terrazzo/cli": patch +"@terrazzo/parser": patch +"@terrazzo/token-tools": patch +--- + +Fix bug in normalize where modes were skipped over diff --git a/package.json b/package.json index d211cf0e..9a404c55 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,6 @@ "scripts": { "build": "turbo run build", "build:apps": "turbo run build:app", - "build:examples": "pnpm run -r --parallel --if-present --stream build:tokens", "changeset": "changeset", "inject-license": "node scripts/inject-license.js", "dev": "pnpm run -r --parallel --if-present dev", diff --git a/packages/cli/src/normalize.ts b/packages/cli/src/normalize.ts index d3fd51e8..df33682c 100644 --- a/packages/cli/src/normalize.ts +++ b/packages/cli/src/normalize.ts @@ -1,7 +1,8 @@ import fs from 'node:fs'; import path from 'node:path'; -import { type MemberNode, type ObjectNode, parse as parseJSON, print } from '@humanwhocodes/momoa'; -import { type Logger, defineConfig, parse, traverse } from '@terrazzo/parser'; +import { type MemberNode, type ObjectNode, type StringNode, parse as parseJSON, print } from '@humanwhocodes/momoa'; +import { type Logger, defineConfig, getObjMembers, parse, traverse } from '@terrazzo/parser'; +import { isAlias } from '@terrazzo/token-tools'; import { cwd, printError } from './shared.js'; export interface NormalizeOptions { @@ -9,8 +10,10 @@ export interface NormalizeOptions { output: URL; } -function find$value(member: MemberNode) { - return member.name.type === 'String' && member.name.value === '$value'; +function findMember(name: string) { + return function (member: MemberNode) { + return member.name.type === 'String' && member.name.value === name; + }; } export async function normalizeCmd(filename: string, { logger, output }: NormalizeOptions) { @@ -33,19 +36,38 @@ export async function normalizeCmd(filename: string, { logger, output }: Normali traverse(document, { enter(node, _parent, nodePath) { const token = tokens[nodePath.join('.')]; + if (!token || token.aliasOf || node.type !== 'Member' || node.value.type !== 'Object') { return; } - const $valueI = node.value.members.findIndex(find$value); - const newValueContainer = parseJSON(JSON.stringify({ $value: token.$value })).body as ObjectNode; - const newValueNode = newValueContainer.members.find(find$value)!; + const $valueI = node.value.members.findIndex(findMember('$value')); switch (token.$type) { case 'color': case 'dimension': case 'duration': { if (node.value.members[$valueI]!.value.type === 'String') { + const newValueContainer = parseJSON(JSON.stringify({ $value: token.$value })).body as ObjectNode; + const newValueNode = newValueContainer.members.find(findMember('$value'))!; node.value.members[$valueI] = newValueNode; + + const { $extensions } = getObjMembers(node.value); + if ($extensions?.type === 'Object') { + const { mode } = getObjMembers($extensions); + if (mode?.type === 'Object') { + for (let i = 0; i < mode.members.length; i++) { + const modeName = (mode.members[i]!.name as StringNode).value; + if (isAlias(token.mode[modeName]!)) { + continue; + } + const newModeValueContainer = parseJSON( + JSON.stringify({ [modeName]: token.mode[modeName]!.$value }), + ).body as ObjectNode; + const newModeValueNode = newModeValueContainer.members.find(findMember(modeName))!; + mode.members[i] = newModeValueNode; + } + } + } } break; } diff --git a/packages/cli/test/fixtures/normalize/input.json b/packages/cli/test/fixtures/normalize/input.json index 6a648dd0..1efe9fba 100644 --- a/packages/cli/test/fixtures/normalize/input.json +++ b/packages/cli/test/fixtures/normalize/input.json @@ -109,6 +109,60 @@ "900": { "$value": "#522504" }, "1000": { "$value": "#401b01" } }, + "celery": { + "400": { + "$value": "rgb(39, 187, 54)", + "$extensions": { + "mode": { + "dark": "rgb(34, 184, 51)", + "darkest": "rgb(13, 171, 37)", + "light": "rgb(39, 187, 54)", + "lightest": "rgb(48, 193, 61)", + "middark": "rgb(45, 190, 58)", + "midlight": "rgb(7, 167, 33)" + } + } + }, + "500": { + "$value": "rgb(7, 167, 33)", + "$extensions": { + "mode": { + "dark": "rgb(68, 202, 73)", + "darkest": "rgb(45, 191, 58)", + "light": "rgb(7, 167, 33)", + "lightest": "rgb(15, 172, 38)", + "middark": "rgb(78, 207, 80)", + "midlight": "rgb(0, 148, 19)" + } + } + }, + "600": { + "$value": "rgb(0, 145, 18)", + "$extensions": { + "mode": { + "dark": "rgb(105, 220, 99)", + "darkest": "rgb(80, 208, 82)", + "light": "rgb(0, 145, 18)", + "lightest": "rgb(0, 150, 20)", + "middark": "rgb(112, 223, 104)", + "midlight": "rgb(0, 127, 15)" + } + } + }, + "700": { + "$value": "rgb(0, 124, 15)", + "$extensions": { + "mode": { + "dark": "rgb(142, 235, 127)", + "darkest": "rgb(115, 224, 107)", + "light": "rgb(0, 124, 15)", + "lightest": "rgb(0, 128, 15)", + "middark": "rgb(146, 237, 130)", + "midlight": "rgb(0, 107, 15)" + } + } + } + }, "background": { "brand": { "default": { "$value": "{color.brand.800}" }, diff --git a/packages/cli/test/fixtures/normalize/want.json b/packages/cli/test/fixtures/normalize/want.json index 286b5887..6d749e3a 100644 --- a/packages/cli/test/fixtures/normalize/want.json +++ b/packages/cli/test/fixtures/normalize/want.json @@ -1009,6 +1009,284 @@ } } }, + "celery": { + "400": { + "$value": { + "colorSpace": "srgb", + "channels": [ + 0.15294117647058825, + 0.7333333333333333, + 0.21176470588235294 + ], + "alpha": 1 + }, + "$extensions": { + "mode": { + "dark": { + "colorSpace": "srgb", + "channels": [ + 0.13333333333333333, + 0.7215686274509804, + 0.2 + ], + "alpha": 1 + }, + "darkest": { + "colorSpace": "srgb", + "channels": [ + 0.050980392156862744, + 0.6705882352941176, + 0.1450980392156863 + ], + "alpha": 1 + }, + "light": { + "colorSpace": "srgb", + "channels": [ + 0.15294117647058825, + 0.7333333333333333, + 0.21176470588235294 + ], + "alpha": 1 + }, + "lightest": { + "colorSpace": "srgb", + "channels": [ + 0.18823529411764706, + 0.7568627450980392, + 0.23921568627450981 + ], + "alpha": 1 + }, + "middark": { + "colorSpace": "srgb", + "channels": [ + 0.17647058823529413, + 0.7450980392156863, + 0.22745098039215686 + ], + "alpha": 1 + }, + "midlight": { + "colorSpace": "srgb", + "channels": [ + 0.027450980392156862, + 0.6549019607843137, + 0.12941176470588237 + ], + "alpha": 1 + } + } + } + }, + "500": { + "$value": { + "colorSpace": "srgb", + "channels": [ + 0.027450980392156862, + 0.6549019607843137, + 0.12941176470588237 + ], + "alpha": 1 + }, + "$extensions": { + "mode": { + "dark": { + "colorSpace": "srgb", + "channels": [ + 0.26666666666666666, + 0.792156862745098, + 0.28627450980392155 + ], + "alpha": 1 + }, + "darkest": { + "colorSpace": "srgb", + "channels": [ + 0.17647058823529413, + 0.7490196078431373, + 0.22745098039215686 + ], + "alpha": 1 + }, + "light": { + "colorSpace": "srgb", + "channels": [ + 0.027450980392156862, + 0.6549019607843137, + 0.12941176470588237 + ], + "alpha": 1 + }, + "lightest": { + "colorSpace": "srgb", + "channels": [ + 0.058823529411764705, + 0.6745098039215687, + 0.14901960784313725 + ], + "alpha": 1 + }, + "middark": { + "colorSpace": "srgb", + "channels": [ + 0.3058823529411765, + 0.8117647058823529, + 0.3137254901960784 + ], + "alpha": 1 + }, + "midlight": { + "colorSpace": "srgb", + "channels": [ + 0, + 0.5803921568627451, + 0.07450980392156863 + ], + "alpha": 1 + } + } + } + }, + "600": { + "$value": { + "colorSpace": "srgb", + "channels": [ + 0, + 0.5686274509803921, + 0.07058823529411765 + ], + "alpha": 1 + }, + "$extensions": { + "mode": { + "dark": { + "colorSpace": "srgb", + "channels": [ + 0.4117647058823529, + 0.8627450980392157, + 0.38823529411764707 + ], + "alpha": 1 + }, + "darkest": { + "colorSpace": "srgb", + "channels": [ + 0.3137254901960784, + 0.8156862745098039, + 0.3215686274509804 + ], + "alpha": 1 + }, + "light": { + "colorSpace": "srgb", + "channels": [ + 0, + 0.5686274509803921, + 0.07058823529411765 + ], + "alpha": 1 + }, + "lightest": { + "colorSpace": "srgb", + "channels": [ + 0, + 0.5882352941176471, + 0.0784313725490196 + ], + "alpha": 1 + }, + "middark": { + "colorSpace": "srgb", + "channels": [ + 0.4392156862745098, + 0.8745098039215686, + 0.40784313725490196 + ], + "alpha": 1 + }, + "midlight": { + "colorSpace": "srgb", + "channels": [ + 0, + 0.4980392156862745, + 0.058823529411764705 + ], + "alpha": 1 + } + } + } + }, + "700": { + "$value": { + "colorSpace": "srgb", + "channels": [ + 0, + 0.48627450980392156, + 0.058823529411764705 + ], + "alpha": 1 + }, + "$extensions": { + "mode": { + "dark": { + "colorSpace": "srgb", + "channels": [ + 0.5568627450980392, + 0.9215686274509803, + 0.4980392156862745 + ], + "alpha": 1 + }, + "darkest": { + "colorSpace": "srgb", + "channels": [ + 0.45098039215686275, + 0.8784313725490196, + 0.4196078431372549 + ], + "alpha": 1 + }, + "light": { + "colorSpace": "srgb", + "channels": [ + 0, + 0.48627450980392156, + 0.058823529411764705 + ], + "alpha": 1 + }, + "lightest": { + "colorSpace": "srgb", + "channels": [ + 0, + 0.5019607843137255, + 0.058823529411764705 + ], + "alpha": 1 + }, + "middark": { + "colorSpace": "srgb", + "channels": [ + 0.5725490196078431, + 0.9294117647058824, + 0.5098039215686274 + ], + "alpha": 1 + }, + "midlight": { + "colorSpace": "srgb", + "channels": [ + 0, + 0.4196078431372549, + 0.058823529411764705 + ], + "alpha": 1 + } + } + } + } + }, "background": { "brand": { "default": { diff --git a/packages/cli/test/normalize.test.ts b/packages/cli/test/normalize.test.ts index 91fac085..71e89f7a 100644 --- a/packages/cli/test/normalize.test.ts +++ b/packages/cli/test/normalize.test.ts @@ -9,7 +9,7 @@ describe('tz normalize', () => { test('basic', async () => { const cwd = new URL('./fixtures/normalize/', import.meta.url); await execa(cmd, ['normalize', 'input.json', '-o', 'actual.json'], { cwd }); - expect(fs.readFileSync(new URL('./actual.json', cwd), 'utf8')).toMatchFileSnapshot( + await expect(fs.readFileSync(new URL('./actual.json', cwd), 'utf8')).toMatchFileSnapshot( fileURLToPath(new URL('./want.json', cwd)), ); }); diff --git a/packages/storybook/package.json b/packages/storybook/package.json index 71f27209..e472287a 100644 --- a/packages/storybook/package.json +++ b/packages/storybook/package.json @@ -8,7 +8,7 @@ "dev": "pnpm run sb", "build:app": "storybook build -o storybook", "format": "biome check --fix --unsafe src", - "lint": "pnpm run --filter @terrazzo/storybook \"/^lint:.*/\"", + "lint": "pnpm run lint:js && pnpm run lint:css", "lint:js": "biome check src", "lint:css": "stylelint \"src/**/*.css\"", "sb": "storybook dev -p 9009"