Skip to content

Commit 517ffe4

Browse files
committed
cli config path #449
1 parent 5e2bd1d commit 517ffe4

File tree

1 file changed

+51
-17
lines changed

1 file changed

+51
-17
lines changed

src/cli/common.js

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ function cli(api){
1616
"warnings" : { "format" : "<rule[,rule]+>", "description" : "Indicate which rules to include as warnings."},
1717
"ignore" : { "format" : "<rule[,rule]+>", "description" : "Indicate which rules to ignore completely."},
1818
"exclude-list": { "format" : "<file|dir[,file|dir]+>", "description" : "Indicate which files/directories to exclude from being linted."},
19+
"config" : { "format" : "<file>", "description" : "Reads csslint options from specified file."},
1920
"version" : { "format" : "", "description" : "Outputs the current version number."}
2021
};
2122

@@ -250,8 +251,9 @@ function cli(api){
250251
}
251252

252253

253-
function processArguments(args, options) {
254+
function processArguments(args, extend) {
254255
var arg = args.shift(),
256+
options = extend || {},
255257
argName,
256258
parts,
257259
files = [];
@@ -293,9 +295,16 @@ function cli(api){
293295
}
294296
}
295297

296-
function readConfigFile(options) {
297-
var data = api.readFile(api.getFullPath(".csslintrc")),
298-
json;
298+
function readConfigFile(config) {
299+
var csslintrc = config || ".csslintrc",
300+
data = api.readFile(api.getFullPath(csslintrc));
301+
return data;
302+
}
303+
304+
function readConfigData(config) {
305+
var data = readConfigFile(config),
306+
json,
307+
options = {};
299308
if (data) {
300309
if (data.charAt(0) === "{") {
301310
try {
@@ -308,45 +317,70 @@ function cli(api){
308317
}
309318
} catch(e) {}
310319
}
311-
options = processArguments(data.split(/[\s\n\r]+/m), options);
320+
options = processArguments(data.split(/[\s\n\r]+/m));
312321
}
313322

314323
return options;
315324
}
316325

326+
function mergeOptions(/*arguments*/) {
327+
var allOptions = Array.apply(null, arguments).sort(),
328+
allOptionsCount = allOptions.length,
329+
options = allOptions[0],
330+
overrideOptions,
331+
overrideOptionix,
332+
overrideOption;
333+
334+
for (var i = 1; i < allOptionsCount; i += 1) {
335+
overrideOptions = allOptions[i];
336+
337+
for (overrideOptionix in overrideOptions) {
338+
if (overrideOptions.hasOwnProperty(overrideOptionix)) {
339+
overrideOption = overrideOptions[overrideOptionix];
340+
options[overrideOptionix] = overrideOption;
341+
}
342+
}
343+
}
317344

345+
return options;
346+
}
318347

319348
//-----------------------------------------------------------------------------
320349
// Process command line
321350
//-----------------------------------------------------------------------------
322351

323352
var args = api.args,
324353
argCount = args.length,
325-
options = {};
354+
options,
355+
rcOptions,
356+
cliOptions;
326357

327-
// first look for config file .csslintrc
328-
options = readConfigFile(options);
329-
330-
// Command line arguments override config file
331-
options = processArguments(args, options);
358+
// Preprocess command line arguments
359+
cliOptions = processArguments(args);
332360

333-
if (options.help || argCount === 0){
361+
if (cliOptions.help || argCount === 0){
334362
outputHelp();
335363
api.quit(0);
336364
}
337365

338-
// Validate options
339-
validateOptions(options);
340-
341-
if (options.version){
366+
if (cliOptions.version){
342367
api.print("v" + CSSLint.version);
343368
api.quit(0);
344369
}
345370

346-
if (options["list-rules"]){
371+
if (cliOptions["list-rules"]){
347372
printRules();
348373
api.quit(0);
349374
}
350375

376+
// Look for config file
377+
rcOptions = readConfigData(cliOptions.config);
378+
379+
// Command line arguments override config file
380+
options = mergeOptions(rcOptions, cliOptions);
381+
382+
// Validate options
383+
validateOptions(options);
384+
351385
api.quit(processFiles(options.files,options));
352386
}

0 commit comments

Comments
 (0)