<commit_before>import jspm from 'jspm';
import whacko from 'whacko';
import fs from 'fs';
import path from 'path';
import * as log from '../logger';
import globby from 'globby';
import utils from 'systemjs-builder/lib/utils';
import _ from 'lodash';

export function bundleTemplate(pattern, fileName, _opts) {
  var templates = [];

  let options = _.defaultsDeep(_opts, {
    packagePath: '.'
  });

  jspm.setPackagePath(options.packagePath);

  var builder = new jspm.Builder();
  var baseURL = builder.loader.baseURL;
  var cwd = utils.fromFileURL(baseURL);;
  var outfile = path.resolve(utils.fromFileURL(baseURL), fileName);

  if (fs.existsSync(outfile)) {
    if (!options.force) {
      log.err('A bundle named `' + outfile + '` is already exists. Use --force to overwrite.');
      return;
    }
    fs.unlinkSync(outfile);
  }

  globby
    .sync(pattern, {
      cwd: cwd.replace(/\\/g, '/')
    })
    .forEach(function(file) {
      file = path.resolve(cwd, file);
      var content = fs.readFileSync(file, {
        encoding: 'utf8'
      });

      var $ = whacko.load(content);
      var name = getCanonicalName(builder, file, 'view');

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

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

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


function injectLink(outfile, baseURL, injectOptions) {
  var link = '';
  var fileName = injectOptions.indexFile;
  var bundle = path.resolve(baseURL, path.relative(baseURL, outfile));
  var index = path.resolve(baseURL, fileName || 'index.html');
  var destFile = injectOptions.destFile ? path.resolve(baseURL, injectOptions.destFile) : index;

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

  if (!relpath.startsWith('.')) {
    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(destFile, $.html());
}

function getCanonicalName(builder, file, pluginName) {
  return builder.getCanonicalName(utils.toFileURL(file) + '!' + pluginName);
}
<commit_msg>Changed bundler to use the same call as js loader<commit_after>import jspm from 'jspm';
import config from 'jspm/lib/config';
import whacko from 'whacko';
import fs from 'fs';
import path from 'path';
import * as log from '../logger';
import globby from 'globby';
import utils from 'systemjs-builder/lib/utils';
import _ from 'lodash';

export function bundleTemplate(pattern, fileName, _opts) {
  var templates = [];

  let options = _.defaultsDeep(_opts, {
    packagePath: '.'
  });

  jspm.setPackagePath(options.packagePath);

  var builder = new jspm.Builder();
  var baseURL = builder.loader.baseURL;
  var cwd = utils.fromFileURL(baseURL);;
  var outfile = path.resolve(utils.fromFileURL(baseURL), fileName);

  if (fs.existsSync(outfile)) {
    if (!options.force) {
      log.err('A bundle named `' + outfile + '` is already exists. Use --force to overwrite.');
      return;
    }
    fs.unlinkSync(outfile);
  }

  globby
    .sync(pattern, {
      cwd: cwd.replace(/\\/g, '/')
    })
    .forEach(function(file) {
      file = path.resolve(cwd, file);
      var content = fs.readFileSync(file, {
        encoding: 'utf8'
      });

      var $ = whacko.load(content);
      var name = getCanonicalName(builder, file, 'view');

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

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

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


function injectLink(outfile, baseURL, injectOptions) {
  var link = '';
  var fileName = injectOptions.indexFile;
  var bundle = path.resolve(baseURL, path.relative(baseURL, outfile));
  var index = path.resolve(baseURL, fileName || 'index.html');
  var destFile = injectOptions.destFile ? path.resolve(baseURL, injectOptions.destFile) : index;

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

  if (!relpath.startsWith('.')) {
    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(destFile, $.html());
}

function getCanonicalName(builder, file, pluginName) {
  var fileName = builder.getCanonicalName(utils.toFileURL(path.resolve(config.pjson.baseURL, file)));
  return builder.getCanonicalName(fileName + '!' + pluginName);
}

{'fraction_non_alphanumeric': 0.1702209414024976, 'fraction_numerical': 0.0011527377521613833, 'mean_word_length': 3.6275555555555554, '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': '7571108', 'n_tokens_mistral': 1744, 'n_tokens_neox': 1721, 'n_words': 365}