All files / packages/sds-metadata/src/generators bem-naming-map.ts

0% Statements 0/11
0% Branches 0/2
0% Functions 0/1
0% Lines 0/10

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                                                                                       
import { CSSParser } from '../parsers/css-parser.js';
import { BEMDoubleDashProcessor, BEMDoubleDashProcessorResult } from '../processors/index.js';
import { BEM_NAMING_FILENAME, SLDS_CSS_PATH, SLDS_PLUS_CSS_PATH } from '../constants.js';
import { writeData } from '../services/file-service.js';
import { SupportedFileFormat } from '../types.js';
 
async function parseForSelectros(cssFilePath: string): Promise<BEMDoubleDashProcessorResult> {
  const processor = new BEMDoubleDashProcessor();
  const cssParser = await CSSParser.parseCSSFile(cssFilePath);
  return cssParser.processSelectors(processor);
}
 
/**
 * This function parses SLDS.css and SLDSPlus.css files from aura/slds-scs repo
 * to generate a mapping of -- (bem) notion selectors to _ notion selectors.
 * @param outputDir - Custom output directory for generated files. Defaults to current working directory
 */
export async function generateBemNamingMap(outputDir: string = process.cwd(), format: SupportedFileFormat = 'json') {
  // Process both files in parallel
  const [sldsBem, sldsPlusBem] = await Promise.all([
    parseForSelectros(SLDS_CSS_PATH),
    parseForSelectros(SLDS_PLUS_CSS_PATH)
  ]);
 
  // Merge maps efficiently
  const mergedMap = new Map<string, string>();
  
  // Add all entries from both maps
  for (const [key, value] of Object.entries(sldsBem)) {
    mergedMap.set(key, value);
  }
  for (const [key, value] of Object.entries(sldsPlusBem)) {
    mergedMap.set(key, value);
  }
 
  // Convert to sorted array of entries for JSON stringification
  const sortedEntries = Array.from(mergedMap.entries()).sort(([a], [b]) => a.localeCompare(b));
  
  // Convert back to object for JSON stringification
  const sortedObject = Object.fromEntries(sortedEntries);
 
  // Write to file
  await writeData(BEM_NAMING_FILENAME, sortedObject, outputDir, format);
}