<commit_before>"use strict";

var fs = require("fs");
var Handlebars = require("handlebars");

/**
 * @type {Function}
 */
var replaceVersion;

function augmentConfig(config) {
  //noinspection JSHint
  Object.keys(config).forEach(function(key) {
    if (typeof this[key] === "object") {
      var obj = this[key];

      if (obj.all) {
        obj.local = obj.all;
        obj.cdn = obj.all;
      }
      else {
        augmentConfig(obj);
      }
    }
  }, config);
}

function checkComponent(config, component, configType) {
  if (!config[component]) {
    throw new Error("No path specified for Bower component '" + component + "'");
  }

  if (config[component][configType]) {
    return config[component][configType];
  }
  else {
    return config[component];
  }
}

function generateSafeBowerLinkTag(link) {
  return new Handlebars.SafeString("<link rel=\"stylesheet\" href=\"" + link + "\">");
}

function generateSafeLocalBowerLinkTag(config, component) {
  var cfg = checkComponent(config, component, "css");

  return generateSafeBowerLinkTag(cfg.local);
}

function generateSafeBowerScriptTag(config, component) {
  var cfg = checkComponent(config, component, "js");

  return new Handlebars.SafeString("<script src=\"" + cfg.local + "\"></script>");
}

function generateSafeCDNBowerLinkTag(config, component) {
  var cfg = checkComponent(config, component, "css");

  return generateSafeBowerLinkTag(replaceVersion(component, cfg.cdn, cfg["use-version"]));
}

function generateSafeCDNScriptTag(config, component) {
  var cfg = checkComponent(config, component, "js");

  var link = replaceVersion(component, cfg.cdn, cfg["use-version"]);

  return new Handlebars.SafeString("<script type=\"application/javascript\" src=\"" + link + "\"></script>");
}

module.exports = {
  configure: function(config) {
    augmentConfig(config);

    var DEV_ENV = "development";
    var env = process.env.NODE_ENV || DEV_ENV;
    var isProd = env !== DEV_ENV;

    config.bower = JSON.parse(fs.readFileSync(config.bower));

    replaceVersion = function(component, base, useComponentVersion) {
      component = useComponentVersion || component;

      var version = config.bower.devDependencies[component].replace(/~/g, "");

      return base.replace(/\{version\}/, version);
    };

    return {
      configureHandlebarsHelpers: function(handlebarsConfig) {
        var helpers;
        handlebarsConfig.helpers = helpers = (handlebarsConfig.helpers || {});

        helpers.js = function(component) {
          if (isProd) {
            return generateSafeCDNScriptTag(config, component);
          }

          return generateSafeBowerScriptTag(config, component);
        };

        helpers.css = function(component) {<commit_msg>Fix to recognise other environmentis (eg: test) and use local bower components.<commit_after>"use strict";

var fs = require("fs");
var Handlebars = require("handlebars");

/**
 * @type {Function}
 */
var replaceVersion;

function augmentConfig(config) {
  //noinspection JSHint
  Object.keys(config).forEach(function(key) {
    if (typeof this[key] === "object") {
      var obj = this[key];

      if (obj.all) {
        obj.local = obj.all;
        obj.cdn = obj.all;
      }
      else {
        augmentConfig(obj);
      }
    }
  }, config);
}

function checkComponent(config, component, configType) {
  if (!config[component]) {
    throw new Error("No path specified for Bower component '" + component + "'");
  }

  if (config[component][configType]) {
    return config[component][configType];
  }
  else {
    return config[component];
  }
}

function generateSafeBowerLinkTag(link) {
  return new Handlebars.SafeString("<link rel=\"stylesheet\" href=\"" + link + "\">");
}

function generateSafeLocalBowerLinkTag(config, component) {
  var cfg = checkComponent(config, component, "css");

  return generateSafeBowerLinkTag(cfg.local);
}

function generateSafeBowerScriptTag(config, component) {
  var cfg = checkComponent(config, component, "js");

  return new Handlebars.SafeString("<script src=\"" + cfg.local + "\"></script>");
}

function generateSafeCDNBowerLinkTag(config, component) {
  var cfg = checkComponent(config, component, "css");

  return generateSafeBowerLinkTag(replaceVersion(component, cfg.cdn, cfg["use-version"]));
}

function generateSafeCDNScriptTag(config, component) {
  var cfg = checkComponent(config, component, "js");

  var link = replaceVersion(component, cfg.cdn, cfg["use-version"]);

  return new Handlebars.SafeString("<script type=\"application/javascript\" src=\"" + link + "\"></script>");
}

module.exports = {
  configure: function(config) {
    augmentConfig(config);

    var PROD_ENV = "production";
    var env = process.env.NODE_ENV || "development";
    var isProd = env === PROD_ENV;

    config.bower = JSON.parse(fs.readFileSync(config.bower));

    replaceVersion = function(component, base, useComponentVersion) {
      component = useComponentVersion || component;

      var version = config.bower.devDependencies[component].replace(/~/g, "");

      return base.replace(/\{version\}/, version);
    };

    return {
      configureHandlebarsHelpers: function(handlebarsConfig) {
        var helpers;
        handlebarsConfig.helpers = helpers = (handlebarsConfig.helpers || {});

        helpers.js = function(component) {
          if (isProd) {
            return generateSafeCDNScriptTag(config, component);
          }

          return generateSafeBowerScriptTag(config, component);
        };

        helpers.css = function(component) {
{'fraction_non_alphanumeric': 0.14123581336696092, 'fraction_numerical': 0.0, 'mean_word_length': 3.7011007620660457, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '6495932', 'n_tokens_mistral': 1644, 'n_tokens_neox': 1605, 'n_words': 349}