<commit_before>module.exports = {
  name: 'fastboot',
  description: 'Runs a server to render your app using FastBoot.',

  availableOptions: [
    { name: "build", type: Boolean, default: true }
  ],

  anonymousOptions: [
    '<app-js-path>',
    '<vendor-js-path>'
  ],

  triggerBuild: function(commandOptions) {
    var BuildTask = this.tasks.Build;
    var buildTask = new BuildTask({
      ui: this.ui,
      analytics: this.analytics,
      project: this.project
    });

    commandOptions.environment = commandOptions.environment || 'development';
    commandOptions.outputPath = 'dist';
    return buildTask.run(commandOptions);
  },

  runCommand: function(rawArgs) {
    var fs = require('fs');

    var appFile = rawArgs[0];
    var vendorFile = rawArgs[1];

    appFile = fs.readFileSync(appFile);
    vendorFile = fs.readFileSync(vendorFile);

    var sandbox = createSandbox();

    sandbox.sandbox.run(vendorFile.toString());
    sandbox.sandbox.run(appFile.toString());

    var express = require('express');
    var app = express();

    app.get("/*", function(req, res) {
      sandbox.promise.then(function(handleURL) {
        console.log("Handling", req.path);
        handleURL(req.path).then(res.send.bind(res));
      }, function(err) {
        console.log(err);
      });
    });

    var server = app.listen(3000, function() {
      var host = server.address().address;
      var port = server.address().port;

      console.log('Ember FastBoot running at http://%s:%s', host, port);
    });

    var RSVP    = require('rsvp');
    var Promise = RSVP.Promise;

    // Block forever
    return new Promise(function() { });
  },

  run: function(options, rawArgs) {
    var self = this;

    if (options.build) {
      return this.triggerBuild(options)
        .then(function() {
          return self.runCommand(rawArgs);
        });
    }

    return self.runCommand(rawArgs);
  }
};

function createSandbox() {
  var SimpleDOM = require('simple-dom');
  var Contextify = require('contextify');
  var RSVP    = require('rsvp');

  var deferred = RSVP.defer();

  var sandbox = {
    // Expose this so that the FastBoot initializer has access to the fake DOM.
    // We don't expose this as `document` so that other libraries don't mistakenly
    // think they have a full DOM.
    SimpleDOM: SimpleDOM,

    // Expose the console to the FastBoot environment so we can debug
    console: console,

    // setTimeout is an assumed part of JavaScript environments. Expose it.
    setTimeout: setTimeout,

    // Convince jQuery not to assume it's in a browser
    module: { exports: {} },

    // Expose a hook for the Ember app to provide its handleURL functionality
    FastBoot: { resolve: deferred.resolve }
  };

  // Set the global as `window`.
  sandbox.window = sandbox;

  // The sandbox is now a JavaScript context O_o
  Contextify(sandbox);

  return { sandbox: sandbox, promise: deferred.promise };
}<commit_msg>Automatically detect built artifact paths<commit_after>module.exports = {
  name: 'fastboot',
  description: 'Runs a server to render your app using FastBoot.',

  availableOptions: [
    { name: "build", type: Boolean, default: true },
    { name: 'environment', type: String, default: 'development', aliases: ['e',{'dev' : 'development'}, {'prod' : 'production'}] },
    { name: "port", type: Number, default: 3000 }
  ],

  triggerBuild: function(commandOptions) {
    var BuildTask = this.tasks.Build;
    var buildTask = new BuildTask({
      ui: this.ui,
      analytics: this.analytics,
      project: this.project
    });

    commandOptions.environment = commandOptions.environment || 'development';
    commandOptions.outputPath = 'dist';
    return buildTask.run(commandOptions);
  },

  run: function(options, args) {
    var runCommand = function() {
      return this.runCommand(this.project.name(), options);
    }.bind(this);

    if (options.build) {
      return this.triggerBuild(options)
        .then(runCommand);
    }

    return runCommand();
  },

  runCommand: function(appName, options) {
    var RSVP = require('rsvp');

    var port = options.port;

    var emberApp = bootEmberApp(appName);
    startServer(emberApp, port);

    // Block forever
    return new RSVP.Promise(function() { });
  }
};

function startServer(emberApp, port) {
  var express = require('express');
  var app = express();

  app.get("/*", function(req, res) {
    emberApp.waitForBoot().then(function(handleURL) {
      console.log("Handling", req.path);
      handleURL(req.path).then(res.send.bind(res));
    }, function(err) {
      console.log(err);
    });
  });

  var server = app.listen(port, function() {
    var host = server.address().address;
    var port = server.address().port;

    console.log('Ember FastBoot running at http://%s:%s', host, port);
  });
}

function bootEmberApp(appName) {
  var fs = require('fs');

  var sandbox = createSandbox();

  appFile = readAppFile(appName);
  vendorFile = readVendorFile();

  sandbox.sandbox.run(vendorFile.toString());
  sandbox.sandbox.run(appFile.toString());

  return {
    sandbox: sandbox.sandbox,
    waitForBoot: function() {
      return sandbox.promise;
    }
  };
}

function readAppFile(appName) {
  return readFile("app", "dist/assets/" + appName + "*.js");
}

function readVendorFile() {
  return readFile("vendor", "dist/assets/vendor*.js");
}

function readFile(name, globPath) {
  var fs = require('fs');
  var glob = require('glob');

  var files = glob.sync(globPath);

  assert("Found " + files.length + " " + name + " files (expected 1) when globbing '" + globPath + "'.", files.length === 1);

  return fs.readFileSync(files[0]);
}

function createSandbox() {
  var SimpleDOM = require('simple-dom');
  var Contextify = require('contextify');
  var RSVP    = require('rsvp');

  var deferred = RSVP.defer();

  var sandbox = {
    // Expose this so that the FastBoot initializer has access to the fake DOM.
    // We don't expose this as `document` so that other libraries don't mistakenly
    // think they have a full DOM.
    SimpleDOM: SimpleDOM,

    // Expose the console to the FastBoot environment so we can debug
    console: console,

    // setTimeout is an assumed part of JavaScript environments. Expose it.
    setTimeout: setTimeout,

    // Convince jQuery not to assume it's in a browser
    module: { exports: {} },

    // Expose a hook for the Ember app to provide its handleURL functionality
    FastBoot: { resolve: deferred.resolve }
  };

  // Set the global as `window`.
  sandbox.window = sandbox;

  // The sandbox is now a JavaScript context O_o
  Contextify(sandbox);

  return { sandbox: sandbox, promise: deferred.promise };
}

function assert(message, condition) {
  var SilentError = require('ember-cli/lib/errors/silent');

  if (condition === false) {
    throw new SilentError(message);
  }
}
{'fraction_non_alphanumeric': 0.13555620267909144, 'fraction_numerical': 0.0018928363424577752, 'mean_word_length': 3.1579903147699757, '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': '3915829', 'n_tokens_mistral': 2147, 'n_tokens_neox': 2048, 'n_words': 569}