Skip to content

Commit

Permalink
Add a Lesshint.getConfig() method. Closes #290
Browse files Browse the repository at this point in the history
  • Loading branch information
jwilsson committed Dec 21, 2016
1 parent 7a4fff5 commit 4348d98
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
16 changes: 11 additions & 5 deletions lib/config-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions lib/lesshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
30 changes: 30 additions & 0 deletions test/specs/config-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
13 changes: 13 additions & 0 deletions test/specs/lesshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 4348d98

Please sign in to comment.