6
6
* Usage example:
7
7
* ./node_modules/.bin/csscomb [options] [file1 [dir1 [fileN [dirN]]]]
8
8
*/
9
- var format = require ( './format' ) ;
10
9
var fs = require ( 'fs' ) ;
11
10
var parseArgs = require ( 'minimist' ) ;
12
11
var path = require ( 'path' ) ;
13
- var vow = require ( 'vow' ) ;
12
+
14
13
var Comb = require ( './csscomb' ) ;
14
+ var Errors = require ( './errors' ) ;
15
+
16
+
17
+ var getInputData = new Promise ( function ( resolve ) {
18
+ var input = '' ;
19
+ process . stdin . resume ( ) ;
20
+ process . stdin . setEncoding ( 'utf8' ) ;
21
+ process . stdin . on ( 'data' , function ( data ) {
22
+ input += data ;
23
+ } ) ;
24
+ process . stdin . on ( 'end' , function ( ) {
25
+ resolve ( input ) ;
26
+ } ) ;
27
+ } ) ;
28
+
15
29
var comb = new Comb ( ) ;
30
+ var options = getOptions ( ) ;
31
+
32
+ if ( options . help ) {
33
+ displayHelp ( ) ;
34
+ process . exit ( 0 ) ;
35
+ }
36
+
37
+ if ( options . detect ) {
38
+ const config = detectConfig ( ) ;
39
+ process . stdout . write ( config ) ;
40
+ process . exit ( 0 ) ;
41
+ }
42
+
43
+ var config = getConfig ( ) ;
44
+ comb . configure ( config ) ;
45
+
46
+ if ( options . fix && process . stdin . isTTY ) {
47
+ processFiles ( options . _ ) ;
48
+ } else if ( options . fix ) {
49
+ processSTDIN ( ) ;
50
+ } else if ( process . stdin . isTTY ) {
51
+ lintFiles ( options . _ ) ;
52
+ } else {
53
+ lintSTDIN ( ) ;
54
+ }
55
+
56
+
57
+ function getOptions ( ) {
58
+ var parserOptions = {
59
+ boolean : [ 'help' , 'fix' , 'verbose' ] ,
60
+ alias : {
61
+ config : 'c' ,
62
+ detect : 'd' ,
63
+ fix : 'f' ,
64
+ help : 'h' ,
65
+ verbose : 'v'
66
+ }
67
+ } ;
68
+ return parseArgs ( process . argv . slice ( 2 ) , parserOptions ) ;
69
+ }
16
70
17
71
function displayHelp ( ) {
18
72
var help = [
@@ -30,43 +84,105 @@ function displayHelp() {
30
84
' Run the tool in detect mode, returning detected options.' ,
31
85
' -f, --fix' ,
32
86
' Run the tool in fixer mode, modifying files when possible.' ,
87
+ ' -h, --help' ,
88
+ ' Display help message.' ,
33
89
' -v, --verbose' ,
34
90
' Whether to print logging info.'
35
91
] ;
36
- console . log ( help . join ( '\n' ) ) ;
92
+ process . stdout . write ( help . join ( '\n' ) ) ;
37
93
}
38
94
39
- var getInputData = new vow . Promise ( function ( resolve ) {
40
- var input = '' ;
41
- process . stdin . resume ( ) ;
42
- process . stdin . setEncoding ( 'utf8' ) ;
43
- process . stdin . on ( 'data' , function ( data ) {
44
- input += data ;
45
- } ) ;
46
- process . stdin . on ( 'end' , function ( ) {
47
- resolve ( input ) ;
95
+ function detectConfig ( ) {
96
+ const config = Comb . detectInFile ( options . detect ) ;
97
+ return JSON . stringify ( config , false , 4 ) ;
98
+ }
99
+
100
+ function getConfig ( ) {
101
+ var configPath = options . config &&
102
+ path . resolve ( process . cwd ( ) , options . config ) ||
103
+ Comb . getCustomConfigPath ( ) ;
104
+
105
+ var config ;
106
+ if ( ! fs . existsSync ( configPath ) ) {
107
+ config = require ( '../config/csscomb.json' ) ;
108
+ } else if ( configPath . match ( / \. c s s $ / ) ) {
109
+ config = Comb . detectInFile ( configPath ) ;
110
+ } else {
111
+ config = Comb . getCustomConfig ( configPath ) ;
112
+ }
113
+
114
+ if ( ! config ) {
115
+ const errorMessage = Errors . configParsingError ( configPath ) ;
116
+ process . stderr . write ( errorMessage ) ;
117
+ process . exit ( 1 ) ;
118
+ }
119
+
120
+ applyTemplate ( config ) ;
121
+ if ( options . verbose ) config . verbose = options . verbose ;
122
+
123
+ return config ;
124
+ }
125
+
126
+ function applyTemplate ( config ) {
127
+ if ( ! config . template ) return ;
128
+
129
+ if ( ! fs . existsSync ( config . template ) ) {
130
+ const errorMessage = Errors . missingTemplateFile ( config . template ) ;
131
+ process . stderr . write ( errorMessage ) ;
132
+ process . exit ( 1 ) ;
133
+ }
134
+
135
+ var templateConfig = Comb . detectInFile ( config . template ) ;
136
+ for ( var attrname in templateConfig ) {
137
+ if ( templateConfig . hasOwnProperty ( attrname ) && ! config [ attrname ] ) {
138
+ config [ attrname ] = templateConfig [ attrname ] ;
139
+ }
140
+ }
141
+ }
142
+
143
+ function processFiles ( files ) {
144
+ const promises = files . map ( file => {
145
+ return comb . processPath ( file ) ;
48
146
} ) ;
49
- } ) ;
50
147
51
- function processInputData ( input ) {
52
- comb . lintString ( input ) . catch ( e => {
53
- process . stderr . write ( e . message ) ;
148
+ Promise . all ( promises ) . catch ( error => {
149
+ process . stderr . write ( error . stack ) ;
54
150
process . exit ( 1 ) ;
55
- } ) . then ( output => {
56
- if ( ! output . length ) {
57
- process . exit ( 0 ) ;
58
- } else {
59
- process . stdout . write ( output ) ;
60
- process . exit ( 1 ) ;
151
+ } ) . then ( c => {
152
+ var tbchanged = c . filter ( isChanged => {
153
+ return isChanged !== undefined ;
154
+ } ) . reduce ( ( a , b ) => {
155
+ return a + b ;
156
+ } , 0 ) ;
157
+
158
+ if ( config . verbose ) {
159
+ let message = [
160
+ `${ c . length } file${ c . length === 1 ? '' : 's' } processed` ,
161
+ `${ tbchanged } file${ tbchanged === 1 ? '' : 's' } fixed` ,
162
+ ''
163
+ ] . join ( '\n' ) ;
164
+ process . stdout . write ( message ) ;
61
165
}
166
+
167
+ process . exit ( 0 ) ;
62
168
} ) ;
63
169
}
64
170
65
171
function processSTDIN ( ) {
66
172
getInputData . then ( processInputData ) ;
67
173
}
68
174
69
- function processFiles ( files ) {
175
+ function processInputData ( input ) {
176
+ comb . processString ( input ) . catch ( e => {
177
+ process . stderr . write ( e . message ) ;
178
+ process . exit ( 1 ) ;
179
+ } ) . then ( output => {
180
+ process . stdout . write ( output ) ;
181
+ process . exit ( 0 ) ;
182
+ } ) ;
183
+ }
184
+
185
+ function lintFiles ( files ) {
70
186
let anyErrorsFound = false ;
71
187
72
188
const promises = files . map ( file => {
@@ -108,88 +224,20 @@ function formatErrors(fileName, errors) {
108
224
return message . join ( '\n' ) ;
109
225
}
110
226
111
- function getOptions ( ) {
112
- var parserOptions = {
113
- boolean : [ 'verbose' , 'lint' ] ,
114
- alias : {
115
- config : 'c' ,
116
- detect : 'd' ,
117
- lint : 'l' ,
118
- verbose : 'v'
119
- }
120
- } ;
121
- return parseArgs ( process . argv . slice ( 2 ) , parserOptions ) ;
227
+ function lintSTDIN ( ) {
228
+ getInputData . then ( lintInputData ) ;
122
229
}
123
230
124
- function applyTemplate ( config ) {
125
- if ( ! config . template ) return ;
126
-
127
- if ( ! fs . existsSync ( config . template ) ) {
128
- let message = `Template configuration file ${ config . template }
129
- was not found.` ;
130
- process . stderr . write ( format ( message ) ) ;
231
+ function lintInputData ( input ) {
232
+ comb . lintString ( input ) . catch ( e => {
233
+ process . stderr . write ( e . message ) ;
131
234
process . exit ( 1 ) ;
132
- }
133
-
134
- var templateConfig = Comb . detectInFile ( config . template ) ;
135
- for ( var attrname in templateConfig ) {
136
- if ( templateConfig . hasOwnProperty ( attrname ) && ! config [ attrname ] ) {
137
- config [ attrname ] = templateConfig [ attrname ] ;
235
+ } ) . then ( output => {
236
+ if ( ! output . length ) {
237
+ process . exit ( 0 ) ;
238
+ } else {
239
+ process . stdout . write ( output ) ;
240
+ process . exit ( 1 ) ;
138
241
}
139
- }
140
- }
141
-
142
- function getConfig ( options ) {
143
- var configPath = options . config &&
144
- path . resolve ( process . cwd ( ) , options . config ) ||
145
- Comb . getCustomConfigPath ( ) ;
146
-
147
- var config ;
148
- if ( ! fs . existsSync ( configPath ) ) {
149
- config = require ( '../config/csscomb.json' ) ;
150
- } else if ( configPath . match ( / \. c s s $ / ) ) {
151
- config = Comb . detectInFile ( configPath ) ;
152
- } else {
153
- config = Comb . getCustomConfig ( configPath ) ;
154
- }
155
-
156
- if ( ! config ) {
157
- let message = `Error parsing configuration file ${ configPath } .` ;
158
- process . stderr . write ( format ( message ) ) ;
159
- process . exit ( 1 ) ;
160
- }
161
-
162
- applyTemplate ( config ) ;
163
- if ( options . verbose ) config . verbose = options . verbose ;
164
-
165
- return config ;
166
- }
167
-
168
- function detectConfig ( file ) {
169
- var config = Comb . detectInFile ( file ) ;
170
- config = JSON . stringify ( config , false , 4 ) ;
171
- process . stdout . write ( config ) ;
172
- process . exit ( 0 ) ;
173
- }
174
-
175
- console . time ( 'Time spent' ) ;
176
-
177
- var options = getOptions ( ) ;
178
-
179
- if ( options . help ) {
180
- displayHelp ( ) ;
181
- process . exit ( 0 ) ;
182
- }
183
-
184
- if ( options . detect ) {
185
- detectConfig ( options . detect ) ;
186
- }
187
-
188
- var config = getConfig ( options ) ;
189
- comb . configure ( config ) ;
190
-
191
- if ( process . stdin . isTTY ) {
192
- processFiles ( options . _ ) ;
193
- } else {
194
- processSTDIN ( ) ;
242
+ } ) ;
195
243
}
0 commit comments