66 * Licensed under the MIT license.
77 */
88'use strict' ;
9+
10+ var path = require ( 'path' ) ;
11+
912module . exports = function ( grunt ) {
1013
1114 grunt . registerMultiTask ( 'csscomb' , 'Sorting CSS properties in specific order.' , function ( ) {
1215
1316 var Comb = require ( 'csscomb' ) ,
14- defaultConfig = require ( '../node_modules/csscomb/.csscomb.json' ) ;
17+ comb = new Comb ( ) ;
18+
19+ function getConfigPath ( configPath ) {
20+ var HOME = process . env . HOME || process . env . HOMEPATH || process . env . USERPROFILE ;
21+
22+ configPath = configPath || process . cwd ( ) + '/.csscomb.json' ;
23+
24+ // If we've finally found a config, return its path:
25+ if ( grunt . file . exists ( configPath ) ) return configPath ;
26+
27+ // If we are in HOME dir already and yet no config file, quit:
28+ if ( path . dirname ( configPath ) === HOME ) return ;
29+
30+ // If there is no config in this directory, go one level up and look for
31+ // a config there:
32+ configPath = path . dirname ( path . dirname ( configPath ) ) + '/.csscomb.json' ;
33+ return getConfigPath ( configPath ) ;
34+ }
1535
1636 // Get config file from task's options:
17- var config = grunt . task . current . options ( ) . sortOrder ;
37+ var config = grunt . task . current . options ( ) . config || getConfigPath ( ) ;
1838
1939 // Check if config file is set and exists. If not, use default one:
2040 if ( config && grunt . file . exists ( config ) ) {
2141 grunt . log . ok ( 'Using custom config file "' + config + '"...' ) ;
2242 config = grunt . file . readJSON ( config ) ;
2343 } else {
24- config = defaultConfig ;
44+ grunt . log . ok ( 'Using default config file...' ) ;
45+ config = comb . getConfig ( 'csscomb' ) ;
2546 }
2647
27- this . files . forEach ( function ( f ) {
48+ // Configure csscomb:
49+ comb . configure ( config ) ;
2850
29- // Create a new instance of csscomb and configure it:
30- var comb = new Comb ( ) ;
31- comb . configure ( config ) ;
51+ this . files . forEach ( function ( f ) {
3252
3353 f . src . filter ( function ( filepath ) {
3454 // Warn on and remove invalid source files (if nonull was set).
@@ -50,4 +70,4 @@ module.exports = function (grunt) {
5070 } ) ;
5171 } ) ;
5272 } ) ;
53- } ;
73+ } ;
0 commit comments