|
6 | 6 | * Licensed under the MIT license. |
7 | 7 | */ |
8 | 8 | 'use strict'; |
| 9 | + |
| 10 | +var path = require('path'); |
| 11 | + |
9 | 12 | module.exports = function (grunt) { |
10 | 13 |
|
11 | 14 | grunt.registerMultiTask('csscomb', 'Sorting CSS properties in specific order.', function () { |
12 | 15 |
|
13 | 16 | var Comb = require('csscomb'), |
14 | | - comb = new Comb(), |
15 | | - defaultConfig = comb.getConfig('csscomb'); |
| 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 | + } |
16 | 35 |
|
17 | 36 | // Get config file from task's options: |
18 | | - var config = grunt.task.current.options().sortOrder; |
| 37 | + var config = grunt.task.current.options().config || getConfigPath(); |
19 | 38 |
|
20 | 39 | // Check if config file is set and exists. If not, use default one: |
21 | 40 | if (config && grunt.file.exists(config)) { |
22 | 41 | grunt.log.ok('Using custom config file "' + config + '"...'); |
23 | 42 | config = grunt.file.readJSON(config); |
24 | 43 | } else { |
25 | | - config = defaultConfig; |
| 44 | + grunt.log.ok('Using default config file...'); |
| 45 | + config = comb.getConfig('csscomb'); |
26 | 46 | } |
27 | 47 |
|
28 | 48 | // Configure csscomb: |
|
0 commit comments