All files / packages/design-system-2/scripts copyComponentAssets.ts

0% Statements 0/44
0% Branches 0/12
0% Functions 0/8
0% Lines 0/36

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                                                                                                                                         
import fs from 'fs-extra';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
 
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const root = path.resolve(__dirname, '../');
const srcSlds2 = path.resolve(root, 'src/slds2');
const componentsDist = path.resolve(root, 'dist/components');
// Per-component theme-layer CSS (themes/*.css) is an internal artifact consumed
// by tooling (the catalog hook audit), not part of the published package, so it
// lands under build/components/ rather than dist/components/.
const componentsBuild = path.resolve(root, 'build/components');
 
// Ensure destination directories exist
fs.ensureDirSync(componentsDist);
fs.ensureDirSync(componentsBuild);
 
// Find all slds2 component directories
const componentDirs = fs
  .readdirSync(srcSlds2, { withFileTypes: true })
  .filter((entry) => entry.isDirectory())
  .map((entry) => path.resolve(srcSlds2, entry.name));
 
// True when `src` is inside a component's themes/ subdirectory.
const isThemeAsset = (src: string) => path.relative(srcSlds2, src).split(path.sep).includes('themes');
 
// Shared exclusions: underscore dirs (e.g. __stories__), JS files, and UIF files.
const isExcludedAsset = (src: string) => {
  const pathParts = path.relative(srcSlds2, src).split(path.sep);
  if (pathParts.some((part) => part.startsWith('__'))) return true;
  if (path.extname(src) === '.js') return true;
  if (path.basename(src).endsWith('.uif.json')) return true;
  return false;
};
 
// dist/components/: everything except themes/ (which is not published).
const distFilterFunc = (src: string) => {
  if (isExcludedAsset(src)) return false;
  if (isThemeAsset(src)) return false;
  return true;
};
 
// build/components/themes/: theme assets only (JS/UIF never live under themes/,
// but keep the shared exclusion for consistency).
const themeFilterFunc = (src: string) => !isExcludedAsset(src);
 
// Copy each component directory: structure CSS to dist/, theme CSS to build/.
componentDirs.forEach((componentDir) => {
  const componentName = path.basename(componentDir);
 
  console.log(`Copying component: ${componentName}`);
 
  const distDest = path.resolve(componentsDist, componentName);
  // Ensure destination exists before copying (handles race condition with build:uif)
  fs.ensureDirSync(distDest);
  fs.copySync(componentDir, distDest, { filter: distFilterFunc, overwrite: true });
 
  // Only stage a build/ entry for components that actually ship theme files.
  const themesSrc = path.resolve(componentDir, 'themes');
  if (fs.existsSync(themesSrc)) {
    const themesDest = path.resolve(componentsBuild, componentName, 'themes');
    fs.ensureDirSync(themesDest);
    fs.copySync(themesSrc, themesDest, { filter: themeFilterFunc, overwrite: true });
  }
});
 
console.log('Component structure CSS copied to dist/components/, theme CSS to build/components/');