All files / packages/design-system-2/scripts/buildCss file-lists.js

100% Statements 12/12
100% Branches 0/0
100% Functions 5/5
100% Lines 8/8

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                                                    26x                         15x                         2x 424x 424x   1x                         5x                                 6x          
/**
 * Core file collection helpers and dev/production file list builders.
 *
 * Shared helpers (isNonEmpty, ignoreThemes, getFoundationFilesForTheme) are
 * exported for use by file-lists-theme-layer.js. Import sorting lives in sort-imports.js.
 *
 * Public exports:
 *   getDevFileList()                   - dev bundle: config + component CSS + sub-themes (no tokens, no themes/)
 *   getProductionFileListForTheme()    - production bundle for a given theme
 *   getFoundationFilesForTheme()       - config + tokens + legacy hooks for a given theme
 *   isNonEmpty()                       - checks if a file has non-whitespace content
 *   ignoreThemes()                     - glob ignore patterns for themes/ subdirectories
 */
 
import * as glob from 'glob';
import path from 'node:path';
import fs from 'fs-extra';
import { args } from './args.js';
import config, { root, designTokensDist } from './config.js';
 
// ─── Shared helpers ───────────────────────────────────────────────────────────
 
/**
 * Absolute glob patterns that exclude all themes/ subdirectories.
 * Glob ignores require absolute paths; relative patterns do not match absolute results.
 */
export const ignoreThemes = () => [
  path.resolve(root, `src/${args['--system']}/**/themes/**`),
  path.resolve(root, `src/sub-themes/**/themes/**`),
];
 
/**
 * Returns the foundation files (config + tokens + legacy hooks) for a specific theme.
 *
 * Sources tokens directly from @salesforce-ux/design-tokens dist.
 * Shared by getProductionFileListForTheme() and file-lists-theme-layer.js.
 *
 * @param {string} themeName Theme name (e.g. "cosmos", "lightning-blue").
 */
export const getFoundationFilesForTheme = (themeName) => [
  ...glob.sync(config.sources.config),
  ...glob.sync(path.resolve(designTokensDist, `themes/${themeName}/**/*.css`)),
  ...glob.sync(config.sources.legacyHooksShared),
  ...glob.sync(path.resolve(root, `src/legacy-hooks/${themeName}/**/*.css`)),
];
 
/**
 * Returns true if a file exists and contains non-whitespace content.
 * Used to filter out blank theme files so they don't produce empty @import rules.
 *
 * @param {string} file Absolute path to a CSS file.
 */
export const isNonEmpty = (file) => {
  try {
    return fs.readFileSync(file, 'utf8').trim().length > 0;
  } catch {
    return false;
  }
};
 
// ─── File list builders ───────────────────────────────────────────────────────
 
/**
 * Dev file list: config + shared legacy hooks + component CSS + sub-themes.
 *
 * Themed tokens and theme-scoped component hooks are omitted; the Storybook
 * decorator handles theme switching at runtime via separate token imports.
 * themes/ subdirectories are also excluded.
 */
export const getDevFileList = () => [
  ...glob.sync([config.sources.config, config.sources.input, config.sources.subThemes], {
    ignore: ignoreThemes(),
  }),
  ...glob.sync(config.sources.legacyHooksShared),
];
 
/**
/**
 * Production file list for a specific theme.
 *
 * Used by the production orchestrator to loop all themes.
 * Theme-layer component CSS (component themes/ directories) is excluded;
 * those belong exclusively in the theme-layer outputs.
 *
 * @param {string} themeName Theme name (e.g. "cosmos", "lightning-blue").
 */
export const getProductionFileListForTheme = (themeName) => [
  ...getFoundationFilesForTheme(themeName),
  ...glob.sync(config.sources.input, { ignore: ignoreThemes() }),
  ...glob.sync(config.sources.subThemes, { ignore: ignoreThemes() }),
];