var fs = require("fs");
var path = require("path");
var vm = require("vm");

var execSync = require("exec-sync");
var EventEmitter = require("events").EventEmitter;

var Inflect = require("inflect-js");

/*
 * constructor function
 */
function ElmRunner(filename) {
  this.filename = filename;

  this.baseName = path.basename(filename, path.extname(filename));
  this.moduleName = Inflect.classify(this.baseName);

  this.outputPath = path.join(path.dirname(filename), this.baseName + ".js");

  this.compile();
  this.execute();
  this.wrap();
}

/**
 * run elm module through `elm-make` to generate compiled js
 */
ElmRunner.prototype.compile = function() {
  execSync("elm-make " + this.filename + " --output " + this.outputPath);
}

/**
 * execute script generated by elm-make in a vm context
 */
ElmRunner.prototype.execute = function() {
  var context = vm.createContext();
  var compiledOutput = fs.readFileSync(this.outputPath)

  vm.runInContext(compiledOutput, context, this.outputPath);

  this.compiledModule = context.Elm.worker(context.Elm[this.moduleName]);
}

/**
 * wrap compiled and executed object in EventEmitters
 */
ElmRunner.prototype.wrap = function(compiledModule) {
  var emitter = new EventEmitter();

  return emitter;
}

/**
 * expose a function that wraps new instances
 */
module.exports = function(filename) {
  return new ElmRunner(filename);
}

var fs = require("fs");
var path = require("path");
var vm = require("vm");

var execSync = require("exec-sync");
var EventEmitter = require("events").EventEmitter;

var Inflect = require("inflect-js");

/*
 * constructor function
 */
function ElmRunner(filename) {
  this.filename = filename;

  this.baseName = path.basename(filename, path.extname(filename));
  this.moduleName = Inflect.classify(this.baseName);

  this.outputPath = path.join(path.dirname(filename), this.baseName + ".js");

  this.compile();
  this.execute();
  this.wrap();
}

/**
 * run elm module through `elm-make` to generate compiled js
 */
ElmRunner.prototype.compile = function() {
  execSync("elm-make " + this.filename + " --output " + this.outputPath);
};

/**
 * execute script generated by elm-make in a vm context
 */
ElmRunner.prototype.execute = function() {
  var context = vm.createContext();
  var compiledOutput = fs.readFileSync(this.outputPath)

  vm.runInContext(compiledOutput, context, this.outputPath);

  this.compiledModule = context.Elm.worker(context.Elm[this.moduleName]);
};

/**
 * wrap compiled and executed object in EventEmitters
 */
ElmRunner.prototype.wrap = function(compiledModule) {
  var emitter = new EventEmitter();

  return emitter;
};

/**
 * expose a function that wraps new instances
 */
module.exports = function(filename) {
  return new ElmRunner(filename);
};

add semicolons after function assignments