forked from peterkeating/grunt-csscss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsscss.js
More file actions
188 lines (156 loc) · 4.55 KB
/
csscss.js
File metadata and controls
188 lines (156 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* grunt-csscss
*/
'use strict';
module.exports = function(grunt) {
grunt.registerMultiTask('csscss', 'CSSCSS redundancy analyzer.', function() {
/**
* Retrieves defined options.
*/
var options = this.options();
grunt.verbose.writeflags(options, 'Options');
/**
* Flag for whether any duplicates were found by CSSCSS.
*/
var hasDuplicates = false;
var done = this.async();
var args = [];
/**
* Outputs the rules that have been matched.
*/
if (options.verbose) {
args.push('-v');
}
/**
* Flag indicating whether the output should be colorized. This is true by
* default.
*/
if (options.colorize === false) {
args.push('--no-color');
}
/**
* Enables Compass extensions when parsing Sass files. This argument is not
* set if the compassConfig property has been defined.
*/
if (options.compass && !options.compassConfig) {
args.push('--compass');
}
/**
* Enables Compass extensions when parsing Sass files and specifies the path
* to the config file.
* The compassConfig option is deprecated and will be removed in a future release.
* The require option should be used instead.
*/
if (options.compassConfig && !options.require) {
args.push('--compass');
args.push('--require');
args.push(options.compassConfig);
grunt.log.writeln('WARNING: compassConfig is DEPRECATED, please use the "require" option.');
}
/**
* Enables Compass extensions when parsing Sass files and specifies the path
* to the config file.
*/
if (options.require) {
args.push('--compass');
args.push('--require');
args.push(options.require);
}
/**
* Returns analysis in JSON format.
*/
if (options.outputJson) {
args.push('-j');
}
/**
* Sets the minimum number of rules before a match is found.
*/
if (options.minMatch) {
args.push('-n');
args.push(options.minMatch);
}
/**
* Sets which properties should be ignored.
*/
if (options.ignoreProperties) {
args.push('--ignore-properties');
args.push(options.ignoreProperties);
}
/**
* Sets whether Sass mixins should be ignored.
*/
if (options.ignoreSassMixins) {
args.push('--ignore-sass-mixins');
}
/**
* Sets which selectors should be ignored.
*/
if (options.ignoreSelectors) {
args.push('--ignore-selectors');
args.push(options.ignoreSelectors);
}
/**
* Sets whether parser errors should be outputted.
*/
if (options.showParserErrors) {
args.push('--show-parser-errors');
}
/**
* Sets whether shorthand should be matched.
*/
if (options.shorthand === false) {
args.push('--no-match-shorthand');
}
grunt.util.async.forEachSeries(this.data.src, function(f, next) {
/**
* adds the file path as the final argument, this goes into a new array so
* the file doesn't get used in the next iteration.
*/
var cmdArgs = args.concat([f]);
/**
* Outputs the file that is being analysed.
*/
grunt.log.writeln(f);
grunt.verbose.writeln('csscss ' + cmdArgs.join(' '));
/**
* Executes the csscss command.
*/
var child = grunt.util.spawn({
cmd: 'csscss',
args: cmdArgs
}, function(error, result, code) {
if (code === 127) {
return grunt.warn('Ruby and csscss have to be installed and in your PATH for this task to run.');
}
next(error);
});
/**
* displays the output and error streams via the parent process.
*/
child.stdout.on('data', function(buf) {
var output = String(buf);
grunt.log.writeln(output);
/**
* When outputting JSON from CSSCSS an empty array will be outputted, this
* should be ignored and shouldn't cause the grunt task to fail if no other
* duplicates are found.
*/
if (!(options.outputJson && JSON.parse(output).length === 0)) {
hasDuplicates = true;
}
});
child.stderr.on('data', function(buf) {
grunt.log.writeln(String(buf));
});
}, function () {
/**
* If instructed to fail when a match happens and matches found lets fail
* the grunt build.
*/
if (options.failWhenDuplicates && hasDuplicates) {
grunt.fail.warn("Failed due to matches found by CSSCSS.");
}
done();
});
});
};