55 * ./node_modules/.bin/csscomb [options] file1 [dir1 [fileN [dirN]]]
66 */
77var fs = require ( 'fs' ) ;
8+ var path = require ( 'path' ) ;
89var program = require ( 'commander' ) ;
910var vow = require ( 'vow' ) ;
1011var 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 ( ) ;
2755var comb = new Comb ( ) ;
2856
2957if ( program . detect ) {
0 commit comments