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

0% Statements 0/13
0% Branches 0/4
0% Functions 0/4
0% Lines 0/11

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                                                                   
import fs from 'fs-extra';
import path from 'path';
import { Glob } from 'glob';
import postcss from 'postcss';
import { fileURLToPath } from 'url';
import postcssComponentSpecsHooks from '../plugins/postcss-component-specs-hooks.js';
 
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
 
// Setup directories
const root = path.resolve(__dirname, '../');
const cssFiles = path.resolve(root, 'src/sds/**/*.css');
const files = new Glob(cssFiles, { ignore: '**/node_modules/**' });
 
async function buildComponentSpec() {
  for await (const file of files) {
    const specDir = path.resolve(file, '../__specs__');
    const componentName = path.basename(file, '.css');
    if (fs.pathExistsSync(specDir)) {
      fs.readFile(file, (err, data) => {
        if (err) throw err;
        postcss([postcssComponentSpecsHooks])
          .process(data, { from: file })
          .then((result) => {
            fs.writeFileSync(`${specDir}/${componentName}.hooks.mdx`, result.markdownString, () => true);
          });
      });
    }
  }
}
 
buildComponentSpec();