{"size": 2628, "ext": "js", "max_stars_repo_path": "lib/helpers/generate-example-params.js", "max_stars_repo_name": "MebinAbraham/design-system", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "lib/helpers/generate-example-params.js", "max_issues_repo_name": "MebinAbraham/design-system", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/helpers/generate-example-params.js", "max_forks_repo_name": "MebinAbraham/design-system", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.5652173913, "max_line_length": 132, "alphanum_fraction": 0.6198630137}
import * as fs from 'fs';
import chalk from 'chalk';
import nunjucks from 'nunjucks';
import marked from 'marked';
import { formatHTML } from '../helpers/format-html';

import { NunjucksLoader } from '../nunjucks-loader';
import { removeFrontmatterFromString } from '../remove-frontmatter-from-string';
import setAttribute from '../filters/set-attribute';
import setAttributes from '../filters/set-attributes';

const sourcePath = `${process.cwd()}/src`;

export function generateExampleParams(params, addDependency) {
  const link = `/${params.path.replace('/index.njk', '').replace('/index.html', '')}`;
  const name = link.split('/examples/')[1];
  const path = `${sourcePath}/${params.path}`;

  if (fs.existsSync(path)) {
    addDependency(path);
    let raw = removeFrontmatterFromString(fs.readFileSync(path, 'utf8'));

    const srcPath = params.componentFolderPath ? `${sourcePath}/${params.componentFolderPath}/` : `${path.split('/examples/')[0]}/`;
    const optionsPath = `${srcPath}_macro-options.md`;
    const macroPath = `${srcPath}_macro.njk`;

    let options;
    let scss;
    let template;

    if (fs.existsSync(optionsPath)) {
      addDependency(optionsPath);
      options = marked(fs.readFileSync(optionsPath, 'utf8'), {
        smartypants: true,
      });
    }

    if (fs.existsSync(srcPath)) {
      const sassFiles = fs.readdirSync(srcPath).filter(file => {
        const fileNameParts = file.split('.');
        const ext = fileNameParts[fileNameParts.length - 1];

        return ext === 'scss';
      });

      const sassRaw = sassFiles.map(file => {
        addDependency(`${srcPath}${file}`);
        return fs.readFileSync(`${srcPath}${file}`, 'utf8');
      });
      scss = sassRaw.join('\n');
    }

    if (fs.existsSync(macroPath)) {
      addDependency(macroPath);
      template = `{% raw %}${fs.readFileSync(macroPath, 'utf8')}{% endraw %}`;
    }

    const loader = new NunjucksLoader(sourcePath);
    const environment = new nunjucks.Environment(loader);

    environment.addFilter('setAttribute', setAttribute);
    environment.addFilter('setAttributes', setAttributes);

    nunjucks.configure(null, {
      watch: false,
      autoescape: true,
    });

    const html = formatHTML(nunjucks.compile(raw, environment).render(), true);

    if (path.match(/.njk$/)) {
      raw = `{% raw %}${raw}{% endraw %}`;
    } else {
      raw = undefined;
    }

    return {
      ...params,
      link,
      raw,
      options,
      html,
      scss,
      name,
      template,
    };
  } else {
    console.log('');
    console.log(chalk.red(`Path: '${path}' not found.`));
  }
}
