Skip to content

Commit dd9c7dd

Browse files
committed
Merge pull request #118 from csscomb/tg/search-config
CLI: Search for a config file recursively (#68, 106)
2 parents 6ed375a + 3419a91 commit dd9c7dd

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

lib/cli.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* ./node_modules/.bin/csscomb [options] file1 [dir1 [fileN [dirN]]]
66
*/
77
var fs = require('fs');
8+
var path = require('path');
89
var program = require('commander');
910
var vow = require('vow');
1011
var Comb = require('./csscomb');
@@ -23,7 +24,34 @@ if (!program.args.length) {
2324
program.help();
2425
}
2526

26-
var configPath = program.config || (process.cwd() + '/config/csscomb.json');
27+
/**
28+
* Look for a config file: recursively from current (process) directory
29+
* up to $HOME dir
30+
* @param {String} configPath
31+
* @returns {String}
32+
*/
33+
function getConfigPath(configPath) {
34+
var HOME = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
35+
// Since `process.cwd()` can be absolutely anything, build default path
36+
// relative to current directory:
37+
var defaultConfigPath = __dirname + '/../config/csscomb.json';
38+
39+
configPath = configPath || process.cwd() + '/.csscomb.json';
40+
41+
// If we've finally found a config, return its path:
42+
if (fs.existsSync(configPath)) return configPath;
43+
44+
// If we are in HOME dir already and yet no config file, return a default
45+
// one from our package:
46+
if (path.dirname(configPath) === HOME) return defaultConfigPath;
47+
48+
// If there is no config in this directory, go one level up and look for
49+
// a config there:
50+
configPath = path.dirname(path.dirname(configPath)) + '/.csscomb.json';
51+
return getConfigPath(configPath);
52+
}
53+
54+
var configPath = program.config || getConfigPath();
2755
var comb = new Comb();
2856

2957
if (program.detect) {

0 commit comments

Comments
 (0)