Skip to content

Commit fa499c6

Browse files
committed
Use .csscomb.json as config if it's present
`sortOrder` option is renamed to `config`
1 parent dafd6be commit fa499c6

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

Gruntfile.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = function(grunt) {
2222
jshintrc: '.jshintrc',
2323
},
2424
},
25-
25+
2626
// Before generating any new files, remove any previously-created files.
2727
clean: {
2828
tests: ['test/fixtures/tmp_*.css'],
@@ -37,7 +37,7 @@ module.exports = function(grunt) {
3737
},
3838
custom: {
3939
options: {
40-
sortOrder: 'test/fixtures/sort.json'
40+
config: 'test/fixtures/sort.json'
4141
},
4242
files: {
4343
'test/fixtures/tmp_customsort.css': ['test/fixtures/style.css'],

tasks/csscomb.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,43 @@
66
* Licensed under the MIT license.
77
*/
88
'use strict';
9+
10+
var path = require('path');
11+
912
module.exports = function (grunt) {
1013

1114
grunt.registerMultiTask('csscomb', 'Sorting CSS properties in specific order.', function () {
1215

1316
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+
}
1635

1736
// Get config file from task's options:
18-
var config = grunt.task.current.options().sortOrder;
37+
var config = grunt.task.current.options().config || getConfigPath();
1938

2039
// Check if config file is set and exists. If not, use default one:
2140
if (config && grunt.file.exists(config)) {
2241
grunt.log.ok('Using custom config file "' + config + '"...');
2342
config = grunt.file.readJSON(config);
2443
} else {
25-
config = defaultConfig;
44+
grunt.log.ok('Using default config file...');
45+
config = comb.getConfig('csscomb');
2646
}
2747

2848
// Configure csscomb:

0 commit comments

Comments
 (0)