All files / packages/design-system/scripts/core write-core-dist.js

100% Statements 36/36
89.47% Branches 17/19
100% Functions 8/8
100% Lines 35/35

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                  68x 68x     68x 68x     68x                   12x 12x 4x       8x 8x 2x 2x     6x 6x                   7x                             6x                   11x 11x 11x                   5x 5x                   4x 4x                   8x 8x   3x 3x                         5x 5x   5x     5x   5x 3x     5x 2x      
/**
 * Write Core Dist
 * Utility for writing CSS files to the dist-core directory
 */
import { fileURLToPath } from 'node:url';
import path from 'node:path';
import fs from 'fs-extra';
import chalk from 'chalk';
 
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
 
// Paths
const packageRoot = path.resolve(__dirname, '../..');
const defaultDistCorePath = path.join(packageRoot, 'dist-core/css');
 
// AgentForce CSS injection from design-system-2
const sdsSubsystemsRoot = path.resolve(packageRoot, '../design-system-2');
 
/**
 * Get AgentForce CSS content for injection
 * Checks dist first (built), then falls back to src
 * @param {string} subsystemsRoot - Path to design-system-2 package root
 * @returns {string} AgentForce CSS content or empty string
 */
export function getAgentForceInjectionCSS(subsystemsRoot = sdsSubsystemsRoot) {
  // Check dist first (preferred - from build)
  const distPath = path.join(subsystemsRoot, 'dist/sub-themes/agentforce/agentforce.css');
  if (fs.existsSync(distPath)) {
    return fs.readFileSync(distPath, 'utf8');
  }
 
  // Fallback to source
  const srcPath = path.join(subsystemsRoot, 'src/sub-themes/agentforce/agentforce.css');
  if (fs.existsSync(srcPath)) {
    console.log(chalk.yellow('Warning: Using agentforce.css from src/ (dist not built)'));
    return fs.readFileSync(srcPath, 'utf8');
  }
 
  console.log(chalk.yellow('Warning: AgentForce CSS not found, skipping injection'));
  return '';
}
 
/**
 * Build the component path for ui-force-components
 * @param {string} distCorePath - Base dist-core path
 * @param {string} componentName - Component directory name
 * @returns {string} Full component file path
 */
export function buildComponentPath(distCorePath, componentName) {
  return path.join(
    distCorePath,
    'ui-force-components/components/force',
    componentName,
    `${componentName}.css`,
  );
}
 
/**
 * Build the shared path for shared-slds-impl
 * @param {string} distCorePath - Base dist-core path
 * @param {string} filename - Filename (without extension)
 * @returns {string} Full shared file path
 */
export function buildSharedPath(distCorePath, filename) {
  return path.join(distCorePath, 'shared-slds-impl/resources/assets/css', `${filename}.css`);
}
 
/**
 * Write CSS to a file path
 * @param {string} css - CSS content
 * @param {string} filePath - Full file path
 * @param {string} label - Display label for logging
 */
export function writeCssFile(css, filePath, label) {
  fs.ensureDirSync(path.dirname(filePath));
  fs.writeFileSync(filePath, css, 'utf8');
  console.log(chalk.gray(`Writing file ${label} to ${filePath} - ${chalk.bgGreen('DONE')}`));
}
 
/**
 * Write CSS to ui-force-components directory
 * @param {string} css - CSS content
 * @param {string} componentName - Component directory name
 * @param {string} distCorePath - Base dist-core path (optional, for testing)
 */
export function writeDistComponent(css, componentName, distCorePath = defaultDistCorePath) {
  const componentPath = buildComponentPath(distCorePath, componentName);
  writeCssFile(css, componentPath, `${componentName}.css`);
}
 
/**
 * Write CSS to shared-slds-impl directory
 * @param {string} css - CSS content
 * @param {string} filename - Filename (without extension)
 * @param {string} distCorePath - Base dist-core path (optional, for testing)
 */
export function writeDistShared(css, filename, distCorePath = defaultDistCorePath) {
  const sharedPath = buildSharedPath(distCorePath, filename);
  writeCssFile(css, sharedPath, `${filename}.css`);
}
 
/**
 * Append AgentForce CSS to content
 * @param {string} css - Original CSS
 * @param {string} subsystemsRoot - Path to design-system-2 package root
 * @returns {string} CSS with AgentForce appended
 */
export function appendAgentForceCss(css, subsystemsRoot = sdsSubsystemsRoot) {
  const agentForceCss = getAgentForceInjectionCSS(subsystemsRoot);
  if (!agentForceCss) return css;
  // Strip CSS comments to match the PostCSS discardComments pass on the main CSS
  const stripped = agentForceCss.replace(/\/\*[\s\S]*?\*\//g, '');
  return css + '\n' + stripped;
}
 
/**
 * Write CSS to both component and shared directories
 * @param {Object} result - PostCSS result with css property
 * @param {string|null} componentName - Component name for ui-force-components (null to skip)
 * @param {string|null} sharedFilename - Filename for shared-slds-impl (null to skip)
 * @param {Object} options - Optional paths for testing
 * @param {string} options.distCorePath - Override dist-core path
 * @param {string} options.sdsSubsystemsRoot - Override design-system-2 root path
 */
export default function writeDist(result, componentName, sharedFilename, options = {}) {
  const distCorePath = options.distCorePath || defaultDistCorePath;
  const subsystemsRoot = options.sdsSubsystemsRoot || sdsSubsystemsRoot;
 
  const css = result.css;
 
  // Append AgentForce injection CSS
  const finalCss = appendAgentForceCss(css, subsystemsRoot);
 
  if (componentName) {
    writeDistComponent(finalCss, componentName, distCorePath);
  }
 
  if (sharedFilename) {
    writeDistShared(finalCss, sharedFilename, distCorePath);
  }
}