<commit_before>var through = require('through2');
var gutil = require('gulp-util');
var MailChimpAPI = require('mailchimp').MailChimpAPI;
var PluginError = gutil.PluginError;
var api;

// Consts
const PLUGIN_NAME = 'gulp-inline-css';

function prefixStream(prefixText) {
  var stream = through();
  stream.write(prefixText);
  return stream;
}


// Plugin level function (dealing with files)
function gulpInlineCSS(apikey, opt) {
  if (!apikey) {
    throw new PluginError(PLUGIN_NAME, "Missing MailChimp API Key");
  }
  if (!opt) opt = {};

  //prefixText = new Buffer(prefixText); // allocate ahead of time
  
  try { 
    api = new MailChimpAPI(apikey, { version : '2.0' });
  } catch (error) {
    console.log(error.message);
  }

  // Creating a stream through which each file will pass
  var stream = through.obj(function (file, enc, callback) {
    if (file.isNull()) {
      this.push(file); // Do nothing if no contents
      return callback();
    }

    if (file.isBuffer()) {
      // file.contents = Buffer.concat([file.contents]);
      gutil.log('Inlining CSS of', gutil.colors.magenta(path.file));
      api.call('helper', 'inline-css', { 'html': file.contents.toString('utf8'), 'strip_css': true  }, function (error, data) {
        if (error) {
          console.log(error.message);
        } else {
          //console.log(JSON.stringify(data)); // Do something with your data!
          file.path = gutil.replaceExtension(file.path, '.email');
          file.contents = new Buffer(data.html);
          this.push(file);
          return callback();
        }
      }.bind(this));
    }

    if (file.isStream()) {
      file.contents = file.contents.pipe(prefixStream(prefixText));
      this.push(file);
      return callback();
    }
  });

  // returning the file stream
  return stream;
};

// Exporting the plugin main function
module.exports = gulpInlineCSS;<commit_msg>Removed renaming part, best done with gulp-rename.<commit_after>var through = require('through2');
var gutil = require('gulp-util');
var MailChimpAPI = require('mailchimp').MailChimpAPI;
var PluginError = gutil.PluginError;
var path = require('path');
var api;

// Consts
const PLUGIN_NAME = 'gulp-inline-css';

function prefixStream(prefixText) {
  var stream = through();
  stream.write(prefixText);
  return stream;
}


function gulpInlineCSS(apikey, opt) {
  if (!apikey) {
    throw new PluginError(PLUGIN_NAME, "Missing MailChimp API Key");
  }
  if (!opt) opt = {};
  
  try { 
    api = new MailChimpAPI(apikey, { version : '2.0' });
  } catch (error) {
    console.log(error.message);
  }

  // Creating a stream through which each file will pass
  var stream = through.obj(function (file, enc, callback) {
    if (file.isNull()) {
      this.push(file); // Do nothing if no contents
      return callback();
    }

    if (file.isBuffer()) {
      gutil.log('Inlining CSS of', gutil.colors.magenta(file.path) + '...');
      api.call('helper', 'inline-css', { 'html': file.contents.toString('utf8'), 'strip_css': true  }, function (error, data) {
        if (error) {
          // TODO: Emit an error message
          console.log(error.message);
        } else {
          // file.path = gutil.replaceExtension(file.path, '.email');
          gutil.log(gutil.colors.magenta(path.basename(file.path)), 'was', gutil.colors.green('inlined'));
          file.contents = new Buffer(data.html);
        }
        this.push(file);
        return callback();
      }.bind(this));
    }

    if (file.isStream()) {
      file.contents = file.contents.pipe(prefixStream(prefixText));
      this.push(file);
      return callback();
    }
  });

  // returning the file stream
  return stream;
}

// Exporting the plugin main function
module.exports = gulpInlineCSS;
{'fraction_non_alphanumeric': 0.14934892373106565, 'fraction_numerical': 0.002125963327132607, 'mean_word_length': 2.953781512605042, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1473262', 'n_tokens_mistral': 1194, 'n_tokens_neox': 1174, 'n_words': 285}