Skip to content

Commit 2ecdfb2

Browse files
committed
improved logging messages for validation warnings/errors of $modes
1 parent 85603bd commit 2ecdfb2

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

packages/tokens/scripts/build.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,33 @@ const distFolder = path.resolve(__dirname, '../dist');
3737
for (const mode of modes) {
3838
StyleDictionary.registerPreprocessor({
3939
name: `replace-value-for-mode-${mode}`,
40-
preprocessor: (dictionary, _options) => {
40+
preprocessor: (dictionary, options) => {
41+
// we get the `buildPath` from the `PlatformConfig` option
42+
const buildPath = (options as any).buildPath;
4143
// recursively traverse token objects and replace the `$value` with the corresponding colocated `$modes` theme value
4244
// note: the `slice` is always an object (a token or a parent group)
43-
function replaceModes(slice: DesignToken) {
45+
function replaceModes(slice: DesignToken, tokenPath: string[]) {
4446
if (slice.$modes) {
4547
if (mode in slice.$modes) {
4648
// extra validation to catch instances where the `default` mode value is different from the `$value`
4749
if (mode === 'default' && slice.$modes[mode] !== slice.$value) {
48-
console.warn(`⚠️ ${chalk.yellow.bold('WARNING')} - Found themed 'default' token with value different than '$value' (\`${slice.$modes[mode]}\` instead of the expected \`${slice.$value}\`) - File: ${slice.filePath}`);
50+
console.warn(`⚠️ ${chalk.yellow.bold('WARNING')} - Found themed 'default' token '{${path.join('.')}}' with value different than '$value' (\`${slice.$modes[mode]}\` instead of the expected \`${slice.$value}\`) - BuildPath: ${buildPath} - File: ${slice.filePath}`);
4951
}
5052
slice.$value = slice.$modes[mode];
5153
} else {
5254
// we want to interrupt the execution of the script if one of the expected modes is missing
53-
throw new Error(`❌ ${chalk.red.bold('ERROR')} - Found themed token without '${mode}' value - ${JSON.stringify(slice, null, 2)}`);
55+
throw new Error(`❌ ${chalk.red.bold('ERROR')} - Found themed token '{${path.join('.')}}' without '${mode}' value - BuildPath: ${buildPath} - File: ${slice.filePath} - Path: ${path.join('.')} - ${JSON.stringify(slice, null, 2)}`);
5456
}
5557
} else {
56-
Object.values(slice).forEach((value) => {
58+
Object.entries(slice).forEach(([key, value]) => {
5759
if (typeof value === 'object') {
58-
replaceModes(value);
60+
replaceModes(value, [...tokenPath, key]);
5961
}
6062
});
6163
}
6264
return slice;
6365
}
64-
return replaceModes(dictionary);
66+
return replaceModes(dictionary, []);
6567
},
6668
});
6769
}

0 commit comments

Comments
 (0)