All files / packages/sds-components/scripts buildRegistry.js

0% Statements 0/18
100% Branches 0/0
0% Functions 0/4
0% Lines 0/18

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                                                                                                                       
import * as glob from 'glob';
import path from 'path';
import fs from 'fs-extra';
import { fileURLToPath } from 'url';
 
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
 
const root = path.resolve(__dirname, '../');
 
// find component modules, return array
const findModules = () => {
  return glob.sync(path.resolve(root, 'src/sds/**/*.js'), {
    ignore: [
      path.resolve(root, '**/__stories__/*.stories.js'),
      path.resolve(root, '**/__tests__/**/*.js'),
      path.resolve(root, '**/__mocks__/**/*.js'),
      path.resolve(root, '**/index.js'),
      path.resolve(root, '**/utils/**/*.js'),
      path.resolve(root, '**/privateThemeProvider/*.js'),
    ],
  });
};
 
const entries = findModules().reduce((obj, entry) => {
  const name = path.parse(entry).name;
  obj = Object.assign(obj, { [name]: entry });
  return obj;
}, {});
 
const imports = () => {
  const imports = Object.keys(entries)
    .sort()
    .reduce((arr, entry) => {
      arr.push(`import ${entry.replaceAll('-', '')} from "./${entry}";`);
      return arr;
    }, []);
  return imports.join('\n');
};
 
const registries = () => {
  const registries = Object.keys(entries)
    .sort()
    .reduce((arr, entry) => {
      const name = entry.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
      arr.push(
        `if (customElements.get("sds-${name}") === undefined) customElements.define("sds-${name}", ${entry.replaceAll(
          '-',
          '',
        )}.CustomElementConstructor);`,
      );
      return arr;
    }, []);
  return registries.join('\n');
};
 
const content = [imports(), '\n', registries()].join('');
 
fs.outputFile(path.resolve(root, 'dist/define.js'), content);