{"size": 1979, "ext": "js", "max_stars_repo_path": "scripts/generators.js", "max_stars_repo_name": "NicKAnderson038/ReactIconBuilder", "max_stars_repo_stars_event_min_datetime": "2019-08-08T22:41:10.000Z", "max_stars_repo_stars_event_max_datetime": "2021-11-15T10:56:13.000Z", "max_issues_repo_path": "scripts/generators.js", "max_issues_repo_name": "NicKAnderson038/ReactIconBuilder", "max_issues_repo_issues_event_min_datetime": "2019-07-09T08:04:09.000Z", "max_issues_repo_issues_event_max_datetime": "2019-08-28T08:48:53.000Z", "max_forks_repo_path": "scripts/generators.js", "max_forks_repo_name": "NicKAnderson038/ReactIconBuilder", "max_forks_repo_forks_event_min_datetime": "2020-08-10T13:00:08.000Z", "max_forks_repo_forks_event_max_datetime": "2021-10-13T18:43:14.000Z"}
{"max_stars_count": 12, "max_issues_count": 2, "max_forks_count": 11, "avg_line_length": 25.0506329114, "max_line_length": 77, "alphanum_fraction": 0.5634158666}
const { toPascalCase } = require('./utils');
const { readFileSync, writeFileSync } = require('fs');
const { execSync } = require('child_process');

const generateIconComponentFile = (icons, { dir }) => {
  const indexContent = [
    "import React from 'react';",
    "import Icon from './Icon';",
    '',
    icons
      .map(
        icon =>
          `export const ${toPascalCase(
            icon,
          )} = props => <Icon {...props} name="${icon}" />;`,
      )
      .join('\n'),
  ].join('\n');

  writeFileSync(`${dir}/index.js`, indexContent);
  console.log('Icon component file created! ✅');
};

const generateWebIconMap = (icons, { dir }) => {
  const iconMapContent = [
    icons
      .map(
        icon =>
          `import { ReactComponent as ${toPascalCase(
            icon,
          )} } from './${icon}.svg';`,
      )
      .join('\n'),
    '',
    'export default {',
    icons.map(icon => `"${icon}": ${toPascalCase(icon)}, `).join('\n'),
    '};',
  ].join('\n');

  writeFileSync(`${dir}/icon-map.js`, iconMapContent);
  console.log('Web Icon Map created! ✅');
};

const generateReactNativeAsset = (icons, { dir, fontName, fontDir }) => {
  const generatorOptions = {
    name: fontName,
    css: false,
    html: false,
    types: 'ttf',
    out: fontDir,
    height: 500,
  };

  const optionString = Object.entries(generatorOptions)
    .map(([option, value]) => `--${option} ${value}`)
    .join(' ');

  execSync(`icon-font-generator ./${dir}/*.svg ${optionString}`);

  const glyphMap = JSON.parse(readFileSync(`./${fontDir}/${fontName}.json`));

  const customFontContent = [
    '{',
    icons
      .map(value => `"${value}": ${parseInt(glyphMap[value].substr(1), 16)}`)
      .join(',\n'),
    '}',
  ].join('\n');

  writeFileSync(`./${fontDir}/${fontName}.json`, customFontContent);
  console.log('React Native Asset generated! ✅');
};

module.exports = {
  generateIconComponentFile,
  generateWebIconMap,
  generateReactNativeAsset,
};
