From 2dd52a0597af024ea32f371a1a157f27aacb3369 Mon Sep 17 00:00:00 2001 From: Sam Richard Date: Sun, 30 Aug 2015 18:39:38 -0400 Subject: [PATCH] Add configurable config file * 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 #47 --- lib/config.js | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/lib/config.js b/lib/config.js index da2f4194..8c94a893 100644 --- a/lib/config.js +++ b/lib/config.js @@ -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;