Skip to content

Commit

Permalink
Add configurable config file
Browse files Browse the repository at this point in the history
* Add ability to recurse up directory tree to find nearest config file
* Add ability to define a custom config path in `package.json` (relative to `package.json`)

Resolves sasstools#47
  • Loading branch information
Snugug committed Aug 30, 2015
1 parent 9f5fead commit 2dd52a0
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,55 @@ var yaml = require('js-yaml'),
path = require('path'),
merge = require('merge');

var defaults = yaml.safeLoad(fs.readFileSync(path.join(__dirname, '/config/sass-lint.yml'), 'utf8')),
configPath = path.join(process.cwd(), '/.sass-lint.yml');
var defaults = yaml.safeLoad(fs.readFileSync(path.join(__dirname, 'config', 'sass-lint.yml'), 'utf8'));

var findFile = function findFile (configPath, filename) {
var HOME = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE,
dirname,
parentDirname;

configPath = configPath || path.join(process.cwd(), filename);

if (fs.existsSync(configPath)) {
return fs.realpathSync(configPath);
}

dirname = path.dirname(configPath);
parentDirname = path.dirname(dirname);

if (dirname === HOME || dirname === parentDirname) {
return null;
}

configPath = path.join(parentDirname, filename);

return findFile(configPath, filename);
};

module.exports = function (options) {
var config = {},
var configPath,
meta,
metaPath,
config,
finalConfig = {};

finalConfig = defaults;
metaPath = findFile(null, 'package.json');
meta = require(metaPath);

if (meta.sasslintConfig) {
configPath = path.resolve(path.dirname(metaPath), meta.sasslintConfig);
}
else {
configPath = findFile(null, '.sass-lint.yml');
}

if (fs.existsSync(configPath)) {
config = yaml.safeLoad(fs.readFileSync(configPath, 'utf8'));
finalConfig = merge.recursive(finalConfig, config);
finalConfig = merge.recursive(defaults, config);
}

if (options) {
finalConfig = merge.recursive(finalConfig, options);
finalConfig = merge.recursive(defaults, options);
}

return finalConfig;
Expand Down

0 comments on commit 2dd52a0

Please sign in to comment.