diff --git a/lib/utils.js b/lib/utils.js index 3f91179..b4786d5 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -2,16 +2,23 @@ const Glob = require('glob'); const Hoek = require('hoek'); +const Joi = require('joi'); const _ = require('lodash'); const internals = { - options: { - cwd: process.cwd() - } + options: {} }; exports.loadFilesFromDir = (pattern, options) => { + const validation = Joi.validate(options, Joi.object().keys({ + cwd: Joi.string() + })); + + if (validation.error) { + throw new Error(validation.error.annotate()); + } + const files = Glob.sync(pattern, Hoek.applyToDefaults(internals.options, options)); return _.map(files, (file) => (`${options.cwd}/${file}`)); }; diff --git a/package.json b/package.json index 9b6d7b0..8df5b45 100644 --- a/package.json +++ b/package.json @@ -42,5 +42,8 @@ "commitizen": { "path": "node_modules/cz-conventional-changelog" } + }, + "dependencies": { + "joi": "10.2.1" } } diff --git a/test/utils.js b/test/utils.js index 24f9512..2d91de2 100644 --- a/test/utils.js +++ b/test/utils.js @@ -26,7 +26,20 @@ Lab.describe(`${Lab.pkg.name}:utils`, () => { return done(); }); - Lab.it('should use fail if not cwd were passed.', (done) => { + Lab.it('should return an empty array if not files were found.', (done) => { + + const files = Utils.loadFilesFromDir('*.js'); + + Lab.expect(files).to.exist(); + Lab.expect(files).to.be.an.array(); + Lab.expect(files).to.have.length(0); + + return done(); + }); + + Lab.it('should fail if an invalid cwd option were passed.', (done) => { + + Lab.expect(() => Utils.loadFilesFromDir('*.js', { cwd: 123 })).to.throw(Error); return done(); });