Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | 2x 22x 22x 42x 42x 4x 42x 50x 20x 42x 22x 22x 22x 22x 22x 1x 1x 1x 21x 21x 21x 5x 21x 5x 21x 4x 4x 4x 4x 4x 4x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 22x 22x 22x 22x 22x 70x 70x 70x 70x 70x 63x 63x 7x 18x 6x 1x 22x | /**
* Platform file loading, path resolution, availability checking, and token categorization.
* Responsible for reading build output files and classifying tokens as matching,
* mismatched, or missing across platforms.
*/
import fs from 'node:fs';
import path from 'node:path';
import { parseColorsFromCSS } from './css-parser.js';
import { parseColorsFromKotlin } from './android-parser.js';
import { parseColorsFromSwift } from './ios-parser.js';
const THEME = 'cosmos';
/**
* Resolve all platform file paths relative to process.cwd() at call time.
* Using cwd() lazily (not at import time) allows tests to change directory
* without needing to re-import the module.
* @returns {{ CSS_PATH, CSS_REFERENCE_PATH, RAW_JSON_PATH, ANDROID_PATH, IOS_PATH }}
*/
export function resolvePaths() {
const cwd = process.cwd();
return {
CSS_PATH: path.resolve(cwd, `dist/themes/${THEME}/${THEME}.global.tokens.css`),
CSS_REFERENCE_PATH: path.resolve(cwd, `dist/themes/${THEME}/${THEME}.reference.tokens.css`),
RAW_JSON_PATH: path.resolve(cwd, `dist/themes/${THEME}/${THEME}.global.tokens.raw.json`),
ANDROID_PATH: path.resolve(cwd, `dist/themes/${THEME}/android/SalesforceCosmosTokensAll.kt`),
IOS_PATH: path.resolve(cwd, `dist/themes/${THEME}/ios/SalesforceCosmosTokensAll.swift`),
};
}
/**
* Recursively traverse a raw token JSON object and collect all deprecated color token paths.
* @param {object} obj
* @param {string} currentPath
* @param {Set<string>} deprecated - accumulated set of normalized path strings
* @returns {Set<string>}
*/
export function findDeprecatedColorTokens(obj, currentPath = '', deprecated = new Set()) {
Iif (typeof obj !== 'object' || obj === null) return deprecated;
if (obj['$deprecated'] === true && currentPath.includes('.color.')) {
deprecated.add(currentPath.replace(/^slds\.g\./, '').replaceAll('.', '-'));
}
for (const [key, value] of Object.entries(obj)) {
if (!key.startsWith('$')) {
findDeprecatedColorTokens(value, currentPath ? `${currentPath}.${key}` : key, deprecated);
}
}
return deprecated;
}
/**
* Check whether the required platform output files exist and whether there are
* enough platforms to perform a comparison. Exits the process if CSS is missing
* or fewer than 2 platforms are available.
* @param {{ CSS_PATH, ANDROID_PATH, IOS_PATH }} paths
* @returns {{ availablePlatforms: string[], missingPlatforms: {name: string, path: string}[], cssExists: boolean, androidExists: boolean, iosExists: boolean } | null}
*/
export function checkPlatformAvailability(paths) {
const { CSS_PATH, ANDROID_PATH, IOS_PATH } = paths;
const cssExists = fs.existsSync(CSS_PATH);
const androidExists = fs.existsSync(ANDROID_PATH);
const iosExists = fs.existsSync(IOS_PATH);
if (!cssExists) {
console.error(`❌ CSS file not found: ${CSS_PATH}`);
process.exit(1);
return null;
}
const availablePlatforms = ['CSS'];
const missingPlatforms = [];
if (androidExists) availablePlatforms.push('Android');
else missingPlatforms.push({ name: 'Android', path: ANDROID_PATH });
if (iosExists) availablePlatforms.push('iOS');
else missingPlatforms.push({ name: 'iOS', path: IOS_PATH });
if (availablePlatforms.length < 2) {
console.error(`❌ Need at least 2 platforms to compare. Only found: ${availablePlatforms.join(', ')}`);
console.log(`\n CSS: ${cssExists ? '✓' : '✗'}`);
console.log(` Android: ${androidExists ? '✓' : '✗'}`);
console.log(` iOS: ${iosExists ? '✓' : '✗'}\n`);
process.exit(1);
return null;
}
return { availablePlatforms, missingPlatforms, cssExists, androidExists, iosExists };
}
/**
* Load and parse color tokens from all available platform output files.
* @param {{ CSS_PATH, CSS_REFERENCE_PATH, RAW_JSON_PATH, ANDROID_PATH, IOS_PATH }} paths
* @param {{ androidExists: boolean, iosExists: boolean }} platformAvailability
* @returns {{ cssColors: Map, androidColors: Map, iosColors: Map, rawCssCount: number, deprecatedCount: number }}
*/
export function loadPlatformColors(paths, platformAvailability) {
const { CSS_PATH, CSS_REFERENCE_PATH, RAW_JSON_PATH, ANDROID_PATH, IOS_PATH } = paths;
const { androidExists, iosExists } = platformAvailability;
let cssContent = fs.readFileSync(CSS_PATH, 'utf8');
Eif (fs.existsSync(CSS_REFERENCE_PATH)) {
cssContent += '\n' + fs.readFileSync(CSS_REFERENCE_PATH, 'utf8');
}
const cssColors = parseColorsFromCSS(cssContent);
const rawCssCount = cssColors.size;
let deprecatedCount = 0;
Eif (fs.existsSync(RAW_JSON_PATH)) {
const rawJson = JSON.parse(fs.readFileSync(RAW_JSON_PATH, 'utf8'));
// Count deprecated tokens for reporting; mobile now includes them with annotations
deprecatedCount = findDeprecatedColorTokens(rawJson).size * 2; // ×2 for -light and -dark
}
const androidColors = androidExists
? parseColorsFromKotlin(fs.readFileSync(ANDROID_PATH, 'utf8'))
: new Map();
const iosColors = iosExists ? parseColorsFromSwift(fs.readFileSync(IOS_PATH, 'utf8')) : new Map();
return { cssColors, androidColors, iosColors, rawCssCount, deprecatedCount };
}
/**
* Classify the union of all token names into three buckets:
* - matching: all present platforms have the same hex value
* - mismatched: all present platforms have different hex values
* - missing: token is absent from one or more available platforms
* @param {Map} cssColors
* @param {Map} androidColors
* @param {Map} iosColors
* @param {string[]} availablePlatforms
* @returns {{ allTokens: Set, matching: string[], mismatched: object[], missing: object[] }}
*/
export function categorizeTokens(cssColors, androidColors, iosColors, availablePlatforms) {
const allTokens = new Set([...cssColors.keys(), ...androidColors.keys(), ...iosColors.keys()]);
const matching = [];
const mismatched = [];
const missing = [];
for (const token of allTokens) {
const cssHex = cssColors.get(token);
const androidHex = androidColors.get(token);
const iosHex = iosColors.get(token);
const platforms = [cssHex && 'CSS', androidHex && 'Android', iosHex && 'iOS'].filter(Boolean);
if (platforms.length < availablePlatforms.length) {
missing.push({ token, platforms, cssHex, androidHex, iosHex });
continue;
}
const hexValues = [cssHex, androidHex, iosHex].filter(Boolean);
if (hexValues.every((hex) => hex === hexValues[0])) {
matching.push(token);
} else {
mismatched.push({ token, cssHex, androidHex, iosHex });
}
}
return { allTokens, matching, mismatched, missing };
}
|