All files / packages/design-system/scripts/core core-update.js

73.77% Statements 45/61
68.18% Branches 15/22
100% Functions 8/8
73.33% Lines 44/60

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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248                          1x 1x     1x               3x                     66x 16x 16x 16x 16x 16x   50x 50x 50x                   4x 2x           2x 1x           1x                 2x 1x           1x                   11x                                                                                                             10x                                                         7x 7x   7x 7x 7x     7x 7x 7x 49x 12x   37x         7x 7x 7x 14x 2x   12x       7x   7x       1x 1x                                                                
#!/usr/bin/env node
/**
 * Core Update Script
 * Copies generated CSS files to local Core/BLT repository for testing.
 *
 * Requires .distrc.json with localCorePath configured.
 */
import { fileURLToPath } from 'node:url';
import path from 'node:path';
import fs from 'fs-extra';
import nconf from 'nconf';
import chalk from 'chalk';
 
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
 
// Base paths
const packageRoot = path.resolve(__dirname, '../..');
 
/**
 * Create a path resolver for a given root
 * @param {string} root - Root path
 * @returns {Function} Path resolver function
 */
export function createPathResolver(root) {
  return (...args) => path.join(root, ...args);
}
 
/**
 * Copy a file from source to destination
 * @param {string} src - Source file path
 * @param {string} dest - Destination file path
 * @param {string} label - Display label for logging
 * @returns {boolean} True if file was copied, false if skipped
 */
export function copyFile(src, dest, label) {
  if (fs.existsSync(src)) {
    fs.ensureDirSync(path.dirname(dest));
    fs.copyFileSync(src, dest);
    console.log(chalk.green(`   ✓ ${label}`));
    console.log(chalk.gray(`     → ${dest}\n`));
    return true;
  } else {
    console.log(chalk.yellow(`   ⚠ ${label} not found, skipping...`));
    console.log(chalk.gray(`     Source: ${src}\n`));
    return false;
  }
}
 
/**
 * Validate configuration
 * @param {string} localCorePath - Path to local Core repo
 * @returns {{ valid: boolean, error?: string }}
 */
export function validateConfig(localCorePath) {
  if (!localCorePath) {
    return {
      valid: false,
      error: 'localCorePath is required in .distrc.json',
    };
  }
 
  if (!fs.existsSync(localCorePath)) {
    return {
      valid: false,
      error: `Local Core path not found: ${localCorePath}`,
    };
  }
 
  return { valid: true };
}
 
/**
 * Validate source directories exist
 * @param {string} distCorePath - Path to dist-core/css
 * @returns {{ valid: boolean, error?: string }}
 */
export function validateSource(distCorePath) {
  if (!fs.existsSync(distCorePath)) {
    return {
      valid: false,
      error: 'dist-core/css not found. Run release:core-css:generate first.',
    };
  }
 
  return { valid: true };
}
 
/**
 * Get file mappings for Core CSS files
 * @param {string} distCorePath - Source dist-core path
 * @param {string} localCorePath - Destination Core path
 * @returns {Array<{src: string, dest: string, label: string}>}
 */
export function getCoreCssFileMappings(distCorePath, localCorePath) {
  return [
    // shared-slds-impl CSS files
    {
      src: path.join(distCorePath, 'shared-slds-impl/resources/assets/css/slds.css'),
      dest: path.join(localCorePath, 'shared-slds-impl/resources/assets/css/slds.css'),
      label: 'slds.css',
    },
    {
      src: path.join(distCorePath, 'shared-slds-impl/resources/assets/css/scopedSlds.css'),
      dest: path.join(localCorePath, 'shared-slds-impl/resources/assets/css/scopedSlds.css'),
      label: 'scopedSlds.css',
    },
    {
      src: path.join(distCorePath, 'shared-slds-impl/resources/assets/css/sldsOffline.css'),
      dest: path.join(localCorePath, 'shared-slds-impl/resources/assets/css/sldsOffline.css'),
      label: 'sldsOffline.css',
    },
    // ui-force-components CSS files
    {
      src: path.join(distCorePath, 'ui-force-components/components/force/sldsTemplate/sldsTemplate.css'),
      dest: path.join(localCorePath, 'ui-force-components/components/force/sldsTemplate/sldsTemplate.css'),
      label: 'sldsTemplate.css',
    },
    {
      src: path.join(
        distCorePath,
        'ui-force-components/components/force/scopedSldsTemplate/scopedSldsTemplate.css',
      ),
      dest: path.join(
        localCorePath,
        'ui-force-components/components/force/scopedSldsTemplate/scopedSldsTemplate.css',
      ),
      label: 'scopedSldsTemplate.css',
    },
    {
      src: path.join(distCorePath, 'ui-force-components/components/force/sldsOffline/sldsOffline.css'),
      dest: path.join(localCorePath, 'ui-force-components/components/force/sldsOffline/sldsOffline.css'),
      label: 'sldsOffline.css (component)',
    },
    // Touch CSS
    {
      src: path.join(distCorePath, 'sfdc/htdocs/_slds/styles/sldsTouch.css'),
      dest: path.join(localCorePath, 'sfdc/htdocs/_slds/styles/sldsTouch.css'),
      label: 'sldsTouch.css',
    },
  ];
}
 
/**
 * Get file mappings for design-system-2 files
 * @param {string} sdsSubsystemsPath - Source design-system-2 path
 * @param {string} localCorePath - Destination Core path
 * @returns {Array<{src: string, dest: string, label: string}>}
 */
export function getSdsSubsystemsFileMappings(sdsSubsystemsPath, localCorePath) {
  return [
    {
      src: path.join(sdsSubsystemsPath, 'slds+.css'),
      dest: path.join(
        localCorePath,
        'ui-force-components/components/force/sldsPlusTemplate/sldsPlusTemplate.css',
      ),
      label: 'slds+.css → sldsPlusTemplate.css',
    },
    {
      src: path.join(sdsSubsystemsPath, 'scoped-slds+.css'),
      dest: path.join(
        localCorePath,
        'ui-force-components/components/force/scopedSldsPlusTemplate/scopedSldsPlusTemplate.css',
      ),
      label: 'scoped-slds+.css → scopedSldsPlusTemplate.css',
    },
  ];
}
 
/**
 * Run the core update process
 * @param {Object} options - Options
 * @param {string} options.localCorePath - Path to local Core repo
 * @param {string} options.distCorePath - Path to dist-core/css
 * @param {string} options.sdsSubsystemsPath - Path to design-system-2/dist
 * @returns {{ success: boolean, copied: number, skipped: number }}
 */
export function runCoreUpdate({ localCorePath, distCorePath, sdsSubsystemsPath }) {
  let copied = 0;
  let skipped = 0;
 
  console.log(chalk.blue('\n📦 Copying CSS files to local Core...\n'));
  console.log(chalk.gray(`   Source: ${distCorePath}`));
  console.log(chalk.gray(`   Target: ${localCorePath}\n`));
 
  // Copy Core CSS files
  console.log(chalk.cyan('   Copying Core CSS files...\n'));
  const coreMappings = getCoreCssFileMappings(distCorePath, localCorePath);
  for (const { src, dest, label } of coreMappings) {
    if (copyFile(src, dest, label)) {
      copied++;
    } else {
      skipped++;
    }
  }
 
  // Copy design-system-2 files
  console.log(chalk.cyan('   Copying slds+ files from design-system-2...\n'));
  const sdsMappings = getSdsSubsystemsFileMappings(sdsSubsystemsPath, localCorePath);
  for (const { src, dest, label } of sdsMappings) {
    if (copyFile(src, dest, label)) {
      copied++;
    } else {
      skipped++;
    }
  }
 
  console.log(chalk.green('✅ Core update complete!\n'));
 
  return { success: true, copied, skipped };
}
 
// Run if called directly
const isMainModule = process.argv[1] && fileURLToPath(import.meta.url).includes(process.argv[1]);
Iif (isMainModule) {
  const rootPath = createPathResolver(packageRoot);
 
  // Read .distrc.json configuration
  nconf.argv().file({ file: rootPath('.distrc.json') });
 
  // Get localCorePath from config - required
  const localCorePath = nconf.get('localCorePath');
  const configValidation = validateConfig(localCorePath);
  if (!configValidation.valid) {
    console.error(chalk.red(`❌ ${configValidation.error}`));
    if (!localCorePath) {
      console.error(
        chalk.gray('   Create .distrc.json from .distrc.json.example and set your local Core path.'),
      );
    }
    process.exit(1);
  }
 
  // Source paths
  const distCorePath = rootPath('dist-core/css');
  const sdsSubsystemsPath = path.resolve(packageRoot, '../../design-system-2/dist');
 
  // Validate source
  const sourceValidation = validateSource(distCorePath);
  if (!sourceValidation.valid) {
    console.error(chalk.red(`❌ ${sourceValidation.error}`));
    process.exit(1);
  }
 
  runCoreUpdate({ localCorePath, distCorePath, sdsSubsystemsPath });
}