<commit_before>import api from 'jspm/api';
import whacko from 'whacko';
import glob from 'glob';
import fs from 'fs';
import url from 'url';
import path from 'path';

var pluginName = 'view';

function bundleJS(moduleExpression, outfile, options) {
  api.bundle(moduleExpression, outfile, options);
}

export default function bundle(config) {

  var loader = api.Builder().loader;
  var baseURL = loader.baseURL;
  var paths = loader.paths;

  var jsConfig = config.js;
  var templateConfig = config.template;

  Object.keys(jsConfig)
    .forEach(function(key) {
      var cfg = jsConfig[key];
      var outfile = key + '.js';
      var moduleExpr = cfg.modules.join(' + ');
      var opt = cfg.options;

      bundleJS(moduleExpr, outfile, opt);
    });

  if (!templateConfig) return;

  Object.keys(templateConfig)
    .forEach(function(key) {
      var cfg = templateConfig[key];
      var outfile = key + '.html';
      var pattern = cfg.pattern;
      var options = cfg.options;

      bundleTemplate(pattern, outfile, options, baseURL, paths);
    });
}


function bundleTemplate(pattern, outfile, options, baseURL, paths) {
  var templates = [];
  var cwd = baseURL.replace(/^file:/, '');

    glob
      .sync(pattern, {
        cwd: cwd
      })
      .forEach(function(file) {

        file = path.resolve(cwd, file);

        var content = fs.readFileSync(file, {
          encoding: 'utf8'
        });
        var $ = whacko.load(content);
        var tid = getTemplateId(file, baseURL, paths);

        $('template').attr('id', tid);
        var template = $.html('template');
        templates.push(template);
      });

    fs.writeFileSync(outfile, templates.join('\n'));

  if (options.inject) {
    injectLink(outfile, baseURL);
  }
}

function injectLink(outfile, baseURL) {
  var bu = baseURL.replace(/^file:/, '') + path.sep;
  var link = '';
  var bundle = path.resolve(bu, path.relative(bu, outfile));
  var index = path.resolve(bu, 'index.html');

  var relpath = path.relative(path.dirname(index), path.dirname(bundle)).replace(/\\/g, '/');

  //regex : !link.startsWith('.')
  if (!(/^\./.test(relpath))) {
    link = relpath ? './' + relpath + '/' + path.basename(bundle) : './' + path.basename(bundle);
  } else {
    link = relpath + '/' + path.basename(bundle);
  }

  var content = fs.readFileSync(index, {
    encoding: 'utf8'
  });

  var $ = whacko.load(content);

  $('body').append('<link aurlia-view-bundle rel="import" href="' + link + '">');

  fs.writeFileSync(index, $.html());
}

<commit_msg>feat(view bundle): `inject` will only inject if the link is not present<commit_after>import api from 'jspm/api';
import whacko from 'whacko';
import glob from 'glob';
import fs from 'fs';
import url from 'url';
import path from 'path';

var pluginName = 'view';

function bundleJS(moduleExpression, outfile, options) {
  api.bundle(moduleExpression, outfile, options);
}

export default function bundle(config) {

  var loader = api.Builder().loader;
  var baseURL = loader.baseURL;
  var paths = loader.paths;

  var jsConfig = config.js;
  var templateConfig = config.template;

  Object.keys(jsConfig)
    .forEach(function(key) {
      var cfg = jsConfig[key];
      var outfile = key + '.js';
      var moduleExpr = cfg.modules.join(' + ');
      var opt = cfg.options;

      bundleJS(moduleExpr, outfile, opt);
    });

  if (!templateConfig) return;

  Object.keys(templateConfig)
    .forEach(function(key) {
      var cfg = templateConfig[key];
      var outfile = key + '.html';
      var pattern = cfg.pattern;
      var options = cfg.options;

      bundleTemplate(pattern, outfile, options, baseURL, paths);
    });
}


function bundleTemplate(pattern, outfile, options, baseURL, paths) {
  var templates = [];
  var cwd = baseURL.replace(/^file:/, '');

    glob
      .sync(pattern, {
        cwd: cwd
      })
      .forEach(function(file) {

        file = path.resolve(cwd, file);

        var content = fs.readFileSync(file, {
          encoding: 'utf8'
        });
        var $ = whacko.load(content);
        var tid = getTemplateId(file, baseURL, paths);

        $('template').attr('id', tid);
        var template = $.html('template');
        templates.push(template);
      });

    fs.writeFileSync(outfile, templates.join('\n'));

  if (options.inject) {
    injectLink(outfile, baseURL);
  }
}

function injectLink(outfile, baseURL) {
  var bu = baseURL.replace(/^file:/, '') + path.sep;
  var link = '';
  var bundle = path.resolve(bu, path.relative(bu, outfile));
  var index = path.resolve(bu, 'index.html');

  var relpath = path.relative(path.dirname(index), path.dirname(bundle)).replace(/\\/g, '/');

  //regex : !link.startsWith('.')
  if (!(/^\./.test(relpath))) {
    link = relpath ? './' + relpath + '/' + path.basename(bundle) : './' + path.basename(bundle);
  } else {
    link = relpath + '/' + path.basename(bundle);
  }

  var content = fs.readFileSync(index, {
    encoding: 'utf8'
  });

  var $ = whacko.load(content);

  if($('link[aurelia-view-bundle][href="'+ link +'"]').length === 0 ){
    $('body').append('<link aurelia-view-bundle rel="import" href="' + link + '">');
  }

  fs.writeFileSync(index, $.html());
}


{'fraction_non_alphanumeric': 0.1695468509984639, 'fraction_numerical': 0.0009600614439324117, 'mean_word_length': 3.031733746130031, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4967228', 'n_tokens_mistral': 1718, 'n_tokens_neox': 1679, 'n_words': 387}