hem
Version:
stitches CommonJS, and ties up other lose ends of web-app development.
288 lines (245 loc) • 8.66 kB
JavaScript
// Generated by CoffeeScript 1.12.7
(function() {
var Hem, application, argv, compilers, events, fs, help, log, optimist, path, server, testing, utils, versioning,
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
fs = require('fs');
path = require('path');
optimist = require('optimist');
argv = optimist.usage(['usage:\nhem COMMAND', ' server :start a dynamic development server', ' build :serialize application to disk', ' watch :build & watch disk for changes', ' test :build and run tests', ' clean :clean compiled targets', ' version :version the application files', ' check :check slug file values'].join("\n")).alias('p', 'port').describe('p', ':hem server port').alias('h', 'host').describe('h', ':hem server host').alias('c', 'compress').describe('c', ':all compilations are compressed/minified').alias('w', 'watch').describe('w', ':watch files when running tests').alias('s', 'slug').describe('s', ':run hem using a specified slug file').alias('n', 'nocolors').describe('n', ':disable color in console output').alias('v', 'verbose').describe('v', ':make hem more talkative(verbose)').alias('g', 'grep').describe('g', ':only run specific modules during test').alias('r', 'runner').describe('r', ':override the default test runner').argv;
argv.command = argv._[0];
argv.targets = argv._.slice(1);
if (!!argv.nocolors) {
require("sty").disable();
}
log = require('./log');
log.VERBOSE = argv.v = !!argv.v;
utils = require('./utils');
compilers = require('./compilers');
server = require('./server');
testing = require('./test');
application = require('./package');
versioning = require('./versioning');
events = require('./events');
compilers.argv = argv;
application.argv = argv;
help = function() {
var ref;
log("<b>HEM</b> Version: <green>" + ((ref = require('../package.json')) != null ? ref.version : void 0) + "</green>\n");
optimist.showHelp();
return process.exit();
};
Hem = (function() {
Hem.exec = function(command, options) {
return (new this(options)).exec(command);
};
Hem.middleware = function(slug) {
var hem;
hem = new Hem(slug);
return server.middleware(hem);
};
Hem.prototype.options = {};
Hem.prototype.apps = [];
Hem.prototype.allApps = [];
Hem.prototype.home = process.cwd();
function Hem(options) {
var base, base1, base2, base3, base4, base5, base6, config, name, ref, slug;
switch (typeof options) {
case "string":
slug = options;
break;
case "object":
this.options = options;
break;
default:
slug || (slug = argv.slug || 'slug');
}
if (slug) {
this.options = this.readSlug(slug);
}
(base = this.options).hem || (base.hem = {});
(base1 = this.options.hem).port || (base1.port = 9294);
(base2 = this.options.hem).host || (base2.host = "localhost");
(base3 = this.options.hem).test || (base3.test = {});
(base4 = this.options.hem.test).runner || (base4.runner = "karma");
(base5 = this.options.hem.test).reporters || (base5.reporters = "progress");
(base6 = this.options.hem.test).frameworks || (base6.frameworks = "jasmine");
if (argv.port) {
this.options.hem.port = argv.port;
}
if (argv.host) {
this.options.hem.host = argv.host;
}
if (argv.runner) {
this.options.hem.test.runner = argv.runner;
}
ref = this.options;
for (name in ref) {
config = ref[name];
if (name === "hem" || typeof config === 'function') {
continue;
}
this.allApps.push(application.create(name, config, this, argv));
}
}
Hem.prototype.server = function() {
var app, i, len, ref, results, value;
value = "http://" + (this.options.hem.host || "*") + ":" + this.options.hem.port;
log("Starting Server at <blue>" + value + "</blue>");
app = server.start(this);
events.emit("server", app);
ref = this.apps;
results = [];
for (i = 0, len = ref.length; i < len; i++) {
app = ref[i];
results.push(app.watch());
}
return results;
};
Hem.prototype.clean = function() {
var app, i, len, ref, results;
ref = this.apps;
results = [];
for (i = 0, len = ref.length; i < len; i++) {
app = ref[i];
results.push(app.unlink());
}
return results;
};
Hem.prototype.build = function() {
this.clean();
return this.buildApps();
};
Hem.prototype.version = function() {
var app, i, len, ref, results;
ref = this.apps;
results = [];
for (i = 0, len = ref.length; i < len; i++) {
app = ref[i];
results.push(app.version());
}
return results;
};
Hem.prototype.watch = function() {
var app, i, len, ref, results;
this.buildApps();
ref = this.apps;
results = [];
for (i = 0, len = ref.length; i < len; i++) {
app = ref[i];
results.push(app.watch());
}
return results;
};
Hem.prototype.test = function() {
var testOptions;
this.build();
testOptions = this.options.hem.tests || {};
testOptions.basePath || (testOptions.basePath = this.home);
if (argv.watch) {
this.watch();
testOptions.singleRun = false;
} else {
testOptions.singleRun = true;
}
return testing.run(this.apps, testOptions);
};
Hem.prototype.check = function() {
var app, i, inspect, len, printOptions, ref, results;
printOptions = {
showHidden: false,
colors: !argv.nocolors,
depth: null
};
inspect = require('util').inspect;
log("> Configuration for <green>hem</green>:");
console.log(inspect(this.options.hem, printOptions));
log("");
ref = this.apps;
results = [];
for (i = 0, len = ref.length; i < len; i++) {
app = ref[i];
log("> Configuration values for <green>" + app.name + "</green>:");
console.log(inspect(app, printOptions));
results.push(log(""));
}
return results;
};
Hem.prototype.exec = function(command) {
var base;
if (command == null) {
command = argv.command;
}
if (!this[command]) {
return help();
}
this.apps = this.getTargetApps();
if (typeof (base = this.slug).custom === "function") {
base.custom(this);
}
return this[command]();
};
Hem.prototype.readSlug = function(slug) {
var error, slugPath;
slugPath = path.resolve(slug);
try {
slugPath = require.resolve(slugPath);
} catch (error1) {
error = error1;
log.errorAndExit("Couldn't find slug file " + (path.dirname(slugPath)));
}
this.home = path.dirname(slugPath);
try {
delete require.cache[slugPath];
this.slug = require(slugPath);
} catch (error1) {
error = error1;
log.errorAndExit(("Couldn't load slug file " + slugPath + ": ") + error.message);
}
return this.slug.config || this.slug;
};
Hem.prototype.getTargetApps = function(targets) {
var app, i, len, ref, ref1, results, targetAll;
if (targets == null) {
targets = argv.targets;
}
targetAll = targets.length === 0;
ref = this.allApps;
results = [];
for (i = 0, len = ref.length; i < len; i++) {
app = ref[i];
if ((ref1 = app.name, indexOf.call(targets, ref1) >= 0) || targetAll) {
results.push(app);
}
}
return results;
};
Hem.prototype.buildApps = function() {
var app, i, len, ref, results;
ref = this.apps;
results = [];
for (i = 0, len = ref.length; i < len; i++) {
app = ref[i];
results.push(app.build());
}
return results;
};
Hem.prototype.module = function(name) {
switch (name) {
case "compilers":
return compilers;
case "events":
return events;
case "reporters":
return testing.phantom.reporters;
case "versioning":
return versioning;
case "log":
return log;
default:
throw new Error("Unknown module name " + name);
}
};
return Hem;
})();
module.exports = Hem;
}).call(this);