Skip to content

Commit 9c96c5b

Browse files
author
jklein
committed
Adding an exclude-files option to the command line interface
1 parent 73ee863 commit 9c96c5b

File tree

1 file changed

+65
-39
lines changed

1 file changed

+65
-39
lines changed

src/cli/common.js

Lines changed: 65 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function cli(api){
88
//-------------------------------------------------------------------------
99
// Helper functions
1010
//-------------------------------------------------------------------------
11-
11+
1212
/**
1313
* Returns an array of messages for a particular type.
1414
* @param messages {Array} Array of CSS Lint messages.
@@ -18,7 +18,7 @@ function cli(api){
1818
function pluckByType(messages, type){
1919
return messages.filter(function(message) {
2020
return message.type === type;
21-
});
21+
});
2222
}
2323

2424
/**
@@ -29,24 +29,24 @@ function cli(api){
2929
function gatherRules(options, ruleset){
3030
var warnings = options.rules || options.warnings,
3131
errors = options.errors;
32-
32+
3333
if (warnings){
3434
ruleset = ruleset || {};
3535
warnings.split(",").forEach(function(value){
3636
ruleset[value] = 1;
3737
});
3838
}
39-
39+
4040
if (errors){
4141
ruleset = ruleset || {};
4242
errors.split(",").forEach(function(value){
4343
ruleset[value] = 2;
4444
});
4545
}
46-
46+
4747
return ruleset;
4848
}
49-
49+
5050
/**
5151
* Filters out rules using the ignore command line option.
5252
* @param options {Object} the CLI options
@@ -55,17 +55,40 @@ function cli(api){
5555
function filterRules(options) {
5656
var ignore = options.ignore,
5757
ruleset = null;
58-
58+
5959
if (ignore) {
6060
ruleset = CSSLint.getRuleset();
6161
ignore.split(",").forEach(function(value){
6262
delete ruleset[value];
63-
});
63+
});
6464
}
65-
65+
6666
return ruleset;
6767
}
6868

69+
70+
/**
71+
* Filters out files using the exclude-files command line option.
72+
* @param files {Array} the list of files to check for exclusions
73+
* @param options {Object} the CLI options
74+
* @return {Array} A list of files
75+
*/
76+
function filterFiles(files, options) {
77+
var excludeFiles = options["exclude-files"],
78+
filesToLint = files;
79+
80+
81+
if (excludeFiles) {
82+
excludeFiles.split(",").forEach(function(value){
83+
if (filesToLint.indexOf(value) > -1) {
84+
filesToLint.splice(filesToLint.indexOf(value),1);
85+
}
86+
});
87+
}
88+
89+
return filesToLint;
90+
}
91+
6992
/**
7093
* Outputs all available rules to the CLI.
7194
* @return {void}
@@ -107,12 +130,12 @@ function cli(api){
107130
if (output){
108131
api.print(output);
109132
}
110-
133+
111134
if (messages.length > 0 && pluckByType(messages, "error").length > 0) {
112135
exitCode = 1;
113136
}
114137
}
115-
138+
116139
return exitCode;
117140
}
118141

@@ -126,14 +149,15 @@ function cli(api){
126149
"\nUsage: csslint-rhino.js [options]* [file|dir]*",
127150
" ",
128151
"Global Options",
129-
" --help Displays this information.",
130-
" --format=<format> Indicate which format to use for output.",
131-
" --list-rules Outputs all of the rules available.",
132-
" --quiet Only output when errors are present.",
133-
" --errors=<rule[,rule]+> Indicate which rules to include as errors.",
134-
" --warnings=<rule[,rule]+> Indicate which rules to include as warnings.",
135-
" --ignore=<rule,[,rule]+> Indicate which rules to ignore completely.",
136-
" --version Outputs the current version number."
152+
" --help Displays this information.",
153+
" --format=<format> Indicate which format to use for output.",
154+
" --list-rules Outputs all of the rules available.",
155+
" --quiet Only output when errors are present.",
156+
" --errors=<rule[,rule]+> Indicate which rules to include as errors.",
157+
" --warnings=<rule[,rule]+> Indicate which rules to include as warnings.",
158+
" --ignore=<rule,[,rule]+> Indicate which rules to ignore completely.",
159+
" --exclude-files=<file,[,file]+> Indicate which files to exclude from being linted.",
160+
" --version Outputs the current version number."
137161
].join("\n") + "\n");
138162
}
139163

@@ -143,56 +167,58 @@ function cli(api){
143167
* @param options {Object} options object
144168
* @return {Number} exit code
145169
*/
146-
function processFiles(files, options){
170+
function processFiles(fileArray, options){
147171
var exitCode = 0,
148172
formatId = options.format || "text",
149173
formatter,
174+
files = filterFiles(fileArray,options),
150175
output;
151-
176+
152177
if (!files.length) {
153178
api.print("csslint: No files specified.");
154179
exitCode = 1;
155180
} else {
156181
if (!CSSLint.hasFormat(formatId)){
157182
api.print("csslint: Unknown format '" + formatId + "'. Cannot proceed.");
158-
exitCode = 1;
183+
exitCode = 1;
159184
} else {
160185
formatter = CSSLint.getFormatter(formatId);
161-
186+
162187
output = formatter.startFormat();
163188
if (output){
164189
api.print(output);
165190
}
166191

192+
167193
files.forEach(function(file){
168194
if (exitCode === 0) {
169195
exitCode = processFile(file,options);
170196
} else {
171197
processFile(file,options);
172198
}
173199
});
174-
200+
175201
output = formatter.endFormat();
176202
if (output){
177203
api.print(output);
178204
}
179205
}
180206
}
181207
return exitCode;
182-
}
183-
184-
208+
}
209+
210+
185211
function processArguments(args, options) {
186212
var arg = args.shift(),
187213
argName,
188214
parts,
189215
files = [];
190-
216+
191217
while(arg){
192218
if (arg.indexOf("--") === 0){
193219
argName = arg.substring(2);
194220
options[argName] = true;
195-
221+
196222
if (argName.indexOf("=") > -1){
197223
parts = argName.split("=");
198224
options[parts[0]] = parts[1];
@@ -201,7 +227,7 @@ function cli(api){
201227
}
202228

203229
} else {
204-
230+
205231
//see if it's a directory or a file
206232
if (api.isDirectory(arg)){
207233
files = files.concat(api.getFiles(arg));
@@ -211,24 +237,24 @@ function cli(api){
211237
}
212238
arg = args.shift();
213239
}
214-
240+
215241
options.files = files;
216242
return options;
217243
}
218-
244+
219245
function readConfigFile(options) {
220246
var data = api.readFile(api.getFullPath(".csslintrc"));
221-
if (data) {
247+
if (data) {
222248
options = processArguments(data.split(/[\s\n\r]+/m), options);
223249
api.print("ignore = " + options.ignore);
224250
api.print("errors = " + options.errors);
225251
api.print("warnings = " + options.warnings);
226252
}
227-
253+
228254
return options;
229255
}
230-
231-
256+
257+
232258

233259
//-----------------------------------------------------------------------------
234260
// Process command line
@@ -237,10 +263,10 @@ function cli(api){
237263
var args = api.args,
238264
argCount = args.length,
239265
options = {};
240-
266+
241267
// first look for config file .csslintrc
242-
options = readConfigFile(options);
243-
268+
options = readConfigFile(options);
269+
244270
// Command line arguments override config file
245271
options = processArguments(args, options);
246272

0 commit comments

Comments
 (0)