<commit_before>'use strict';

var fs      =  require('fs');
var gutil   = require('gulp-util');
var through = require('through2');
var lodash  = require('lodash');
var path    = require('path');
var Minimize = require('minimize');

function templateTransformPlugin(options) {

  var defaultOptions = {
    root: '',
    minify: {}
  };

  var opts = lodash.extend(defaultOptions, options);

  return through.obj(objectStream);

  function objectStream(file, enc, cb) {
    /* jshint validthis: true */

    var _this = this;

    if (file.isNull()) {
      this.push(file);
      return cb();
    }

    if (file.isStream()) {
      _this.emit('error', pluginError('Streaming not supported'));
      return cb();
    }

    try {
      var contents = file.contents.toString();
      // file.contents = new Buffer(transform(contents, opts));
      transform(file, contents, opts);
    } catch (err) {
      err.fileName = file.path;
      _this.emit('error', pluginError(err));
    }

    _this.push(file);
    cb();
  }
}

function pluginError(msg) {
  return new gutil.PluginError('gulp-directive-replace', msg);
}

function transform(file, content, opts) {

  if (!content) {
    return content;
  }

  var templateUrlRegExp = /templateUrl\:[^\'\"]*(\'|\")([^\'\"]+)(\'|\")/gm;

  var output = content.toString();
  var templateUrlDirective = templateUrlRegExp.exec(output);
  var templateUrl = path.join(opts.root, templateUrlDirective[2]);
  var template = fs.readFileSync( templateUrl , 'utf8');


  var minimize = new Minimize(opts.minify);
  minimize.parse(template, function (err, data) {
      if (err) {
        return callback(pluginError(err));
      }

      var result = output.replace(templateUrlRegExp, 'template: \'' + data + '\'');
      file.contents = new Buffer(result);
  });<commit_msg>[BUGFIX] Fixed bug of error when trying to compile a directive without templateUrl property<commit_after>'use strict';

var fs      =  require('fs');
var gutil   = require('gulp-util');
var through = require('through2');
var lodash  = require('lodash');
var path    = require('path');
var Minimize = require('minimize');

function templateTransformPlugin(options) {

  var defaultOptions = {
    root: '',
    minify: {}
  };

  var opts = lodash.extend(defaultOptions, options);

  return through.obj(objectStream);

  function objectStream(file, enc, cb) {
    /* jshint validthis: true */

    var _this = this;

    if (file.isNull()) {
      this.push(file);
      return cb();
    }

    if (file.isStream()) {
      _this.emit('error', pluginError('Streaming not supported'));
      return cb();
    }

    try {
      var contents = file.contents.toString();
      // file.contents = new Buffer(transform(contents, opts));
      transform(file, contents, opts);
    } catch (err) {
      err.fileName = file.path;
      _this.emit('error', pluginError(err));
    }

    _this.push(file);
    cb();
  }
}

function pluginError(msg) {
  return new gutil.PluginError('gulp-directive-replace', msg);
}

function transform(file, content, opts) {

  if (!content) {
    return content;
  }

  var templateUrlRegExp = /templateUrl\:[^\'\"]*(\'|\")([^\'\"]+)(\'|\")/gm;

  var output = content.toString();
  var templateUrlDirective = templateUrlRegExp.exec(output);

  if (!templateUrlDirective) {
    /* exits if the directive doesn't have templateUrl */
    return;
  }

  var templateUrl = path.join(opts.root, templateUrlDirective[2]);
  var template = fs.readFileSync( templateUrl , 'utf8');


  var minimize = new Minimize(opts.minify);
  minimize.parse(template, function (err, data) {
      if (err) {
        return callback(pluginError(err));
      }

      var result = output.replace(templateUrlRegExp, 'template: \'' + data + '\'');
      file.contents = new Buffer(result);
  });
{'fraction_non_alphanumeric': 0.16013673415724428, 'fraction_numerical': 0.0015777018143570865, 'mean_word_length': 3.2266666666666666, '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': '5114247', 'n_tokens_mistral': 1221, 'n_tokens_neox': 1177, 'n_words': 275}