All files / packages/sds-metadata/src/generators all-styling-hooks.ts

0% Statements 0/20
0% Branches 0/2
0% Functions 0/3
0% Lines 0/18

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                                                                                                           
import { SLDS_CSS_PATH, SLDS_DEPRECATED_STYLING_HOOKS_FILENAME, SLDS_PLUS_CSS_PATH, SLDS_PLUS_STYLING_HOOKS_FILENAME, SLDS_STYLING_HOOKS_FILENAME } from "../constants.js";
import { CSSParser } from "../parsers/css-parser.js";
import { StylingHooksProcessor, StylingHooksProcessorResults } from "../processors/index.js";
import { writeData } from "../services/file-service.js";
import { SupportedFileFormat } from "../types.js";
export async function parseForStylingHooks(cssFilePath: string): Promise<StylingHooksProcessorResults> {
  const cssParser = await CSSParser.parseCSSFile(cssFilePath);
  return cssParser.processDeclerations(new StylingHooksProcessor());
}
 
export function getDeprecatedStylingHooks(sldsStylingHooks: StylingHooksProcessorResults, sldsPlusStylingHooks: StylingHooksProcessorResults): string[] {
  const allSldsStylingHooks = Object.values(sldsStylingHooks).flat();
  const allSldsPlusStylingHooks = Object.values(sldsPlusStylingHooks).flat();
  return allSldsStylingHooks.filter(stylingHook => !allSldsPlusStylingHooks.includes(stylingHook));
}
 
function filterSDSHooks(styleHooksMap: StylingHooksProcessorResults): StylingHooksProcessorResults {
  return Object.entries(styleHooksMap).reduce((acc, [key, value]) => {
    const filteredValue = value.filter(stylingHook => !stylingHook.startsWith('--sds-'));
    acc[key] = filteredValue;
    return acc;
  }, {} as StylingHooksProcessorResults); 
}
 
/**
 * This function parses SLDS.css and SLDSPlus.css files from aura/slds-scs repo
 * to generate:
 *  - list of styling hooks in slds
 *  - list of styling hooks in slds plus / cosmos
 *  - list of styling hooks which were only available in slds and deprecated in slds plus / cosmos
 * @param outputDir - Custom output directory for generated files. Defaults to current working directory
 */
export async function generateStylingHooks(outputDir: string = process.cwd(), format: SupportedFileFormat = 'json') {
  try {
    // Process both files in parallel
    const [sldsStylingHooks, sldsPlusStylingHooks] = await Promise.all([
        parseForStylingHooks(SLDS_CSS_PATH),
        parseForStylingHooks(SLDS_PLUS_CSS_PATH)
      ]);
 
    const deprecatedStylingHooks = getDeprecatedStylingHooks(sldsStylingHooks, sldsPlusStylingHooks);
    await writeData(SLDS_DEPRECATED_STYLING_HOOKS_FILENAME, deprecatedStylingHooks, outputDir, format);
 
    const filteredSldsStylingHooks = filterSDSHooks(sldsStylingHooks);
    await writeData(SLDS_STYLING_HOOKS_FILENAME, filteredSldsStylingHooks, outputDir, format);
 
    const filteredSldsPlusStylingHooks = filterSDSHooks(sldsPlusStylingHooks);
    await writeData(SLDS_PLUS_STYLING_HOOKS_FILENAME, filteredSldsPlusStylingHooks, outputDir, format);
 
  } catch (error) {
    console.error("Error processing CSS files:", error);
    throw error;
  }
}