Skip to content

Commit

Permalink
fix(utils): validation in loadFilesFromDir
Browse files Browse the repository at this point in the history
adding more validation for cwd.
  • Loading branch information
ar4mirez committed Feb 7, 2017
1 parent 904e000 commit b69b219
Show file tree
Hide file tree
Showing 9 changed files with 2,162 additions and 818 deletions.
729 changes: 0 additions & 729 deletions .coverage/coverage.html

This file was deleted.

23 changes: 0 additions & 23 deletions .coverage/lcov.info

This file was deleted.

3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
coverage/
dist/
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ node_js:
before_script:
- npm prune
after_success:
- npm run coverage:report
- npm run semantic-release
script:
- npm run test
- npm run coverage
- npm run coverage:check
- npm run build
branches:
except:
- /^v\d+\.\d+\.\d+$/
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# hapi-octopus

[![travis build](https://img.shields.io/travis/ar4mirez/hapi-octopus.svg?style=flat-square)](https://travis-ci.org/ar4mirez/hapi-octopus)
[![codecov coverage](https://img.shields.io/codecov/c/github/ar4mirez/hapi-octopus.svg?style=flat-square)](https://codecov.io/github/ar4mirez/hapi-octopus)
[![version](https://img.shields.io/npm/v/hapi-octopus.svg?style=flat-square)](http://npm.im/hapi-octopus)
[![downloads](https://img.shields.io/npm/dm/hapi-octopus.svg?style=flat-square)](http://npm-stat.com/charts.html?package=hapi-octopus&from=2015-08-01)
[![MIT License](https://img.shields.io/npm/l/hapi-octopus.svg?style=flat-square)](http://opensource.org/licenses/MIT)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat-square)](https://github.com/semantic-release/semantic-release)

HAPI plugin to auto-load routes, methods, pres, handlers and more.

## Installation

## Usage
17 changes: 11 additions & 6 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
'use strict';

const Glob = require('glob');
const Hoek = require('hoek');
const Joi = require('joi');
const _ = require('lodash');
const Fs = require('fs');

const internals = {
options: {}
options: Joi.object().keys({
cwd: Joi.string().required()
})
};

exports.loadFilesFromDir = (pattern, options) => {

const validation = Joi.validate(options, Joi.object().keys({
cwd: Joi.string()
}));
const validation = Joi.validate(options, internals.options);

if (validation.error) {
throw new Error(validation.error.annotate());
}

const files = Glob.sync(pattern, Hoek.applyToDefaults(internals.options, options));
if (!Fs.statSync(options.cwd).isDirectory()) {
throw new Error('options.cwd: must be a valid directory.');
}

const files = Glob.sync(pattern, options);

return _.map(files, (file) => (`${options.cwd}/${file}`));
};
76 changes: 48 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,49 +1,69 @@
{
"name": "hapi-octopus",
"version": "0.0.0-semantically-released",
"description": "HAPI plugin to auto-load routes, methods, pres, handlers and more.",
"main": "./lib/index.js",
"scripts": {
"test": "lab -a code -c -C -l -L -S -t 100",
"test:cov": "lab -r html -o .coverage/coverage.html -r lcov -o .coverage/lcov.info",
"commit": "git-cz",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"repository": {
"type": "git",
"url": "https://github.com/ar4mirez/hapi-octopus.git"
},
"keywords": [
"hapijs",
"plugin",
"routes",
"handlers",
"pres",
"methods"
],
"version": "0.0.0-semantically-released",
"author": "Angel Ramirez <[email protected]>",
"license": "SEE LICENSE IN <LICENSE.md>",
"bugs": {
"url": "https://github.com/ar4mirez/hapi-octopus/issues"
},
"homepage": "https://github.com/ar4mirez/hapi-octopus#readme",
"config": {
"ghooks": {
"pre-commit": "npm run validate"
},
"commitizen": {
"path": "node_modules/cz-conventional-changelog"
}
},
"dependencies": {
"async": "^2.1.4",
"glob": "^7.1.1",
"joi": "10.2.1"
},
"devDependencies": {
"code": "4.0.0",
"codecov": "^1.0.1",
"commitizen": "2.9.5",
"cz-conventional-changelog": "1.2.0",
"eslint": "^3.15.0",
"eslint-config-hapi": "^10.0.0",
"eslint-plugin-hapi": "^4.0.0",
"fixpack": "^2.3.1",
"ghooks": "^2.0.0",
"lab": "12.1.0",
"npm-run-all": "^4.0.1",
"rimraf": "^2.5.4",
"semantic-release": "6.3.2"
},
"config": {
"commitizen": {
"path": "node_modules/cz-conventional-changelog"
}
"files": [
"lib",
"README.md"
],
"homepage": "https://github.com/ar4mirez/hapi-octopus#readme",
"keywords": [
"handlers",
"hapijs",
"methods",
"plugin",
"pres",
"routes"
],
"license": "SEE LICENSE IN <LICENSE.md>",
"main": "dist/index.js",
"repository": {
"type": "git",
"url": "https://github.com/ar4mirez/hapi-octopus.git"
},
"dependencies": {
"joi": "10.2.1"
"scripts": {
"build": "npm-run-all --parallel build:*",
"build:main": "babel --copy-files --out-dir dist lib",
"commit": "git-cz",
"coverage": "lab -r lcov -o coverage/lcov.info && npm t",
"coverage:check": "lab -t 100",
"coverage:report": "lab -r lcov | codecov",
"prebuild": "rimraf dist",
"semantic-release": "semantic-release pre && npm publish && semantic-release post",
"setup": "npm install && npm run validate",
"test": "lab -c -C -L -S -t 100 -a code",
"validate": "npm run coverage && npm run coverage:check"
}
}
14 changes: 13 additions & 1 deletion test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Lab.describe(`${Lab.pkg.name}:utils`, () => {

Lab.it('should return an empty array if not files were found.', (done) => {

const files = Utils.loadFilesFromDir('*.js');
const files = Utils.loadFilesFromDir('*.js', { cwd: '/' });

Lab.expect(files).to.exist();
Lab.expect(files).to.be.an.array();
Expand All @@ -43,4 +43,16 @@ Lab.describe(`${Lab.pkg.name}:utils`, () => {

return done();
});

Lab.it('should fail if cwd is not a directory.', (done) => {

Lab.expect(() => {

Utils.loadFilesFromDir('*.js', {
cwd: `${__dirname}/artifacts/routes/single_route.js`
});
}).to.throw(Error);

return done();
});
});
Loading

0 comments on commit b69b219

Please sign in to comment.