mirror of
https://github.com/zhigang1992/deployd.git
synced 2026-05-25 16:11:26 +08:00
Added basic dpd.js code generation
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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));
|
||||
|
||||
|
||||
@@ -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;
|
||||
@@ -355,4 +355,6 @@ Collection.prototype.changed = function(ctx, fn) {
|
||||
}
|
||||
}
|
||||
|
||||
Collection.prototype.clientGeneration = true;
|
||||
|
||||
module.exports = Collection;
|
||||
Reference in New Issue
Block a user