<commit_before>const fs = require('fs');
const path = require('path');
const sass = require('node-sass');

const components = 'src/components/';
const inputFolder = 'src/themes/';
const outputFolder = 'src/themes.built/';
const time = new Date();

export { generateTheme };


function isInArray(value, array) {
  return array.indexOf(value) > -1;
}

function walkDir(dir, done) {
  const globalCSS = []; // save global CSS
  const componentCSS = {}; // save component css theme
  const data = {}; // temporary container for file content
  let cssContent = ''; // temporary container for string addition

  fs.readdir(dir, (err, list) => {
    if (err) {
      return done(err);
    }

    let pending = list.length;
    if (!pending) {
      return done(null, data);
    }
    list.forEach((file) => {
      pending -= 1;
      file = path.resolve(dir, file);
      fs.stat(file, (err2, stat) => {
        if (stat && stat.isDirectory()) {
          walkDir(file, (err3, dt) => {
            for (const a of Object.keys(dt)) {
              const filename = path.parse(a).name;
              const brandName = path.basename(path.dirname(a));

              if (!isInArray(brandName, globalCSS)) {
                globalCSS.push(brandName);
              }

              cssContent = '';
              cssContent += `\nexport const ${brandName} = \``;
              cssContent += sass.renderSync({ data: dt[a] }).css;
              cssContent += '`;\n';

              if (componentCSS[filename]) {
                componentCSS[filename] += cssContent;
              } else {
                componentCSS[filename] = cssContent;
              }
            }
            data[file] = dt;
            if (!pending) {
              done(null, data, globalCSS, componentCSS);
            }
          });
        } else {
          fs.readFile(file, 'utf-8', (err4, content) => {
            if (err4) {
              console.log(err4);
            }
            data[file] = content;
            if (!pending) {
              done(null, data);
            }
          });
        }
      });
    });
  });
}

function generateTheme(callback = function () {}) {
  let counter = 0;
  let addString = '';

  if (!fs.existsSync(outputFolder)) {
    fs.mkdirSync(outputFolder);
  }

  fs.readdir(components, (err, files) => {
    if (err) {
      console.log(err);
    }
    files.forEach(file => {
      walkDir(inputFolder, (err2, data, globalCSS, componentCSS) => {
        // console.log('============ checking ', file)
        if (err2) {
          console.log(err2);
        }
        for (key in componentCSS) {
          if (`c-${file}` === key) {
            addString = '\n// Auto Generated Below\n';
            addString += componentCSS[key];

            fs.writeFile(
              `${outputFolder + key}.ts`,
              addString,
              'utf8',
              err3 => {
                if (err3) {
                  throw err3;
                }
                counter += 1;
                console.log(`[${time.getMinutes()}:${time.getSeconds()}:${time.getMilliseconds().toString()[0]}]  Generated: themes.built/${key}.ts`);
                if (counter === Object.keys(componentCSS).length) {
                  callback();
                }
              },<commit_msg>Fix for script error - Fix for error in theme script<commit_after>const fs = require('fs');
const path = require('path');
const sass = require('node-sass');

const components = 'src/components/';
const inputFolder = 'src/themes/';
const outputFolder = 'src/themes.built/';
const time = new Date();

export { generateTheme };


function isInArray(value, array) {
  return array.indexOf(value) > -1;
}

function walkDir(dir, done) {
  const globalCSS = []; // save global CSS
  const componentCSS = {}; // save component css theme
  const data = {}; // temporary container for file content
  let cssContent = ''; // temporary container for string addition

  fs.readdir(dir, (err, list) => {
    if (err) {
      return done(err);
    }

    let pending = list.length;
    if (!pending) {
      return done(null, data);
    }
    list.forEach((file) => {
      file = path.resolve(dir, file);
      fs.stat(file, (err2, stat) => {
        if (stat && stat.isDirectory()) {
          walkDir(file, (err3, dt) => {
            pending -= 1;
            for (const a of Object.keys(dt)) {
              const filename = path.parse(a).name;
              const brandName = path.basename(path.dirname(a));

              if (!isInArray(brandName, globalCSS)) {
                globalCSS.push(brandName);
              }

              cssContent = '';
              cssContent += `\nexport const ${brandName} = \``;
              cssContent += sass.renderSync({ data: dt[a] }).css;
              cssContent += '`;\n';

              if (componentCSS[filename]) {
                componentCSS[filename] += cssContent;
              } else {
                componentCSS[filename] = cssContent;
              }
            }
            data[file] = dt;
            if (!pending) {
              done(null, data, globalCSS, componentCSS);
            }
          });
        } else {
          fs.readFile(file, 'utf-8', (err4, content) => {
            pending -= 1;
            if (err4) {
              console.log(err4);
            }
            data[file] = content;
            if (!pending) {
              done(null, data);
            }
          });
        }
      });
    });
  });
}

function generateTheme(callback = function () {}) {
  let counter = 0;
  let addString = '';

  if (!fs.existsSync(outputFolder)) {
    fs.mkdirSync(outputFolder);
  }

  fs.readdir(components, (err, files) => {
    if (err) {
      console.log(err);
    }
    files.forEach(file => {
      walkDir(inputFolder, (err2, data, globalCSS, componentCSS) => {
        // console.log('============ checking ', file)
        if (err2) {
          console.log(err2);
        }
        for (const key in componentCSS) {
          if (`c-${file}` === key) {
            addString = '\n// Auto Generated Below\n';
            addString += componentCSS[key];

            fs.writeFile(
              `${outputFolder + key}.ts`,
              addString,
              'utf8',
              err3 => {
                if (err3) {
                  throw err3;
                }
                counter += 1;
                console.log(`[${time.getMinutes()}:${time.getSeconds()}:${time.getMilliseconds().toString()[0]}]  Generated: themes.built/${key}.ts`);
                if (counter === Object.keys(componentCSS).length) {
                  callback();
                }
              },
{'fraction_non_alphanumeric': 0.15241244551330227, 'fraction_numerical': 0.0055614008717871635, 'mean_word_length': 1.6562874251497006, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2682144', 'n_tokens_mistral': 1921, 'n_tokens_neox': 1871, 'n_words': 424}