1+ var cssp = require ( 'cssp' ) ;
2+ var minimatch = require ( 'minimatch' ) ;
3+ var vow = require ( 'vow' ) ;
4+ var vowFs = require ( 'vow-fs' ) ;
5+
16/**
2- * Starts Code Style checking process.
7+ * Starts Code Style processing process.
38 *
49 * @name Comb
510 */
@@ -11,13 +16,14 @@ var Comb = function() {
1116 'sort-order' : { }
1217 } ,
1318 this . _config = { } ;
14- this . _excludes = null ;
19+ this . _exclude = null ;
1520} ;
1621
1722Comb . prototype = {
1823
1924 /**
2025 * Loads configuration from JSON.
26+ * Activates and configures required rules.
2127 *
2228 * @param {Object } config
2329 */
@@ -27,6 +33,109 @@ Comb.prototype = {
2733 this . _config [ rule ] = config [ rule ] ;
2834 }
2935 }
36+ this . _exclude = ( config . exclude || [ ] ) . map ( function ( pattern ) {
37+ return new minimatch . Minimatch ( pattern ) ;
38+ } ) ;
39+ } ,
40+
41+ /**
42+ * Process file provided with a string.
43+ * @param {String } text
44+ * @param {String } filename
45+ */
46+ processString : function ( text , filename ) {
47+ var tree ;
48+ try {
49+ tree = cssp . parse ( text ) ;
50+ } catch ( e ) {
51+ throw new Error ( 'Parsing error at ' + filename + ': ' + e . message ) ;
52+ }
53+ text = cssp . translate ( cssp . transform ( tree ) ) ;
54+ } ,
55+
56+ /**
57+ * Process single file.
58+ *
59+ * @param {String } path
60+ * @returns {Promise }
61+ */
62+ processFile : function ( path ) {
63+ var _this = this ;
64+ if ( this . _shouldProcess ( path ) && path . match ( / \. c s s $ / ) ) {
65+ return vowFs . read ( path , 'utf8' ) . then ( function ( data ) {
66+ return _this . processString ( data , path ) ;
67+ } ) ;
68+ }
69+ return null ;
70+ } ,
71+
72+ /**
73+ * Process directory recursively.
74+ *
75+ * @param {String } path
76+ * @returns {Promise }
77+ */
78+ processDirectory : function ( path ) {
79+ var _this = this ;
80+ return vowFs . listDir ( path ) . then ( function ( filenames ) {
81+ return vow . all ( filenames . map ( function ( filename ) {
82+ var fullname = path + '/' + filename ;
83+ return vowFs . stat ( fullname ) . then ( function ( stat ) {
84+ if ( _this . _shouldProcess ( fullname ) ) {
85+ if ( stat . isDirectory ( ) ) {
86+ return _this . processDirectory ( fullname ) ;
87+ } else if ( fullname . match ( / \. c s s $ / ) ) {
88+ return vow . when ( _this . processFile ( fullname ) ) . then ( function ( errors ) {
89+ if ( errors ) return errors ;
90+ } ) ;
91+ }
92+ }
93+ return ;
94+ } ) ;
95+ } ) ) . then ( function ( results ) {
96+ return [ ] . concat . apply ( [ ] , results ) ;
97+ } ) ;
98+ } ) ;
99+ } ,
100+
101+ /**
102+ * Processs directory or file.
103+ *
104+ * @param {String } path
105+ */
106+ processPath : function ( path ) {
107+ path = path . replace ( / \/ $ / , '' ) ;
108+ var _this = this ;
109+ return vowFs . exists ( path ) . then ( function ( exists ) {
110+ if ( exists ) {
111+ return vowFs . stat ( path ) . then ( function ( stat ) {
112+ if ( stat . isDirectory ( ) ) {
113+ return _this . processDirectory ( path ) ;
114+ } else {
115+ return vow . when ( _this . processFile ( path ) ) . then ( function ( errors ) {
116+ if ( errors ) return [ errors ] ;
117+ return ;
118+ } ) ;
119+ }
120+ } ) ;
121+ } else {
122+ throw new Error ( 'Path ' + path + ' was not found.' ) ;
123+ }
124+ } ) ;
125+ } ,
126+
127+ /**
128+ * Returns true if specified path is not in exluded list.
129+ *
130+ * @returns {Boolean }
131+ */
132+ _shouldProcess : function ( path ) {
133+ path = path . replace ( / ^ \. \/ / , '' ) ;
134+ var exclude = this . _exclude ;
135+ for ( var i = exclude . length ; i -- ; ) {
136+ if ( exclude [ i ] . match ( path ) ) return false ;
137+ }
138+ return true ;
30139 }
31140
32141} ;
0 commit comments