postcss-log-warnings
Advanced tools
Comparing version 0.1.3 to 0.2.0
# Changelog | ||
## v0.2.0 | ||
- Change the print format slightly. | ||
- Clear warnings from postcssResult.messages after logging them (configurable with option `keepWarnings`). | ||
## v0.1.3 | ||
@@ -4,0 +8,0 @@ - Add message when exiting process with code 1 to clarify reason. |
10
index.js
@@ -9,10 +9,12 @@ 'use strict'; | ||
module.exports = postcss.plugin('postcss-log-warnings', function(opts) { | ||
module.exports = postcss.plugin('postcss-log-warnings', function(options) { | ||
options = options || {}; | ||
return function(css, result) { | ||
var p = new Promise(function(resolve) { | ||
processResult(result, resolve, opts); | ||
processResult(result, resolve, options); | ||
}); | ||
p.then(function(r) { | ||
if (r) console.log(r); | ||
if (opts.throwError) exitCode = 1; | ||
if (options.throwError) exitCode = 1; | ||
}); | ||
@@ -26,4 +28,4 @@ return p; | ||
console.log(chalk.red.bold('\n** PostCSS warnings were logged **')); | ||
console.log(chalk.red.bold('\n** postcss-log-warnings: warnings were found **')); | ||
process.exit(exitCode); | ||
}); |
@@ -6,5 +6,5 @@ 'use strict'; | ||
module.exports = function(postcssResult, cb, opts) { | ||
opts = opts || {}; | ||
opts.plugins = opts.plugins || []; | ||
module.exports = function(postcssResult, cb, options) { | ||
options = options || {}; | ||
options.plugins = options.plugins || []; | ||
@@ -18,5 +18,5 @@ var warnings = postcssResult.warnings(); | ||
var output = '\n'; | ||
var output = '\n# postcss-log-warnings\n\n'; | ||
output += chalk.red.underline(logFrom(postcssResult.root.source.input.from)) + '\n'; | ||
output += chalk.bold.underline(logFrom(postcssResult.root.source.input.from)) + '\n'; | ||
@@ -28,6 +28,14 @@ var filteredWarnings = warnings.filter(shouldLog); | ||
// 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'; | ||
}); | ||
} | ||
cb(output); | ||
function shouldLog(warning) { | ||
if (opts.plugins.length && opts.plugins.indexOf(warning.plugin) === -1) { | ||
if (options.plugins.length && options.plugins.indexOf(warning.plugin) === -1) { | ||
return false; | ||
@@ -46,6 +54,6 @@ } | ||
} | ||
if (opts.plugins.length !== 1) { | ||
str += chalk.yellow('[' + warning.plugin + '] '); | ||
str += warning.text; | ||
if (options.plugins.length !== 1) { | ||
str += chalk.yellow(' [' + warning.plugin + ']'); | ||
} | ||
str += warning.text; | ||
return str; | ||
@@ -52,0 +60,0 @@ } |
{ | ||
"name": "postcss-log-warnings", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"description": "Log PostCSS warnings in the console", | ||
@@ -27,2 +27,3 @@ "main": "index.js", | ||
"eslint": "0.20.0", | ||
"lodash": "3.8.0", | ||
"tape": "4.0.0" | ||
@@ -29,0 +30,0 @@ }, |
@@ -41,11 +41,18 @@ # postcss-log-warnings [![Build Status](https://travis-ci.org/davidtheclark/postcss-log-warnings.svg?branch=master)](https://travis-ci.org/davidtheclark/postcss-log-warnings) | ||
*By default, this plugin will clear the warnings after it logs them*. Otherwise, your other plugins or your PostCSS runner might re-print the same warnings, causing some confusion. This can be changed by setting the option `{ keepWarnings: true }`. | ||
### Options | ||
- **keepWarnings** (boolean, default = false) | ||
If true, the plugin will *not* clear the warnings after it logs them (by default, it will clear them). Other plugins will then have access to these warnings and might re-print them. | ||
- **plugins** (array of strings, default = []) | ||
If empty, `logWarnings` will log every warning, regardless of which plugin registered it. | ||
If empty, the plugin will log every warning, regardless of which plugin registered it. | ||
To limit output, name the plugins whose warnings you would like to see. | ||
For example, `{ plugins: ['postcss-bem-linter'] }` will only log warnings from the `postcss-bem-linter` plugin. | ||
- **throwError** (boolean, default = `false`) | ||
If `true`, after `logWarnings` logs your warnings it will exit the process with code 1 if it found any warnings. | ||
If `true`, after the plugin logs your warnings it will exit the process with code 1 if it found any warnings. |
@@ -6,17 +6,23 @@ 'use strict'; | ||
var chalk = require('chalk'); | ||
var _ = require('lodash'); | ||
var path = require('path'); | ||
var mockSimpleResult = { | ||
warnings: function() { | ||
return [{ | ||
plugin: 'foo', | ||
text: 'foo warning' | ||
}, { | ||
plugin: 'bar', | ||
text: 'bar warning' | ||
}, { | ||
plugin: 'baz', | ||
text: 'baz warning' | ||
}]; | ||
}, | ||
messages: [{ | ||
type: 'warning', | ||
plugin: 'foo', | ||
text: 'foo warning' | ||
}, { | ||
type: 'warning', | ||
plugin: 'bar', | ||
text: 'bar warning' | ||
}, { | ||
type: 'warning', | ||
plugin: 'baz', | ||
text: 'baz warning' | ||
}, { | ||
type: 'error', | ||
plugin: 'baz', | ||
text: 'baz error' | ||
}], | ||
root: { | ||
@@ -31,17 +37,25 @@ source: { | ||
var simpleOutput = '\n<input css 1>' + | ||
'\n[foo] foo warning' + | ||
'\n[bar] bar warning' + | ||
'\n[baz] baz warning\n'; | ||
mockSimpleResult.warnings = function() { | ||
return this.messages.filter(function(m) { | ||
return m.type === 'warning'; | ||
}); | ||
}; | ||
var simpleOutputNoBar = '\n<input css 1>' + | ||
'\n[foo] foo warning' + | ||
'\n[baz] baz warning\n'; | ||
var simpleOutput = '\n# postcss-log-warnings\n' + | ||
'\n<input css 1>' + | ||
'\nfoo warning [foo]' + | ||
'\nbar warning [bar]' + | ||
'\nbaz warning [baz]\n'; | ||
var simpleOutputNoBar = '\n# postcss-log-warnings\n' + | ||
'\n<input css 1>' + | ||
'\nfoo warning [foo]' + | ||
'\nbaz warning [baz]\n'; | ||
test('processResult with simple mock', function(t) { | ||
t.plan(2); | ||
processResult(mockSimpleResult, function(r) { | ||
processResult(_.cloneDeep(mockSimpleResult), function(r) { | ||
t.equal(chalk.stripColor(r), simpleOutput, 'basic'); | ||
}); | ||
processResult(mockSimpleResult, function(r) { | ||
processResult(_.cloneDeep(mockSimpleResult), function(r) { | ||
t.equal(chalk.stripColor(r), simpleOutputNoBar, 'excluding bar'); | ||
@@ -51,28 +65,47 @@ }, { plugins: ['foo', 'baz']}); | ||
test('clearing warnings from result.messages', function(t) { | ||
t.plan(3); | ||
var resultA = _.cloneDeep(mockSimpleResult); | ||
var resultB = _.cloneDeep(mockSimpleResult); | ||
t.equal(resultA.warnings().length, 3, 'initial length accurate'); | ||
processResult(resultA, function() { | ||
t.equal(resultA.warnings().length, 0, 'warnings are cleared'); | ||
}); | ||
processResult(resultB, function() { | ||
t.deepEqual(mockSimpleResult.messages, resultB.messages, | ||
'keepWarnings option preserves messages exactly'); | ||
}, { keepWarnings: true }); | ||
}); | ||
var mockComplexResult = { | ||
warnings: function() { | ||
return [{ | ||
plugin: 'foo', | ||
text: 'foo warning', | ||
node: { | ||
source: { | ||
start: { | ||
line: 3, | ||
column: 5 | ||
} | ||
messages: [{ | ||
type: 'warning', | ||
plugin: 'foo', | ||
text: 'foo warning', | ||
node: { | ||
source: { | ||
start: { | ||
line: 3, | ||
column: 5 | ||
} | ||
} | ||
}, { | ||
plugin: 'bar', | ||
text: 'bar warning', | ||
node: { | ||
source: { | ||
start: { | ||
line: 1, | ||
column: 99 | ||
} | ||
} | ||
}, { | ||
type: 'warning', | ||
plugin: 'bar', | ||
text: 'bar warning', | ||
node: { | ||
source: { | ||
start: { | ||
line: 1, | ||
column: 99 | ||
} | ||
} | ||
}]; | ||
}, | ||
} | ||
}, { | ||
type: 'error', | ||
plugin: 'baz', | ||
text: 'baz error' | ||
}], | ||
root: { | ||
@@ -87,7 +120,15 @@ source: { | ||
var complexOutput = '\nstyle/rainbows/horses.css' + | ||
'\n3:5\t[foo] foo warning' + | ||
'\n1:99\t[bar] bar warning\n'; | ||
mockComplexResult.warnings = function() { | ||
return this.messages.filter(function(m) { | ||
return m.type === 'warning'; | ||
}); | ||
}; | ||
var complexOutputNoBar = '\nstyle/rainbows/horses.css' + | ||
var complexOutput = '\n# postcss-log-warnings\n' + | ||
'\nstyle/rainbows/horses.css' + | ||
'\n3:5\tfoo warning [foo]' + | ||
'\n1:99\tbar warning [bar]\n'; | ||
var complexOutputNoBar = '\n# postcss-log-warnings\n' + | ||
'\nstyle/rainbows/horses.css' + | ||
'\n3:5\tfoo warning\n'; | ||
@@ -98,8 +139,8 @@ | ||
t.plan(2); | ||
processResult(mockComplexResult, function(r) { | ||
processResult(_.cloneDeep(mockComplexResult), function(r) { | ||
t.equal(chalk.stripColor(r), complexOutput, 'basic'); | ||
}); | ||
processResult(mockComplexResult, function(r) { | ||
processResult(_.cloneDeep(mockComplexResult), function(r) { | ||
t.equal(chalk.stripColor(r), complexOutputNoBar, 'excluding bar'); | ||
}, { plugins: ['foo']}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
245
58
35255
3