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} ;
0 commit comments