|
4 | 4 |
|
5 | 5 | var gutil = require('gulp-util'); |
6 | 6 | var c = gutil.colors; |
| 7 | +var error = gutil.PluginError; |
7 | 8 | var es = require('event-stream'); |
8 | 9 | var fs = require('fs'); |
9 | 10 | var csslint = require('csslint').CSSLint; |
| 11 | +var RcLoader = require('rcloader'); |
10 | 12 |
|
11 | 13 | var formatOutput = function(report, file, options) { |
12 | 14 | if (!report.messages.length) { |
@@ -40,41 +42,39 @@ var cssLintPlugin = function(options) { |
40 | 42 |
|
41 | 43 | var ruleset = {}; |
42 | 44 |
|
43 | | - // Read CSSLint options from a specified csslintrc file. |
44 | | - if (typeof options === 'string') { |
45 | | - // Don't catch readFile errors, let them bubble up |
46 | | - var externalOptions = fs.readFileSync('./'+options); |
47 | | - |
48 | | - try { |
49 | | - options = JSON.parse(externalOptions); |
50 | | - } |
51 | | - catch(err) { |
52 | | - throw new Error('Error parsing csslintrc: '+err); |
53 | | - } |
54 | | - } |
| 45 | + var rcLoader = new RcLoader('.csslintrc', options, { loader: 'async' }); |
55 | 46 |
|
56 | 47 | // Build a list of all available rules |
57 | 48 | csslint.getRules().forEach(function(rule) { |
58 | 49 | ruleset[rule.id] = 1; |
59 | 50 | }); |
60 | 51 |
|
61 | | - for (var rule in options) { |
62 | | - if (!options[rule]) { |
63 | | - // Remove rules that are turned off |
64 | | - delete ruleset[rule]; |
65 | | - } |
66 | | - else { |
67 | | - ruleset[rule] = options[rule]; |
68 | | - } |
69 | | - } |
70 | | - |
71 | 52 | return es.map(function(file, cb) { |
72 | | - var report = csslint.verify(String(file.contents), ruleset); |
| 53 | + if (file.isNull()) return cb(null, file); // pass along |
| 54 | + if (file.isStream()) return cb(new error('gulp-csslint: Streaming not supported')); |
| 55 | + |
| 56 | + rcLoader.for(file.path, function (err, opts) { |
| 57 | + if (err) return cb(err); |
| 58 | + |
| 59 | + var str = file.contents.toString('utf8'); |
| 60 | + |
| 61 | + for (var rule in opts) { |
| 62 | + if (!opts[rule]) { |
| 63 | + // Remove rules that are turned off |
| 64 | + delete ruleset[rule]; |
| 65 | + } |
| 66 | + else { |
| 67 | + ruleset[rule] = opts[rule]; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + var report = csslint.verify(str, ruleset); |
73 | 72 |
|
74 | | - // send status down-stream |
75 | | - file.csslint = formatOutput(report, file, options); |
| 73 | + // send status down-stream |
| 74 | + file.csslint = formatOutput(report, file, ruleset); |
76 | 75 |
|
77 | | - cb(null, file); |
| 76 | + cb(null, file); |
| 77 | + }); |
78 | 78 | }); |
79 | 79 | }; |
80 | 80 |
|
|
0 commit comments