From 4348d987016f53c41df94daadb9b068b5452516d Mon Sep 17 00:00:00 2001 From: Jonathan Wilsson Date: Wed, 21 Dec 2016 19:16:11 +0100 Subject: [PATCH] Add a Lesshint.getConfig() method. Closes #290 --- lib/config-loader.js | 16 +++++++++++----- lib/lesshint.js | 6 ++++++ test/specs/config-loader.js | 30 ++++++++++++++++++++++++++++++ test/specs/lesshint.js | 13 +++++++++++++ 4 files changed, 60 insertions(+), 5 deletions(-) diff --git a/lib/config-loader.js b/lib/config-loader.js index 27d6ce1b..bcb253bf 100644 --- a/lib/config-loader.js +++ b/lib/config-loader.js @@ -17,18 +17,24 @@ var loadConfig = function (path) { return JSON.parse(data); }; -module.exports = function (config) { +module.exports = function (path) { var rcfinder; + var config; + var stats; - // Check if a config file is passed and try to load it, otherwise try and find one - if (config) { - config = loadConfig(config); + path = path || process.cwd(); + stats = fs.statSync(path); + + if (stats.isFile()) { + // A file was passed, try to load it + config = loadConfig(path); } else { + // Try to find a config file instead rcfinder = new RcFinder('.lesshintrc', { loader: loadConfig }); - config = rcfinder.find(process.cwd()); + config = rcfinder.find(path); } return config; diff --git a/lib/lesshint.js b/lib/lesshint.js index 9a72386f..a6318077 100644 --- a/lib/lesshint.js +++ b/lib/lesshint.js @@ -129,6 +129,12 @@ Lesshint.prototype.isExcluded = function (checkPath) { }); }; +Lesshint.prototype.getConfig = function (path) { + var config = configLoader(path); + + return config; +}; + Lesshint.prototype.getReporter = function (reporter) { var reporterPath; diff --git a/test/specs/config-loader.js b/test/specs/config-loader.js index b1cd53e5..a7a1ea56 100644 --- a/test/specs/config-loader.js +++ b/test/specs/config-loader.js @@ -57,4 +57,34 @@ describe('config-loader', function () { expect(loader).to.not.throw(Error); }); + + it('should load a config file when passed one', function () { + var configPath = path.join(path.dirname(__dirname), '/data/config/config.json'); + var expected = JSON.parse(fs.readFileSync(configPath, 'utf8')); + var config; + + config = configLoader(configPath); + + expect(config).to.deep.equal(expected); + }); + + it('should look for a .lesshintrc file when passed a directory', function () { + var configPath = path.resolve(__dirname, '../.lesshintrc'); + var result; + + var expected = { + spaceBeforeBrace: { + enabled: true, + style: 'one_space' + } + }; + + fs.writeFileSync(configPath, JSON.stringify(expected)); + + result = configLoader(__dirname); + + expect(result).to.deep.equal(expected); + + rimraf.sync(configPath); + }); }); diff --git a/test/specs/lesshint.js b/test/specs/lesshint.js index 5824262f..0ac2c099 100644 --- a/test/specs/lesshint.js +++ b/test/specs/lesshint.js @@ -226,6 +226,19 @@ describe('lesshint', function () { }); }); + describe('getConfig', function () { + it('should load the specified config file', function () { + var configPath = path.join(path.dirname(__dirname), '/data/config/config.json'); + var expected = configLoader(configPath); + var lesshint = new Lesshint(); + var config; + + config = lesshint.getConfig(configPath); + + expect(config).to.deep.equal(expected); + }); + }); + describe('getReporter', function () { it('should load the specified reporter by path', function () { var lesshint = new Lesshint();