Skip to content

Commit

Permalink
AddedbasePath as options to allow plugin to correctly resolve rute …
Browse files Browse the repository at this point in the history
…source files
  • Loading branch information
rajatkumar committed Nov 15, 2016
1 parent ef2c238 commit 465c150
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 22 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ when done.
* `opts`: The options object containing
* `opts.server` The restify server to install the routes on to.
* `[opts.config]` The POJO of the enroute config.
* `[opts.basePath]` Used with `[opts.config]`. The POJO requires a
`basePath` to correctly resolve the route source file(s).
* `[opts.configPath]` The path to the enroute config on disk.
* `cb` The callback. Returns `Error` if there's an error installing the routes.

Expand Down Expand Up @@ -103,7 +105,8 @@ const server = restify.createServer();
// install routes with enroute
enroute.install({
config: CONFIG,
server: server
server: server,
basePath: __dirname
}, function (err) {
if (err) {
console.error('unable to install routes');
Expand Down Expand Up @@ -153,7 +156,8 @@ const CONFIG = {
const server = restify.createServer();
// install routes with enroute
enroute.validate({
config: CONFIG
config: CONFIG,
basePath: __dirname
}, function (err) {
if (err) {
console.error('unable to install routes');
Expand Down
8 changes: 5 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,20 @@ module.exports = {
install: function (opts, cb) {
assert.object(opts, 'opts');
assert.func(cb, 'cb');
// asserts of other inputs done by parse and installRoutes repectively
// asserts of other inputs done by parse and installRoutes respectively
vasync.pipeline({arg: {}, funcs: [
function parse(ctx, _cb) {
parser.parse(opts, function (err, config) {
ctx.config = config;
ctx.config = config.validatedConfig;
ctx.basePath = config.basePath;
return _cb(err);
});
},
function installRoutes(ctx, _cb) {
install({
enroute: ctx.config,
server: opts.server
server: opts.server,
basePath: ctx.basePath
}, function (err) {
return _cb(err);
});
Expand Down
8 changes: 7 additions & 1 deletion lib/install.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var fs = require('fs');
var path = require('path');

var _ = require('lodash');
var assert = require('assert-plus');
Expand All @@ -13,13 +14,15 @@ var verror = require('verror');
* @param {object} opts Options object.
* @param {object} opts.enroute The parsed enroute configuration.
* @param {object} opts.server The restify server.
* @param {string} opts.basePath The basepath to resolve source files.
* @param {function} cb The callback.
* @returns {undefined}
*/
function install(opts, cb) {
assert.object(opts, 'opts');
assert.object(opts.enroute, 'opts.enroute');
assert.object(opts.server, 'opts.server');
assert.string(opts.basePath, 'opts.basePath');

vasync.pipeline({arg: {}, funcs: [
// Read the routes from disk and parse them as functions
Expand All @@ -35,7 +38,10 @@ function install(opts, cb) {
// go through each of the HTTP methods
_.forEach(methods, function (src, method) {
barrier.start(routeName + method);
fs.readFile(src.source,
// resolve to the correct file
var sourceFile = path.resolve(opts.basePath, src.source);
// use the resolved file path
fs.readFile(sourceFile,
{encoding: 'utf8'},
function (err, data) {
if (err) {
Expand Down
17 changes: 16 additions & 1 deletion lib/parser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var fs = require('fs');
var path = require('path');

var _ = require('lodash');
var assert = require('assert-plus');
Expand Down Expand Up @@ -30,13 +31,16 @@ module.exports = {
* @param {object} opts The options object
* @param {string} [opts.config] The POJO of the config you want to validate.
* @param {string} [opts.configPath] The path to the config on disk to validate.
* @param {string} [opts.basePath] The basepath is used to correctly resolve
* route source files.
* @param {function} cb The callback f(err, validatedConfig)
*
* @returns {function} The callback
*/
function parse(opts, cb) {
assert.object(opts, 'opts');
assert.optionalString(opts.configPath, 'opts.configPath');
assert.optionalString(opts.basePath, 'opts.basePath');
assert.optionalObject(opts.config, 'opts.config');
assert.func(cb, 'cb');

Expand All @@ -46,14 +50,22 @@ function parse(opts, cb) {
'opts.config');
}

if (!opts.configPath && !opts.basePath) {
throw new verror.VError('must specify opts.basePath');
}

var validatedConfig;
var basePath;

vasync.pipeline({arg: {}, funcs: [
function readInput(it, cb1) {
if (opts.config) {
it.config = opts.config;
basePath = opts.basePath;
cb1();
} else {
// set basePath from `configPath` opts
it.basePath = path.dirname(opts.configPath);
fs.readFile(opts.configPath, 'utf8', function (err, config) {
if (err) {
cb1(new verror.VError({
Expand Down Expand Up @@ -112,7 +124,10 @@ function parse(opts, cb) {
});
}
]}, function (err, res) {
return cb(err, validatedConfig);
return cb(err, {
validatedConfig: validatedConfig,
basePath: basePath
});
});
}

Expand Down
12 changes: 9 additions & 3 deletions test/install.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var path = require('path');

var _ = require('lodash');
var assert = require('chai').assert;
var restify = require('restify');
Expand All @@ -10,6 +12,7 @@ var enroute = require('../lib/index');

var HOST = 'localhost' || process.env.HOST;
var PORT = 1337 || process.env.PORT;
var BASEPATH = path.join(__dirname, '..');

var CONFIG = {
schemaVersion: 1,
Expand Down Expand Up @@ -78,7 +81,8 @@ describe('enroute-install', function () {
it('should install routes', function (done) {
enroute.install({
config: CONFIG,
server: SERVER
server: SERVER,
basePath: BASEPATH
}, function (err) {
assert.ifError(err);
assertServer({}, done);
Expand All @@ -92,7 +96,8 @@ describe('enroute-install', function () {
get: 'source does not exist'
}
},
server: SERVER
server: SERVER,
basePath: BASEPATH
}, function (err) {
assert.isOk(err);
return done();
Expand All @@ -106,7 +111,8 @@ describe('enroute-install', function () {
get: './test/etc/notAFunction.js'
}
},
server: SERVER
server: SERVER,
basePath: BASEPATH
}, function (err) {
assert.isOk(err);
return done();
Expand Down
42 changes: 30 additions & 12 deletions test/parser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var path = require('path');

var _ = require('lodash');
var assert = require('chai').assert;
var uuid = require('uuid');
Expand All @@ -8,6 +10,8 @@ var enroute = require('../lib');

var CONFIG_PATH = './test/etc/enroute.json';
var BAD_CONFIG_PATH = './test/etc/badEnroute.json';
var BASEPATH = path.join(__dirname, '..');

var CONFIG = {
schemaVersion: 1,
routes: {
Expand Down Expand Up @@ -80,10 +84,14 @@ describe('enroute-parser', function () {
it('should parse config from string', function (done) {
enroute.validate({
version: 1,
config: CONFIG
config: CONFIG,
basePath: BASEPATH
}, function (err, config) {
assert.ifError(err);
assert.deepEqual(CONFIG, config);
assert.deepEqual(config, {
validatedConfig: CONFIG,
basePath: BASEPATH
} );
return done();
});
});
Expand Down Expand Up @@ -111,7 +119,8 @@ describe('enroute-parser', function () {
delete config.schemaVersion;

enroute.validate({
config: config
config: config,
basePath: BASEPATH
}, function (err) {
assert.isOk(err);
return done();
Expand All @@ -123,7 +132,8 @@ describe('enroute-parser', function () {
config.schemaVersion = '1';

enroute.validate({
config: config
config: config,
basePath: BASEPATH
}, function (err) {
assert.isOk(err);
return done();
Expand All @@ -135,7 +145,8 @@ describe('enroute-parser', function () {
config.schemaVersion = 2;

enroute.validate({
config: config
config: config,
basePath: BASEPATH
}, function (err) {
assert.isOk(err);
return done();
Expand All @@ -147,7 +158,8 @@ describe('enroute-parser', function () {
delete config.routes;

enroute.validate({
config: config
config: config,
basePath: BASEPATH
}, function (err) {
assert.isOk(err);
return done();
Expand All @@ -159,7 +171,8 @@ describe('enroute-parser', function () {
config.routes = {};

enroute.validate({
config: config
config: config,
basePath: BASEPATH
}, function (err) {
assert.isOk(err);
return done();
Expand All @@ -173,7 +186,8 @@ describe('enroute-parser', function () {
};

enroute.validate({
config: config
config: config,
basePath: BASEPATH
}, function (err) {
assert.isOk(err);
return done();
Expand All @@ -185,7 +199,8 @@ describe('enroute-parser', function () {
config.foo = 'not-an-object';

enroute.validate({
config: config
config: config,
basePath: BASEPATH
}, function (err) {
assert.isOk(err);
return done();
Expand All @@ -200,7 +215,8 @@ describe('enroute-parser', function () {
};

enroute.validate({
config: config
config: config,
basePath: BASEPATH
}, function (err) {
assert.isOk(err);
return done();
Expand All @@ -213,7 +229,8 @@ describe('enroute-parser', function () {
config.routes.foo.post = {};

enroute.validate({
config: config
config: config,
basePath: BASEPATH
}, function (err) {
assert.isOk(err);
return done();
Expand All @@ -226,7 +243,8 @@ describe('enroute-parser', function () {
config.routes.foo.post.foo = 'foo';

enroute.validate({
config: config
config: config,
basePath: BASEPATH
}, function (err) {
assert.isOk(err);
return done();
Expand Down

0 comments on commit 465c150

Please sign in to comment.