diff --git a/lib/config-loader.js b/lib/config-loader.js index c2ad540..f21e698 100644 --- a/lib/config-loader.js +++ b/lib/config-loader.js @@ -5,6 +5,7 @@ var fs = require('fs') 'public': true, 'data': true, 'resources': true, + 'node_modules': true, '.dpd': true } , sh = require('shelljs'); @@ -38,16 +39,16 @@ module.exports.loadConfig = function(basepath, fn) { } } - fs.readdir(basepath, function (err, dir) { + fs.readdir(basepath + 'resources', function (err, dir) { if(dir && dir.length) { dir.forEach(function (file) { if(!ignore[file]) { remaining++; - fs.stat(basepath + '/' + file, function (err, stat) { + fs.stat(basepath + 'resources/' + file, function (err, stat) { remaining--; error = err; if(stat && stat.isDirectory()) { - var spath = basepath + '/' + file + '/' + 'settings.json' + var spath = basepath + 'resources/' + file + '/' + 'settings.json' , resource = file; fs.exists(spath, function (exists) { @@ -75,7 +76,7 @@ module.exports.loadConfig = function(basepath, fn) { }); remaining++ - fs.readdir(basepath + '/' + file, function (err, dir) { + fs.readdir(basepath + '/resources/' + file, function (err, dir) { remaining--; if(err) error = err; @@ -83,7 +84,7 @@ module.exports.loadConfig = function(basepath, fn) { dir.forEach(function (f) { if(path.extname(f) == '.js') { remaining++; - fs.readFile(basepath + '/' + file + '/' + f, 'utf-8', function(err, data) { + fs.readFile(basepath + '/resources/' + file + '/' + f, 'utf-8', function(err, data) { remaining--; var ev = f.replace('.js', ''); ev = ev.substr(0, 1).toUpperCase() + ev.substr(1); diff --git a/lib/resource.js b/lib/resource.js index 48e35ea..d0f309b 100644 --- a/lib/resource.js +++ b/lib/resource.js @@ -118,4 +118,10 @@ Resource.toJSON = function() { }; } +/*! + * resource tag, for duck typing + */ + +Resource.prototype.__resource__ = true; + module.exports = Resource; \ No newline at end of file diff --git a/lib/resources.js b/lib/resources.js index ae21539..2534f79 100644 --- a/lib/resources.js +++ b/lib/resources.js @@ -42,7 +42,7 @@ var build = exports.build = function (resourceConfig, server, fn) { return; } - loadTypes(function(defaults, types) { + loadTypes(function(defaults, types) { Object.keys(types).forEach(function(key) { defaults[key] = types[key]; }); diff --git a/lib/type-loader.js b/lib/type-loader.js index 9bc996a..e1d956f 100644 --- a/lib/type-loader.js +++ b/lib/type-loader.js @@ -1,4 +1,5 @@ -var fs = require('fs'); +var fs = require('fs') + , Resource = require('./resource'); module.exports = function loadTypes(basepath, fn) { var types = {} @@ -9,23 +10,28 @@ module.exports = function loadTypes(basepath, fn) { basepath = undefined; } - var path = basepath || './resources'; + var path = basepath || '.'; // read default lib resources fs.readdir(__dirname + '/resources', function(err, dir) { dir.forEach(function(file) { if(file.indexOf('.js') == file.length - 3 || file.indexOf('.') === -1) { - var c = require(path + '/' + file); + var c = require(__dirname + '/resources/' + file); defaults[c.name] = c; } }); // read local project resources - fs.readdir(path, function(err, dir) { + fs.readdir(path + '/node_modules', function(err, dir) { dir && dir.forEach(function(file) { if(file.indexOf('.js') == file.length - 3 || file.indexOf('.') === -1) { - var c = require(require('path').resolve(path) + '/' + file); - types[c.name] = c; + + try { + var c = require(require('path').resolve(path) + '/node_modules/' + file); + if(c.prototype.__resource__) { + types[c.name] = c; + } + } catch(e) {} } }); diff --git a/test-app/empty/settings.json b/test-app/resources/empty/settings.json similarity index 100% rename from test-app/empty/settings.json rename to test-app/resources/empty/settings.json diff --git a/test-app/full/settings.json b/test-app/resources/full/settings.json similarity index 100% rename from test-app/full/settings.json rename to test-app/resources/full/settings.json diff --git a/test-app/resources/hello.js b/test-app/resources/hello.js deleted file mode 100644 index bd5df42..0000000 --- a/test-app/resources/hello.js +++ /dev/null @@ -1,14 +0,0 @@ -var Resource = require('deployd/lib/resource') - , util = require('util'); - -function Hello(settings) { - Resource.apply(this, arguments); -} -util.inherits(Hello, Resource); -module.exports = Hello; - -Hello.prototype.handle = function (ctx, next) { - if(ctx.req && ctx.req.method !== 'GET') return next(); - - ctx.done(null, {hello: 'world'}); -} \ No newline at end of file diff --git a/test-app/hello/settings.json b/test-app/resources/hello/settings.json similarity index 100% rename from test-app/hello/settings.json rename to test-app/resources/hello/settings.json diff --git a/test-app/resources/proxy/index.js b/test-app/resources/proxy/index.js deleted file mode 100644 index a6b7714..0000000 --- a/test-app/resources/proxy/index.js +++ /dev/null @@ -1,30 +0,0 @@ -/** - * example custom resource - */ - -var Resource = require('deployd/lib/resource') - , util = require('util') - , request = require('request'); - -function Proxy(settings) { - Resource.apply(this, arguments); - this.remote = settings.remote; -} -util.inherits(Proxy, Resource); -module.exports = Proxy; - -Proxy.prototype.handle = function (ctx, next) { - if(ctx.req && ctx.req.method !== 'GET') return next(); - request.get(this.remote + ctx.url).pipe(ctx.res); -} - -Proxy.label = 'HTTP Proxy'; -Proxy.defaultPath = '/proxy'; - -Proxy.basicDashboard = { - settings: [{ - name: "remote" - , type: "text" //"textarea" or "number" works as well - , description: "The remote server to proxy to." - }] -} \ No newline at end of file diff --git a/test-app/proxy/settings.json b/test-app/resources/proxy/settings.json similarity index 100% rename from test-app/proxy/settings.json rename to test-app/resources/proxy/settings.json diff --git a/test-app/recursive/get.js b/test-app/resources/recursive/get.js similarity index 100% rename from test-app/recursive/get.js rename to test-app/resources/recursive/get.js diff --git a/test-app/recursive/settings.json b/test-app/resources/recursive/settings.json similarity index 100% rename from test-app/recursive/settings.json rename to test-app/resources/recursive/settings.json diff --git a/test-app/resources/templates/index.js b/test-app/resources/templates/index.js deleted file mode 100644 index a92a500..0000000 --- a/test-app/resources/templates/index.js +++ /dev/null @@ -1,25 +0,0 @@ -/** - * example custom resource - */ - -var Resource = require('deployd/lib/resource') - , util = require('util') - , request = require('request') - , ejs = require('ejs') - , fs = require('fs'); - -function Templates(settings) { - this.path = settings.path; - Resource.apply(this, arguments); -} -util.inherits(Templates, Resource); -module.exports = Templates; - -Templates.prototype.handle = function (ctx, next) { - var out = ejs.render(fs.readFileSync(this.path.replace('/', '') + ctx.url + '.ejs').toString(), {foo: 'bar'}); - - ctx.res.end(out); -} - -Templates.label = 'EJS Templates'; -Templates.defaultPath = '/views'; \ No newline at end of file diff --git a/test-app/todos/get.js b/test-app/resources/todos/get.js similarity index 100% rename from test-app/todos/get.js rename to test-app/resources/todos/get.js diff --git a/test-app/todos/post.js b/test-app/resources/todos/post.js similarity index 100% rename from test-app/todos/post.js rename to test-app/resources/todos/post.js diff --git a/test-app/todos/put.js b/test-app/resources/todos/put.js similarity index 100% rename from test-app/todos/put.js rename to test-app/resources/todos/put.js diff --git a/test-app/todos/settings.json b/test-app/resources/todos/settings.json similarity index 100% rename from test-app/todos/settings.json rename to test-app/resources/todos/settings.json diff --git a/test-app/todos/validate.js b/test-app/resources/todos/validate.js similarity index 100% rename from test-app/todos/validate.js rename to test-app/resources/todos/validate.js diff --git a/test-app/users/get.js b/test-app/resources/users/get.js similarity index 100% rename from test-app/users/get.js rename to test-app/resources/users/get.js diff --git a/test-app/users/settings.json b/test-app/resources/users/settings.json similarity index 100% rename from test-app/users/settings.json rename to test-app/resources/users/settings.json diff --git a/test-app/views/settings.json b/test-app/resources/views/settings.json similarity index 100% rename from test-app/views/settings.json rename to test-app/resources/views/settings.json diff --git a/test-app/views/test.ejs b/test-app/resources/views/test.ejs similarity index 100% rename from test-app/views/test.ejs rename to test-app/resources/views/test.ejs