Skip to content

Commit

Permalink
feat: add autoPrefix and useFiles options
Browse files Browse the repository at this point in the history
  • Loading branch information
menems committed Dec 22, 2015
1 parent 9d6ab6f commit fe8726d
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ npm install koa-66-aggregate
```
## Options
- `path` : path of routes directories
- `useFiles`: use all file as auto prefix (default: false)
- `autoPrefix`: add dir/file as prefix (default: true)
- `plugins` : object of plugins available on each router instance
- `globs` : default : ['**/index.js']

Expand Down
24 changes: 20 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const path = require('path');
const Router = require('koa-66');
const walk = require('walk-sync');

module.exports = options => {
module.exports = function(options) {

if (!options)
throw new Error('options is required');
Expand All @@ -26,6 +26,12 @@ module.exports = options => {

options.globs = options.globs || ['**/index.js'];

if ( options.useFiles)
options.globs = ['**/*.js'];

// add more options.
options.autoPrefix = options.autoPrefix || true;

const router = new Router();

if (options.plugins) {
Expand All @@ -35,11 +41,21 @@ module.exports = options => {
}

walk(options.path, {globs: options.globs}).forEach( m => {
const abs = path.dirname(path.join(options.path,m));
const route = abs.split(options.path).pop() || '/';
const _router = require(abs);
const _path = path.join(options.path, m);
const abs = path.dirname(_path);
let route = options.autoPrefix ? abs.split(options.path).pop() || '/' : '/';

if ( options.useFiles && options.autoPrefix) {
const fileName = path.basename(m, '.js');
if ( fileName !== 'index') {
route = route + '/' + fileName;
}
}

const _router = require(_path);
router.mount(route , _router);
debug('mount on %s', route);

});

return router;
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/routes2/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const Router = require('koa-66');
const router = module.exports = new Router();

router.get(ctx => ctx.body = 'index');

4 changes: 4 additions & 0 deletions test/fixtures/routes2/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const Router = require('koa-66');
const router = module.exports = new Router();

router.get( ctx => ctx.body = 'test/index');
4 changes: 4 additions & 0 deletions test/fixtures/routes2/test/toto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const Router = require('koa-66');
const router = module.exports = new Router();

router.get( ctx => ctx.body = 'test/toto');
5 changes: 5 additions & 0 deletions test/fixtures/routes2/v1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const Router = require('koa-66');
const router = module.exports = new Router();

router.get( ctx => ctx.body = 'v1');

32 changes: 32 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,38 @@ describe('koa-66-aggregate', () => {
done();
});

it('should work as expected with file', done => {
const routes = agg({path: _path + '/routes2', useFiles: true});
routes.should.be.type('object');
routes.should.have.property('stacks');
routes.stacks.should.have.length(4);

routes.stacks[0].should.have.property('path', '/');
routes.stacks[1].should.have.property('path', '/test');
routes.stacks[2].should.have.property('path', '/test/toto');
routes.stacks[3].should.have.property('path', '/v1');

const fn1 = routes.stacks[0].middleware;
const fn2 = routes.stacks[1].middleware;
const fn3 = routes.stacks[2].middleware;
const fn4 = routes.stacks[3].middleware;

let ctx1 = {};
fn1(ctx1);
ctx1.should.have.property('body', 'index');

let ctx2 = {};
fn2(ctx2);
ctx2.should.have.property('body', 'test/index');
let ctx3 = {};
fn3(ctx3);
ctx3.should.have.property('body', 'test/toto');
let ctx4 = {};
fn4(ctx4);
ctx4.should.have.property('body', 'v1');
done();
});

it('should attach plugin', done => {
const routes = agg({
path: _path + '/routes',
Expand Down

0 comments on commit fe8726d

Please sign in to comment.