Skip to content

Commit

Permalink
Merge pull request #24 from sitraka-hq/dev
Browse files Browse the repository at this point in the history
Implement enhancement for #20
  • Loading branch information
sratsimba authored Jan 6, 2020
2 parents 109943c + 571da6c commit 8870434
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 14 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p

### 3.0.4

- Fix unpublished version
- Fix unpublished version

### 3.1.0

- `routes_dir` accepts an array of parameters.
- Updates documentation.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[![NPM](https://nodei.co/npm/hapi-auto-route.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/hapi-auto-route/)

hapi-auto-route is a hapi plugin that lets you load route objects automatically from a directory. And allow routes path to be prefixed.
hapi-auto-route is a hapi plugin that lets you load route objects automatically by specifying the root directory/directories containing the routes. And allow routes path to be prefixed.

Maintainer: [Sitraka Ratsimba](https://github.com/sitraka-hq)

Expand Down Expand Up @@ -80,10 +80,12 @@ init();

Now, you can start the server and see `Hello` at `http://localhost:3000`.

You can also provide an array of absolute paths if you want to auto-import from multiple base directories.


## API

- `routes_dir`: absolute path to routes directory. `required`
- `routes_dir`: absolute path(s) to routes directory/directories. `required`
- `pattern`: glob pattern used to find route files. Defaults to `**/!(_)*.js`.
- `use_prefix`: Use directory tree as prefix. Defaults to `false`.

Expand Down
32 changes: 27 additions & 5 deletions lib/auto-route.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,39 @@ const Prefix = require('./prefix');

module.exports.getFiles = (baseDir, pattern) => {

const absolutePattern = Path.join( baseDir, pattern);
if (Array.isArray(baseDir)) {
const promise = new Promise((resolve, reject) => {

Promise.all(baseDir.map((d) => this.getFiles(d, pattern)))
.then((value) => resolve(Hoek.flatten(value)));
});
return promise;
}

const absolutePattern = Path.join(baseDir, pattern);
const glob = Util.promisify(Glob);
return glob(absolutePattern, {});
};

module.exports.getRoutes = (files) => {
module.exports.getRoutes = (filepaths) => {

return files.map((file) => {
const routeSchema = Joi.object().keys({
method: Joi.string().required(),
path: Joi.string().required()
// Technically handler is required as well, either in the method or in its options, but that gets sticky with plugins.
}).xor('handler', 'options').unknown(true);

const multipleRoutesAllowed = Joi.alternatives(routeSchema, Joi.array().items(routeSchema));

return Hoek.clone(require(Path.resolve(file)));
const files = filepaths.map((p) => require(Path.resolve(p)));

const routes = files.filter((file) => {

const { error } = Joi.validate(file, multipleRoutesAllowed);
return error === null;
});

return routes.map((file) => Hoek.clone(file));
};

module.exports.getPrefixes = (files, baseDir) => {
Expand Down Expand Up @@ -67,7 +89,7 @@ module.exports.updatePaths = (prefixes, routes, stripTrailingSlash) => {
module.exports.validateOptions = (options) => {

const optionsSchema = Joi.object().keys({
routes_dir: Joi.string().required(),
routes_dir: Joi.alternatives(Joi.string(), Joi.array().items(Joi.string())).required(),
pattern: Joi.string().default('**/!(_)*.js'),
use_prefix: Joi.boolean().default(false)
});
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hapi-auto-route",
"version": "3.0.4",
"version": "3.1.0",
"description": "Autoloads hapi routes",
"author": "Sitraka Ratsimba <ratsimbasitraka@gmail.com>",
"main": "lib/index.js",
Expand All @@ -25,6 +25,7 @@
"devDependencies": {
"@hapi/code": "6.x.x",
"@hapi/hapi": "18.x.x",
"@hapi/eslint-plugin-hapi": "4.x.x",
"@hapi/lab": "20.x.x"
},
"dependencies": {
Expand Down
24 changes: 19 additions & 5 deletions test/auto-route-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,21 @@ lab.describe('AutoRoute', () => {

lab.describe('loadFiles', () => {

lab.it('load files in an array', () => {
lab.it('loads files in an array when given a single base directory', () => {

AutoRoute.getFiles(routeDir, '**/*.js').then((files) => {

expect(files.length).to.be.above(0);
});
});

lab.it('loads files in an array when given an array of base directories', () => {

AutoRoute.getFiles([routeDir, `${routeDir}2`], '**/*.js').then((files) => {

expect(files.length).to.be.above(0);
});
});
});

lab.describe('getRoutes', () => {
Expand Down Expand Up @@ -119,10 +127,16 @@ lab.describe('AutoRoute', () => {

const routes_dir = Path.resolve(__dirname, 'fixtures/routes');

const validOptions = AutoRoute.validateOptions({ routes_dir });
expect(validOptions.routes_dir).to.be.equal(routes_dir);
expect(validOptions.pattern).to.be.equal('**/!(_)*.js');
expect(validOptions.use_prefix).to.be.equal(false);
const validOptionsForSingleDir = AutoRoute.validateOptions({ routes_dir });
expect(validOptionsForSingleDir.routes_dir).to.be.equal(routes_dir);
expect(validOptionsForSingleDir.pattern).to.be.equal('**/!(_)*.js');
expect(validOptionsForSingleDir.use_prefix).to.be.equal(false);

const multi_routes_dir = [routes_dir, `${routes_dir}2`];
const validOptionsForMultiDir = AutoRoute.validateOptions({ routes_dir: multi_routes_dir });
expect(validOptionsForMultiDir.routes_dir).to.be.equal(multi_routes_dir);
expect(validOptionsForMultiDir.pattern).to.be.equal('**/!(_)*.js');
expect(validOptionsForMultiDir.use_prefix).to.be.equal(false);

});

Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/routes2/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = {
'method': 'GET',
'path': '/',
handler: (request, h) => 'index.js'
};
7 changes: 7 additions & 0 deletions test/fixtures/routes2/pages/deep/deep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = {
'method': 'GET',
'path': '/deep',
handler: (request, h) => 'index.js'
};
11 changes: 11 additions & 0 deletions test/fixtures/routes2/pages/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

module.exports = [{
'method': 'GET',
'path': '/page1',
handler: (request, h) => 'index.js'
}, {
'method': 'GET',
'path': '/page2',
handler: (request, h) => 'index.js'
}];
7 changes: 7 additions & 0 deletions test/fixtures/routes2/pages/pages1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = {
'method': 'GET',
'path': '/p1.view',
handler: (request, h) => 'index.js'
};

0 comments on commit 8870434

Please sign in to comment.