Added basic dpd.js code generation

This commit is contained in:
DallonF
2012-06-07 12:53:31 -07:00
parent 3ae2c56bfc
commit 3b1eaded4c
4 changed files with 43 additions and 4 deletions

View File

@@ -90,6 +90,12 @@ Resource.prototype.handle = function (ctx, next) {
ctx.end();
}
/**
* If true, generates utility methods for this resource in dpd.js
*/
Resource.prototype.clientGeneration = false;
Resource.toJSON = function() {
return {
type: this.name,

View File

@@ -36,7 +36,7 @@ var build = exports.build = function (resourceConfig, server, fn) {
});
resources.push(new Files({path: '/', public: './public'}, server));
resources.push(new ClientLib({path: '/dpd.js'}, server));
resources.push(new ClientLib({path: '/dpd.js', resources: resources}, server));
resources.push(new InternalResources({path: '/__resources'}, server));
resources.push(new Dashboard({path: '/dashboard'}, server));

View File

@@ -1,7 +1,7 @@
var Resource = require('../resource')
, util = require('util')
, path = require('path')
, filed = require('filed');
, fs = require('fs');
function ClientLib(settings) {
Resource.apply(this, arguments);
@@ -9,12 +9,43 @@ function ClientLib(settings) {
util.inherits(ClientLib, Resource);
ClientLib.prototype.handle = function (ctx, next) {
var resource = this;
if (ctx.url === '/') {
var f = filed(path.join(__dirname, '../../clib/dpd.js'));
f.pipe(ctx.res);
var f = fs.createReadStream(path.join(__dirname, '../../clib/dpd.js'));
f.on('data', function(d) {
ctx.res.write(d);
});
f.on('end', function() {
resource.generate(ctx.res, function() {
ctx.res.end();
});
});
f.on('error', function(err) {
ctx.done(err);
});
} else {
next();
}
}
ClientLib.prototype.generate = function(res, fn) {
var clientLib = this;
res.write('\n\n //Automatically generated code\n\n');
this.settings.resources.forEach(function(r) {
clientLib.generateResource(r, res);
});
fn();
};
ClientLib.prototype.generateResource = function(r, res) {
var jsName = r.settings.path.replace(/[^A-Za-z0-9]/g, '');
if (r.clientGeneration && jsName) {
res.write('dpd.' + jsName + ' = dpd("' + r.settings.path + '");\n')
}
};
module.exports = ClientLib;

View File

@@ -355,4 +355,6 @@ Collection.prototype.changed = function(ctx, fn) {
}
}
Collection.prototype.clientGeneration = true;
module.exports = Collection;