Skip to content

Commit 55669a4

Browse files
committed
Update v0.5
1 parent 67123b9 commit 55669a4

File tree

6 files changed

+69
-59
lines changed

6 files changed

+69
-59
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,15 @@ A string value that is used to specify custom-sort-order.json file path.
4949

5050
```js
5151
grunt.initConfig({
52-
csscomb: {
52+
foo : {
53+
files: {
54+
'dest/resorted-foo.css': ['src/foo.css'],
55+
},
56+
},
57+
bar : {
5358
files: {
5459
'dest/resorted-foo.css': ['src/foo.css'],
60+
'dest/resorted-bar.css': ['src/bar.css'],
5561
},
5662
},
5763
})
@@ -76,7 +82,7 @@ grunt.initConfig({
7682

7783
## Release History
7884

79-
+ v0.5.0: Add error handling.
85+
+ v0.5.0: Enable multiple files.
8086
+ v0.4.0: Move to csscomb's repository.
8187
+ v0.3.0: Fix sort option bug.
8288
+ v0.2.0: Fix bugs.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
"scripts": {
2929
"test": "grunt test"
3030
},
31+
"dependencies": {
32+
"async": "~0.2.9"
33+
},
3134
"devDependencies": {
3235
"grunt-contrib-jshint": "~0.6.0",
3336
"grunt-contrib-clean": "~0.4.0",

tasks/csscomb.js

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,61 +6,62 @@
66
* Licensed under the MIT license.
77
*/
88
'use strict';
9-
module.exports = function(grunt) {
9+
module.exports = function (grunt) {
1010

11-
grunt.registerMultiTask('csscomb', 'Sorting CSS properties in specific order.', function() {
12-
var fs = require('fs'),
11+
grunt.registerMultiTask('csscomb', 'Sorting CSS properties in specific order.', function () {
12+
13+
var async = require('async'),
14+
fs = require('fs'),
1315
path = require('path'),
14-
exec = require('child_process').exec;
15-
var command,
16-
done = this.async(),
1716
realPath = path.dirname(fs.realpathSync(__filename)),
18-
cssComb = 'php ' + realPath + '/lib/csscomb.php',
19-
fileSrc = '',
20-
fileDest = '',
21-
fileSort = '',
22-
options = this.options({
23-
sortOrder: null
24-
});
17+
done = this.async();
2518

26-
if (options.sortOrder !== null) {
27-
if (grunt.file.exists(options.sortOrder)) {
28-
fileSort = ' -s ' + options.sortOrder;
29-
} else {
30-
grunt.log.error('Custom sort .json file not found.');
31-
return false;
32-
}
33-
}
19+
async.eachSeries(this.files, function (file, next) {
20+
var args = [],
21+
child = {
22+
cmd: 'php',
23+
args: args
24+
},
25+
options = grunt.task.current.options({
26+
sortOrder: null
27+
});
3428

35-
function puts(error, stdout, stderr) {
36-
if (error !== null) {
37-
grunt.log.error(error);
29+
args.push(realPath + '/lib/csscomb.php');
3830

39-
} else {
40-
grunt.log.ok(stdout);
31+
if (options.sortOrder !== null) {
32+
if (grunt.file.exists(options.sortOrder)) {
33+
args.push('-s', options.sortOrder);
34+
} else {
35+
grunt.log.error('Custom sort .json file not found.');
36+
return false;
37+
}
4138
}
42-
}
4339

44-
this.files.forEach(function(file) {
45-
fileSrc = file.src.filter(function(filepath) {
40+
var fileSrc = file.src.filter(function (filepath) {
4641
// Remove nonexistent files (it's up to you to filter or warn here).
4742
if (!grunt.file.exists(filepath)) {
4843
grunt.log.warn('Source file "' + filepath + '" not found.');
4944
return false;
5045
} else {
51-
return true;
46+
return filepath;
5247
}
53-
}).map(function(filepath) {
54-
return filepath;
5548
});
56-
fileSrc = ' -i ' + fileSrc;
49+
args.push('-i', fileSrc);
50+
5751
if (file.dest !== null) {
58-
fileDest = ' -o ' + file.dest;
52+
args.push('-o', file.dest);
5953
}
60-
command = cssComb + fileSort + fileSrc + fileDest;
61-
exec(command, puts);
62-
grunt.verbose.writeln('`' + command + '` was initiated.');
63-
});
64-
54+
55+
grunt.util.spawn(child, function (error, result, code) {
56+
if (error !== null) {
57+
grunt.log.error(error);
58+
} else {
59+
grunt.log.ok(result);
60+
next();
61+
}
62+
});
63+
grunt.verbose.ok('`php ' + child.args.join(' ') + '` was initiated.');
64+
}, done);
65+
6566
});
6667
};

test/fixtures/multi1.css

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
.multi1 {
2-
text-align: center;
3-
border-color: 1px #000 solid;
4-
background-color: red;
5-
height: 100px;
2+
z-index: 100;
63
display: block;
7-
font-weight: bold;
8-
width: 100px;
4+
visibility: hidden;
95
max-height: 44px;
6+
width: 100px;
7+
height: 100px;
8+
border-color: 1px #000 solid;
9+
background-color: red;
1010
vertical-align: 5px;
11-
visibility: hidden;
12-
z-index: 100;
11+
text-align: center;
12+
font-weight: bold;
1313
}

test/fixtures/multi2.css

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
.multi2 {
2-
width: 10px;
3-
border: 1px #fff solid;
42
position: absolute;
5-
z-index: 10;
6-
margin: 10px;
7-
left: 10px;
8-
height: 10px;
9-
float: left;
103
top: 10px;
4+
left: 10px;
5+
z-index: 10;
116
display: table;
7+
float: left;
8+
margin: 10px;
129
padding: 10px;
10+
width: 10px;
11+
height: 10px;
12+
border: 1px #fff solid;
1313
}

test/fixtures/style.css

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
.comb {
2-
text-align: center;
32
width: 100px;
4-
background-color: red;
53
height: 100px;
6-
display: block;
4+
background-color: red;
5+
text-align: center;
76
font-weight: bold;
7+
display: block;
88
}

0 commit comments

Comments
 (0)