All files / packages/design-tokens/src/validators/cross-platform-colors index.js

96.15% Statements 25/26
91.66% Branches 11/12
100% Functions 2/2
95.65% Lines 22/23

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                                                                                        22x 22x   22x 22x   17x   17x 2x 2x 2x   2x     17x         17x             17x 17x   17x 17x   17x 17x   17x   17x         1x 1x      
#!/usr/bin/env node
 
/**
 * Cross-Platform Color Consistency Validator (Cosmos Theme)
 *
 * Validates that color tokens produce identical values across CSS, Android (Kotlin),
 * and iOS (Swift) outputs by comparing normalized hex color values.
 *
 * Note: This validator currently only supports the Cosmos theme. Android and iOS
 * do not have SLDS theme outputs at this time.
 *
 * Module layout:
 *   color-utils.js        — normalizeHex, toHex, parseLightDarkFunction, MODE
 *   css-parser.js         — parseColorsFromCSS
 *   android-parser.js     — parseColorsFromKotlin, normalizeAndroidTokenName
 *   ios-parser.js         — parseColorsFromSwift, normalizeSwiftTokenName
 *   mobile-parser-utils.js — classNameToMode, resolveReferences, storeDirectToken, …
 *   platform-loader.js    — resolvePaths, checkPlatformAvailability, loadPlatformColors, categorizeTokens
 *   reporter.js           — all console output / table printing
 */
 
import { parseValidatorArgs, exitWithStatus } from '../validator-utils.js';
import {
  resolvePaths,
  checkPlatformAvailability,
  loadPlatformColors,
  categorizeTokens,
} from './platform-loader.js';
import {
  printSummaryHeader,
  printResultsSummary,
  printReportTable,
  printVerboseTables,
  printFinalSummary,
} from './reporter.js';
 
/**
 * Main validation function.
 *
 * Resolves file paths from process.cwd() and parses CLI flags at call time,
 * which allows tests to call this function directly after changing cwd
 * without re-importing the module.
 */
export function validateCrossPlatformColors() {
  const { isVerbose, isReport } = parseValidatorArgs();
  const paths = resolvePaths();
 
  const platformAvailability = checkPlatformAvailability(paths);
  if (!platformAvailability) return;
 
  const { availablePlatforms, missingPlatforms } = platformAvailability;
 
  if (missingPlatforms.length > 0) {
    missingPlatforms.forEach(({ name, path }) => {
      console.log(`🟡 Warning: ${name} output not found - skipping ${name} validation`);
      console.log(`   Expected: ${path}`);
    });
    console.log('');
  }
 
  const { cssColors, androidColors, iosColors, rawCssCount, deprecatedCount } = loadPlatformColors(
    paths,
    platformAvailability,
  );
 
  const { allTokens, matching, mismatched, missing } = categorizeTokens(
    cssColors,
    androidColors,
    iosColors,
    availablePlatforms,
  );
 
  const totalTokens = allTokens.size;
  const totalIssues = mismatched.length + missing.length;
 
  printSummaryHeader(availablePlatforms, cssColors, androidColors, iosColors, rawCssCount, deprecatedCount);
  printResultsSummary(matching, mismatched, missing, totalTokens);
 
  if (isReport) printReportTable(allTokens, cssColors, androidColors, iosColors, availablePlatforms);
  if (isVerbose && totalIssues > 0) printVerboseTables(mismatched, missing, availablePlatforms);
 
  printFinalSummary(matching, mismatched, missing, totalTokens, isReport, isVerbose);
 
  exitWithStatus(totalIssues, 0);
}
 
// Auto-execute only when run directly (not when imported by tests)
import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
Iif (process.argv[1] === __filename) {
  validateCrossPlatformColors();
}