-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
processResult.js
59 lines (47 loc) · 1.49 KB
/
processResult.js
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
'use strict';
var chalk = require('chalk');
var path = require('path');
module.exports = function(postcssResult, options) {
options = options || {};
options.plugins = options.plugins || [];
var warnings = postcssResult.warnings();
if (!warnings.length) return undefined;
var output = '\n# postcss-log-warnings\n\n';
output += chalk.bold.underline(logFrom(postcssResult.root.source.input.from)) + '\n';
var filteredWarnings = warnings.filter(shouldLog);
filteredWarnings.forEach(function(w) {
output += warningToString(w) + '\n';
});
// Unless user has set `keepWarnings` option,
// clear all these warnings that were just stringified
if (!options.keepWarnings) {
postcssResult.messages = postcssResult.messages.filter(function(message) {
return message.type !== 'warning';
});
}
return output;
function shouldLog(warning) {
if (options.plugins.length && options.plugins.indexOf(warning.plugin) === -1) {
return false;
}
return true;
}
function warningToString(warning) {
var str = '';
if (warning.node && warning.node.type !== 'root') {
str += chalk.bold(
warning.node.source.start.line + ':' +
warning.node.source.start.column + '\t'
);
}
str += warning.text;
if (options.plugins.length !== 1) {
str += chalk.yellow(' [' + warning.plugin + ']');
}
return str;
}
function logFrom(f) {
if (f.charAt(0) === '<') return f;
return path.relative(process.cwd(), f);
}
};