<commit_before>import Babel from "npm:babel";
import blueprints from '../lib/blueprints';

export default Em.Service.extend({

  compileGist (gist) {
    var out = gist.get('files').map(file => {
      switch(file.get('extension')) {
        case '.js':
          return this.compileJs(file.get('content'), file.get('nameWithModule'));
        case '.hbs':
          return this.compileHbs(file.get('content'), file.get('nameWithModule'));
      }
    });

    // Add app, router, config
    out.push(this.compileJs(blueprints.app, 'demo-app/app'));
    out.push(this.compileJs(blueprints.router, 'demo-app/router'));
    out.push(this.compileJs('export default {modulePrefix:"demo-app"}', 'demo-app/config/environment'));

    // Add boot code
    contentForAppBoot(out, {modulePrefix:'demo-app'});
    return out.join('\n');
  },

  compileJs (code, moduleName) {
    return Babel.transform(code, babelOpts(moduleName)).code;
  },

  compileHbs (code, moduleName) {
    var templateCode = Em.HTMLBars.precompile(code || '');
    return this.compileJs('export default Ember.HTMLBars.template(' + templateCode + ');', moduleName);
  }
});

function babelOpts(moduleName) {
  return {
    modules:'amd',
    moduleIds:true,
    moduleId: moduleName
  };
}

function contentForAppBoot (content, config) {
  // Some modules are not actually transpiled so Babel
  // doesn't recognize them properly...
  var monkeyPatchModules = [
    'ember',
    'ember/resolver',
    'ember/load-initializers'
  ];

  monkeyPatchModules.forEach(function(mod) {
    content.push('  require("'+mod+'").__esModule=true;');
  });

  content.push('  require("' +
    config.modulePrefix +
    '/app")["default"].create(' +
    calculateAppConfig(config) +
    ');');
}

function calculateAppConfig(config) {
  return JSON.stringify(config.APP || {});
}
<commit_msg>document so I understand this next week :-)<commit_after>import Babel from "npm:babel";
import blueprints from '../lib/blueprints';

/**
 * A tiny browser version of the CLI build chain.
 * or more realistically: a hacked reconstruction of it.
 *
 * Parts of this module are directly copied from the ember-cli
 * source code at https://github.com/ember-cli/ember-cli
 */
export default Em.Service.extend({

  /**
   * Build a gist into an Ember app.
   *
   * @param  {Gist} gist    Gist to build
   * @return {String}       Source code for built Ember app
   */
  compileGist (gist) {
    var out = gist.get('files').map(file => {
      switch(file.get('extension')) {
        case '.js':
          return this.compileJs(file.get('content'), file.get('nameWithModule'));
        case '.hbs':
          return this.compileHbs(file.get('content'), file.get('nameWithModule'));
      }
    });

    // Add app, router, config
    out.push(this.compileJs(blueprints.app, 'demo-app/app'));
    out.push(this.compileJs(blueprints.router, 'demo-app/router'));
    out.push(this.compileJs('export default {modulePrefix:"demo-app"}', 'demo-app/config/environment'));

    // Add boot code
    contentForAppBoot(out, {modulePrefix:'demo-app'});

    return out.join('\n');
  },

  /**
   * Compile a javascript file. This means that we
   * transform it using Babel.
   *
   * @param  {String} code       ES6 module code
   * @param  {String} moduleName Name for the module
   * @return {String}            Transpiled module code
   */
  compileJs (code, moduleName) {
    return Babel.transform(code, babelOpts(moduleName)).code;
  },

  /**
   * Compile a Handlebars template into an AMD module.
   *
   * @param  {String} code       hbs code
   * @param  {String} moduleName Name for the module
   * @return {String}            AMD module code
   */
  compileHbs (code, moduleName) {
    var templateCode = Em.HTMLBars.precompile(code || '');
    return this.compileJs('export default Ember.HTMLBars.template(' + templateCode + ');', moduleName);
  }
});

/**
 * Generate babel options for the specified module
 * @param  {String} moduleName
 * @return {Object}            Babel options
 */
function babelOpts(moduleName) {
  return {
    modules:'amd',
    moduleIds:true,
    moduleId: moduleName
  };
}

/**
 * Generate the application boot code
 * @param  {Array} content  Code buffer to append to
 * @param  {Object} config  App configuration
 * @return {Array}          Code buffer
 */
function contentForAppBoot (content, config) {
  // Some modules are not actually transpiled so Babel
  // doesn't recognize them properly...
  var monkeyPatchModules = [
    'ember',
    'ember/resolver',
    'ember/load-initializers'
  ];

  monkeyPatchModules.forEach(function(mod) {
    content.push('  require("'+mod+'").__esModule=true;');
  });

  content.push('  require("' +
    config.modulePrefix +
    '/app")["default"].create(' +
    calculateAppConfig(config) +
    ');');
}

/**
 * Directly copied from ember-cli
 */
function calculateAppConfig(config) {
  return JSON.stringify(config.APP || {});
}

{'fraction_non_alphanumeric': 0.15088996763754045, 'fraction_numerical': 0.0002022653721682848, 'mean_word_length': 3.2120954003407154, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 1, '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': '3068274', 'n_tokens_mistral': 1548, 'n_tokens_neox': 1530, 'n_words': 362}