@@ -2,7 +2,7 @@ var fs = require('fs');
2
2
var path = require ( 'path' ) ;
3
3
4
4
var CleanCSS = require ( 'clean-css' ) ;
5
- var commands = require ( 'commander' ) ;
5
+ var program = require ( 'commander' ) ;
6
6
var glob = require ( 'glob' ) ;
7
7
8
8
var COMPATIBILITY_PATTERN = / ( [ \w \. ] + ) = ( \w + ) / g;
@@ -14,14 +14,15 @@ function cli(process, beforeMinifyCallback) {
14
14
var fromStdin ;
15
15
var debugMode ;
16
16
var removeInlinedFiles ;
17
+ var inputOptions ;
17
18
var options ;
18
19
var stdin ;
19
20
var data ;
20
21
21
22
beforeMinifyCallback = beforeMinifyCallback || Function . prototype ;
22
23
23
24
// Specify commander options to parse command line params correctly
24
- commands
25
+ program
25
26
. version ( buildVersion , '-v, --version' )
26
27
. usage ( '[options] <source-file ...>' )
27
28
. option ( '-c, --compatibility [ie7|ie8]' , 'Force compatibility mode (see Readme for advanced examples)' )
@@ -37,7 +38,7 @@ function cli(process, beforeMinifyCallback) {
37
38
. option ( '--source-map-inline-sources' , 'Enables inlining sources inside source maps' )
38
39
. option ( '--input-source-map [file]' , 'Specifies the path of the input source map file' ) ;
39
40
40
- commands . on ( '--help' , function ( ) {
41
+ program . on ( '--help' , function ( ) {
41
42
console . log ( ' Examples:\n' ) ;
42
43
console . log ( ' %> cleancss one.css' ) ;
43
44
console . log ( ' %> cleancss -o one-min.css one.css' ) ;
@@ -118,49 +119,48 @@ function cli(process, beforeMinifyCallback) {
118
119
process . exit ( ) ;
119
120
} ) ;
120
121
121
- commands . parse ( process . argv ) ;
122
-
123
- if ( commands . rawArgs . indexOf ( '-O0' ) > - 1 ) {
124
- commands . O0 = true ;
125
- }
126
-
127
- if ( commands . rawArgs . indexOf ( '-O1' ) > - 1 ) {
128
- commands . O1 = findArgumentTo ( '-O1' , commands . rawArgs , commands . args ) ;
129
- }
130
-
131
- if ( commands . rawArgs . indexOf ( '-O2' ) > - 1 ) {
132
- commands . O2 = findArgumentTo ( '-O2' , commands . rawArgs , commands . args ) ;
133
- }
122
+ program . parse ( process . argv ) ;
123
+ inputOptions = program . opts ( ) ;
134
124
135
125
// If no sensible data passed in just print help and exit
136
- if ( commands . args . length === 0 ) {
126
+ if ( program . args . length === 0 ) {
137
127
fromStdin = ! process . env . __DIRECT__ && ! process . stdin . isTTY ;
138
128
if ( ! fromStdin ) {
139
- commands . outputHelp ( ) ;
129
+ program . outputHelp ( ) ;
140
130
return 0 ;
141
131
}
142
132
}
143
133
144
- // Now coerce commands into CleanCSS configuration...
145
- debugMode = commands . debug ;
146
- removeInlinedFiles = commands . removeInlinedFiles ;
134
+ // Now coerce arguments into CleanCSS configuration...
135
+ debugMode = inputOptions . debug ;
136
+ removeInlinedFiles = inputOptions . removeInlinedFiles ;
147
137
148
138
options = {
149
- compatibility : commands . compatibility ,
150
- format : commands . format ,
151
- inline : typeof commands . inline == 'string' ? commands . inline : 'local' ,
152
- inlineTimeout : commands . inlineTimeout * 1000 ,
153
- level : commands . O0 || commands . O1 || commands . O2 ?
154
- { '0' : commands . O0 , '1' : commands . O1 , '2' : commands . O2 } :
155
- undefined ,
156
- output : commands . output ,
157
- rebase : commands . withRebase ? true : false ,
158
- rebaseTo : ( 'output' in commands ) && commands . output . length > 0 ? path . dirname ( path . resolve ( commands . output ) ) : ( commands . withRebase ? process . cwd ( ) : undefined ) ,
159
- sourceMap : commands . sourceMap ,
160
- sourceMapInlineSources : commands . sourceMapInlineSources
139
+ compatibility : inputOptions . compatibility ,
140
+ format : inputOptions . format ,
141
+ inline : typeof inputOptions . inline == 'string' ? inputOptions . inline : 'local' ,
142
+ inlineTimeout : inputOptions . inlineTimeout * 1000 ,
143
+ level : { 1 : true } ,
144
+ output : inputOptions . output ,
145
+ rebase : inputOptions . withRebase ? true : false ,
146
+ rebaseTo : ( 'output' in inputOptions ) && inputOptions . output . length > 0 ? path . dirname ( path . resolve ( inputOptions . output ) ) : ( inputOptions . withRebase ? process . cwd ( ) : undefined ) ,
147
+ sourceMap : inputOptions . sourceMap ,
148
+ sourceMapInlineSources : inputOptions . sourceMapInlineSources
161
149
} ;
162
150
163
- if ( commands . inputSourceMap && ! options . sourceMap ) {
151
+ if ( program . rawArgs . indexOf ( '-O0' ) > - 1 ) {
152
+ options . level [ 0 ] = true ;
153
+ }
154
+
155
+ if ( program . rawArgs . indexOf ( '-O1' ) > - 1 ) {
156
+ options . level [ 1 ] = findArgumentTo ( '-O1' , program . rawArgs , program . args ) ;
157
+ }
158
+
159
+ if ( program . rawArgs . indexOf ( '-O2' ) > - 1 ) {
160
+ options . level [ 2 ] = findArgumentTo ( '-O2' , program . rawArgs , program . args ) ;
161
+ }
162
+
163
+ if ( inputOptions . inputSourceMap && ! options . sourceMap ) {
164
164
options . sourceMap = true ;
165
165
}
166
166
@@ -173,12 +173,12 @@ function cli(process, beforeMinifyCallback) {
173
173
beforeMinifyCallback : beforeMinifyCallback ,
174
174
debugMode : debugMode ,
175
175
removeInlinedFiles : removeInlinedFiles ,
176
- inputSourceMap : commands . inputSourceMap
176
+ inputSourceMap : inputOptions . inputSourceMap
177
177
} ;
178
178
179
179
// ... and do the magic!
180
- if ( commands . args . length > 0 ) {
181
- minify ( process , options , configurations , expandGlobs ( commands . args ) ) ;
180
+ if ( program . args . length > 0 ) {
181
+ minify ( process , options , configurations , expandGlobs ( program . args ) ) ;
182
182
} else {
183
183
stdin = process . openStdin ( ) ;
184
184
stdin . setEncoding ( 'utf-8' ) ;
0 commit comments