All files / packages/fds-uif/generator-lwc/scripts build.js

0% Statements 0/54
0% Branches 0/14
0% Functions 0/8
0% Lines 0/51

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                                                                                                                                                                                                                               
/**
 * Write all LWC Components to /dist
 */
 
import { resolveExtends, getModifiers } from '@fds-uif/core';
import { analyzeUif } from '@fds-uif/generator-base';
import { writeFile, mkdir, cp, readdir, stat, access } from 'node:fs/promises';
import { join, dirname, basename, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
 
import { generateComponent } from '../dist/index.js';
 
const __dirname = dirname(fileURLToPath(import.meta.url));
 
const distDir = resolve(import.meta.dirname, './../dist');
const uifDir = resolve(import.meta.dirname, './../../../sds-subsystems/src/slds2');
 
async function getDirectories(folderPath) {
  const entries = await readdir(folderPath, { withFileTypes: true });
 
  const directories = entries.filter((entry) => entry.isDirectory()).map((entry) => entry.name);
 
  return directories;
}
 
function red(text) {
  return `\x1b[31m${text}\x1b[0m`;
}
 
function green(text) {
  return `\x1b[32m${text}\x1b[0m`;
}
 
async function fileExists(path) {
  try {
    await access(path);
    return true; // File exists
  } catch (error) {
    // If an error is thrown, the file does not exist or is inaccessible
    if (error.code === 'ENOENT') {
      return false;
    }
    throw error; // Re-throw other errors
  }
}
 
async function folderExists(folder) {
  try {
    await access(folder);
    return true;
  } catch (error) {
    // If an error is thrown, the file does not exist or is inaccessible
    if (error.code === 'ENOENT') {
      return false;
    }
    throw error; // Re-throw other errors
  }
}
 
const possibleComponentDirs = await getDirectories(uifDir);
// typo needs to be fixed later on
const badNames = {
  'form-element': 'formElement',
};
// path, name
const uifFiles = [];
for (let name of possibleComponentDirs) {
  const path = join(uifDir, name, `${name}.system.slds.uif.json`);
  if (await fileExists(path)) {
    uifFiles.push({ path, name: name in badNames ? badNames[name] : name });
  }
}
 
// append data
for (let uifFile of uifFiles) {
  const uif = await resolveExtends(uifFile.path);
  // { path, name, uif }
  uifFile.uif = uif;
}
 
const failedToProcess = [];
for (let uifFile of uifFiles) {
  const { name, uif } = uifFile;
  try {
    const generated = await generateComponent(uif);
    for (let file of generated.files) {
      if (!(await folderExists(join(distDir, name)))) {
        await mkdir(join(distDir, name));
      }
      await writeFile(join(distDir, name, file.path), file.content);
    }
  } catch (e) {
    failedToProcess.push(name);
  }
}
if (failedToProcess.length) {
  console.log(red('Failed to process:'), failedToProcess.join(', '));
}
console.log(green(`Finished writing ${uifFiles.length} components to ./dist`));
 
// Write a JS file with JSON meta for the components
for (let uifFile of uifFiles) {
  if (failedToProcess.includes(uifFile.name)) {
    uifFile.meta = null;
    const removeIndex = uifFiles.findIndex((x) => x.name === uifFile.name);
    uifFiles.splice(removeIndex, 1);
    continue; // skip failures
  }
  uifFile.meta = analyzeUif(uifFile.uif);
}
await writeFile(join(distDir, 'meta.js'), `export const uifFiles = ${JSON.stringify(uifFiles)};`);