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

const Promise = require('bluebird');


const transformers = {
  // All transformers take an Uint8Array as input and output
  // an object. The result object will include the transformed buffer
  eot: require('ttf2eot'),
  woff: require('ttf2woff'),
  woff2: require('ttf2woff2'),
};

const transformFontTo = (ext, ttfBuffer) => {
  const func = transformers[ext];
  const result = func(new Uint8Array(ttfBuffer));
  return new Buffer(result.buffer);
};

const writeTransformedFonts = (dirname, filename, ttfBuffer) => {
  const writeFile = Promise.promisify(fs.writeFile);

  const promises = Object.keys(transformers).map((ext) => {
    const filepath = `./${dirname}/${filename}.${ext}`;

    // Skips transform and write actions when a font file already exists
    const exists = fs.existsSync(filepath);
    if (exists === true) {
      return Promise.resolve();
    }

    const buffer = transformFontTo(ext, ttfBuffer);
    return writeFile(filepath, buffer);
  });

  return Promise.all(promises);
};

const createCSSFontFaceTemplate = (dirname, filename, styles) => {
  return `
    @font-face {
      font-family: ${styles.fontFamily};
      font-weight: ${styles.fontWeight};
      font-style: ${styles.fontStyle};
      src: url('${dirname}/${filename}.eot');
      src: url('${dirname}/${filename}.woff2') format('woff2'),
          url('${dirname}/${filename}.woff') format('woff'),
          url('${dirname}/${filename}.ttf')  format('truetype');
    }
  `;
};

const fontfeest = function fontfeest(file, fontFamily, fontWeight = 400, fontStyle = 'normal') {
  const dirname = path.dirname(file);
  const filename = path.basename(file, '.ttf');
  const ttfBuffer = fs.readFileSync(file);

  return writeTransformedFonts(dirname, filename, ttfBuffer).then(() => {
    const style = { fontFamily, fontWeight, fontStyle };
    return createCSSFontFaceTemplate(dirname, filename, style);
  });
};

module.exports = fontfeest;

fontfeest('./fixtures/OpenSans-Light.ttf', 'Open Sans', 300).then((css) => {
  console.log(css);
});<commit_msg>Removes testing function in index.js<commit_after>const fs = require('fs');
const path = require('path');

const Promise = require('bluebird');


const transformers = {
  // All transformers take an Uint8Array as input and output
  // an object. The result object will include the transformed buffer
  eot: require('ttf2eot'),
  woff: require('ttf2woff'),
  woff2: require('ttf2woff2'),
};

const transformFontTo = (ext, ttfBuffer) => {
  const func = transformers[ext];
  const result = func(new Uint8Array(ttfBuffer));
  return new Buffer(result.buffer);
};

const writeTransformedFonts = (dirname, filename, ttfBuffer) => {
  const writeFile = Promise.promisify(fs.writeFile);

  const promises = Object.keys(transformers).map((ext) => {
    const filepath = `./${dirname}/${filename}.${ext}`;

    // Skips transform and write actions when a font file already exists
    const exists = fs.existsSync(filepath);
    if (exists === true) {
      return Promise.resolve();
    }

    const buffer = transformFontTo(ext, ttfBuffer);
    return writeFile(filepath, buffer);
  });

  return Promise.all(promises);
};

const createCSSFontFaceTemplate = (dirname, filename, styles) => {
  return `
    @font-face {
      font-family: ${styles.fontFamily};
      font-weight: ${styles.fontWeight};
      font-style: ${styles.fontStyle};
      src: url('${dirname}/${filename}.eot');
      src: url('${dirname}/${filename}.woff2') format('woff2'),
          url('${dirname}/${filename}.woff') format('woff'),
          url('${dirname}/${filename}.ttf')  format('truetype');
    }
  `;
};

const fontfeest = function fontfeest(file, fontFamily, fontWeight = 400, fontStyle = 'normal') {
  const dirname = path.dirname(file);
  const filename = path.basename(file, '.ttf');
  const ttfBuffer = fs.readFileSync(file);

  return writeTransformedFonts(dirname, filename, ttfBuffer).then(() => {
    const style = { fontFamily, fontWeight, fontStyle };
    return createCSSFontFaceTemplate(dirname, filename, style);
  });
};

module.exports = fontfeest;
{'fraction_non_alphanumeric': 0.15709824645688206, 'fraction_numerical': 0.006485707422531828, 'mean_word_length': 4.153465346534653, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, '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': '1383024', 'n_tokens_mistral': 1304, 'n_tokens_neox': 1281, 'n_words': 305}