Skip to content

Commit

Permalink
Added D6
Browse files Browse the repository at this point in the history
  • Loading branch information
zerious committed Jun 3, 2014
1 parent cd04ca4 commit 83c827c
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 110 deletions.
20 changes: 20 additions & 0 deletions commands/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env node
var shellify = require('shellify');

shellify({
root: require.resolve('lighter').replace(/\/lighter\.js$/, '/'),
commands: {
dev: {
note: 'Starts the app in development mode',
options: {
},
alias: 'd'
},
prod: {
note: 'Starts the app in production mode',
options: {
},
alias: 'p'
}
}
});
3 changes: 3 additions & 0 deletions commands/dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function () {
require('./start')('dev');
};
3 changes: 3 additions & 0 deletions commands/prod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function () {
require('./start')('prod');
};
29 changes: 29 additions & 0 deletions commands/start.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var shellify = require('shellify');
var spawn = require('child_process').spawn;

module.exports = function (env) {

/**
* Start a Lighter app.
*/
function start() {

// Keep track of our start time.
var started = new Date();

// Spawn a child process and make it output to stdout.
var child = spawn(process.execPath, ['app'], {env: {NODE_ENV: env}});
child.stderr.pipe(process.stdout);
child.stdout.pipe(process.stdout);

// Restart once a second at most.
child.on('close', function () {
var elapsed = new Date() - started;
delay = Math.max(1e3 - elapsed, 0);
setTimeout(start, delay);
});
}

start();

};
116 changes: 78 additions & 38 deletions lib/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ var Class = require('./Class');
// Decorate http request and response prototypes.
require('./http');

var camel = function (string) {
return string[0].toUpperCase() + string.substr(1);
};

/**
* An App is an application that uses the lighter framework.
*
Expand Down Expand Up @@ -60,7 +64,9 @@ module.exports = Class.extend({
logger: null,
logLevel: (env == 'prod' ? 'info' : (env == 'dev' ? 'debug' : 'trace')),
asciiArt: null,
cullEnv: env,
enableChug: true,
enableD6: true,
enableBeams: true,
enableSplode: true,
enableCluster: (env != 'dev'),
Expand Down Expand Up @@ -232,62 +238,58 @@ module.exports = Class.extend({

// Za allows us to inject the app, but Express doesn't.
if (app.server.setApp) {
app.server.setApp(app);
app.server.setApp(app);
}
},

chugBeams: function chugBeams() {
var app = this;
app.beams = require('beams');
app.beams.setServer(app.server);

var allScripts = app.config.scripts['/all.js'] = app.config.scripts['/all.js'] || [];

// In dev mode, notify clients that we have restarted.
if (app.config.env == 'dev') {
allScripts.unshift('node_modules/lighter/scripts/lighter-beams.js');
app.chug.onReady(function () {
app.beams.emit('chug:change', 'ready');
});
}
allScripts.unshift('node_modules/lighter/node_modules/beams/scripts/beams-jymin.js');
allScripts.unshift('node_modules/lighter/node_modules/jymin/jymin.js');
},

initChug: function initChug() {
var app = this;
app.autoScripts = [];

// Pass the lighter server to Chug so it can route static assets.
app.chug = require('chug');
app.chug.setServer(app.server);
app.chug.enableShrinking();

if (app.config.enableBeams) {
app.chugBeams();
}

['models', 'controllers'].forEach(function (key) {
var Parent = require('./' + camel(key).substr(0, key.length - 1));
//log(Parent.extend);
var load = app[key] = app.chug(app.config[key])
.require(function (Class) {
if (!Class.extend) {
Class = this.module = Parent.extend(Class);
}
Class.instance = new Class(app, this);
});
app.logWhenLoadIsFinished(load, key);
});

app.publics = app.chug(app.config.publics).compile().route().watch();
app.logWhenLoadIsFinished(app.publics, "public files");
['publics', 'views'].forEach(function (key) {
var load = app[key] = app.chug(app.config[key])
load.cull('env', app.config.cullEnv);
load[key == 'publics' ? 'route' : 'compile']().watch();
app.logWhenLoadIsFinished(app[key], key);
});

if (app.config.enableBeams) {
app.initBeams();
}
if (app.config.enableD6) {
app.initD6();
}

app.views = app.chug(app.config.views).compile().watch();
app.logWhenLoadIsFinished(app.views, "views");
var configScripts = app.config.scripts;
var allScripts = configScripts['/all.js'] = configScripts['/all.js'] || [];
allScripts.unshift.apply(allScripts, app.autoScripts);

['scripts', 'styles'].forEach(function (key) {
var resources = app.config[key];
var loads = app[key] = {};
for (var url in resources) {
var files = resources[url];
var load = app.chug(files).compile().watch();
loads[url] = load.concat(url).route();
app.logWhenLoadIsFinished(load, key, url);
loads[url] = load.concat(url).cull('env', app.config.cullEnv).route();
app.logWhenLoadIsFinished(loads[url], key, url);
}
});

Expand All @@ -313,19 +315,20 @@ module.exports = Class.extend({

if (app.config.env == 'dev') {

// Watch server directories that aren't already being watched.
// Watch server directories that aren't already being watched or ignored.
app.watchAndExit('', /^(\..*|controllers|coverage|node_modules|public|scripts|styles|test|views)$/);

// Watch lighter for now, for framework development.
app.watchAndExit('/node_modules/lighter', /^node_modules$/);
// Watch lighter and d6 for now, for framework development.
app.watchAndExit('node_modules/lighter', /^node_modules$/);
app.watchAndExit('node_modules/lighter/node_modules/d6', /^node_modules$/);

}
else {
app.logger.warn("Minifying assets... " + grey + "(to disable, run \"NODE_ENV=dev node app\")" + base);
else if (app.config.env != 'test') {
app.logger.warn("Minifying assets... " + grey + "(to disable, run \"lighter dev\")" + base);
app.views.minify();
[app.scripts, app.styles].forEach(function (loads) {
for (var url in loads) {
loads[url].minify();
loads[url].wrap().minify().gzip().route();
}
});
app.chug.onceReady(function () {
Expand All @@ -350,9 +353,47 @@ module.exports = Class.extend({
}
},

initBeams: function chugBeams() {
var app = this;
app.initJymin();

app.beams = require('beams');
app.beams.setServer(app.server);

app.autoScripts.push('node_modules/lighter/node_modules/beams/scripts/beams-jymin.js');

// In dev mode, notify clients that we have restarted.
if (app.config.env == 'dev') {
app.autoScripts.push('node_modules/lighter/scripts/lighter-beams.js');
app.chug.onReady(function () {
app.beams.emit('chug:change', 'ready');
});
}
},

initD6: function () {
var app = this;
app.initJymin();
app.autoScripts.push('node_modules/lighter/node_modules/d6/scripts/d6-jymin.js');

// Require d6 later so minification can begin before it uses chug.onReady.
setImmediate(function () {
require('d6')(app);
});
},

initJymin: function () {
var app = this;
if (!app.config.hasJymin) {
app.autoScripts.push('node_modules/lighter/node_modules/jymin/jymin.js');
app.config.hasJymin = true;
}
},


logWhenLoadIsFinished: function logWhenLoadIsFinished(load, type, url) {
var app = this;
type = type[0].toUpperCase() + type.substr(1);
type = camel(type);
load
.onceReady(function () {
app.logger.info(type + " " + (url ? 'routed to "' + url + '"' : "loaded") + ". " + grey + "x" + load.assets.length + base);
Expand All @@ -372,8 +413,7 @@ module.exports = Class.extend({
watchCount++;
if (watchCount < 2e2) {
fs.watch(dir, function () {
app.logger.info("Exiting due to core file change.");
app.logger.info("To run indefinitely, use:\n " + '"while true; do NODE_ENV=dev node server; done"');
app.logger.warn("Exiting due to backend file change.");
process.exit();
});
}
Expand Down
30 changes: 1 addition & 29 deletions lib/Controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,6 @@ module.exports = Class.extend({
var controller = this;
for (var property in controller) {
(function (action) {

function iterate_middleware (cur, others, final, args) {

if (cur === final ) {
return final.apply(controller, args);
}

cur.call(controller, args[0], args[1], function next (err) {
if (err) {
throw err;
}
iterate_middleware(others[0], others.slice(1), final, args);
});
}

var allActions;

if (Array.isArray(action)) {
allActions = action;
action = action[action.length-1];
} else {
allActions = [action];
}

if (typeof action == 'function') {
if (/(GET|PUT|POST|DELETE)/.test(action.name)) {
property = (property == 'index' ? '' : '/' + property);
Expand All @@ -57,11 +33,7 @@ module.exports = Class.extend({
methods.forEach(function (method) {
method = method.toLowerCase();
controller.app.server[method](url, function () {

// action.apply(controller, arguments);

// Middleware support on controller level
iterate_middleware(allActions[0], allActions.slice(1), action, arguments);
action.apply(controller, arguments);
});
});
}
Expand Down
Loading

0 comments on commit 83c827c

Please sign in to comment.