Skip to content

Commit

Permalink
Copy postcss-log-warnings files
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtheclark committed Jun 8, 2015
1 parent 982ac3e commit dc0c06a
Show file tree
Hide file tree
Showing 16 changed files with 506 additions and 3 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
@@ -0,0 +1,10 @@
# editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8
1 change: 1 addition & 0 deletions .eslintignore
@@ -0,0 +1 @@
node_modules
23 changes: 23 additions & 0 deletions .eslintrc
@@ -0,0 +1,23 @@
{
"env": {
"node": true
},
"rules": {
"curly": 0,
"radix": 2,
"wrap-iife": 2,
"brace-style": 0,
"comma-style": 2,
"consistent-this": [2, "_that"],
"no-lonely-if": 2,
"no-nested-ternary": 2,
"no-process-exit": 0,
"no-spaced-func": 2,
"no-use-before-define": [2, "nofunc"],
"quotes": [2, "single"],
"space-after-keywords": [2, "always"],
"space-before-blocks": [2, "always"],
"space-in-parens": [2, "never"],
"space-unary-ops": 2
}
}
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
node_modules
*.log
tmp
4 changes: 4 additions & 0 deletions .travis.yml
@@ -0,0 +1,4 @@
language: node_js
node_js:
- "0.12"
- "iojs"
26 changes: 26 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,26 @@
# Changelog

## v0.3.1
- Fix bug causing error if warning is on root node.

## v0.3.0
- Throw error instead of exiting process when `throwError: true`.

## v0.2.3
- Remove async.

## v0.2.0
- Change the print format slightly.
- Clear warnings from postcssResult.messages after logging them (configurable with option `keepWarnings`).

## v0.1.3
- Add message when exiting process with code 1 to clarify reason.

## v0.1.2
- Only log if there is something to log (don't log undefined).

## v0.1.1
- Only exit process if code is non-zero.

## v0.1.0
- First release.
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 PostCSS
Copyright (c) 2015 David Clark

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
67 changes: 65 additions & 2 deletions README.md
@@ -1,2 +1,65 @@
# postcss-console-log-messages
Log PostCSS messages in the console
# postcss-log-warnings [![Build Status](https://travis-ci.org/davidtheclark/postcss-log-warnings.svg?branch=master)](https://travis-ci.org/davidtheclark/postcss-log-warnings)

Log PostCSS warnings in the console.

## Purpose

As of PostCSS 4.1, a single PostCSS process can accumulate warnings from all of the plugins it uses.
Presumably, plugin authors want you to see those warnings.
So this plugin exists to read the accumulated warnings (or warnings from only the plugins you've specified), format them for human legibility, and print them to the console.

## Example Output

![Example](example.png?raw=true)

## Installation

```
npm install postcss-log-warnings
```

## Usage

Add it to your plugin list *after any plugins whose warnings you want to log*, and pass it an object of options.

For example, using [gulp-postcss](https://github.com/w0rm/gulp-postcss):

```js
gulp.task('css', function() {
return gulp.src('./src/*.css')
.pipe(postcss([
bemLinter(),
customProperties(),
calc(),
rejectAllColors(),
logWarnings(myOptions) // <------ ding
]))
.pipe(gulp.dest('./dist'));
});
```

*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 }`.

You can also use this module as a library:

```js
var processResult = require('postcss-log-warnings/lib/processResult');
var warningLog = processResult(postcssResult, options);
```

### 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, 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 the plugin logs your warnings it will throw an error if it found any warnings.
(Not part of the library options.)
Binary file added example.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions index.js
@@ -0,0 +1,21 @@
'use strict';

var postcss = require('postcss');
var chalk = require('chalk');
var processResult = require('./lib/processResult');

module.exports = postcss.plugin('postcss-log-warnings', function(options) {
options = options || {};

return function(css, result) {
var warningLog = processResult(result, options);

if (!warningLog) return;

console.log(warningLog);

if (options.throwError) {
throw new Error(chalk.red.bold('\n** postcss-log-warnings: warnings were found **'));
}
};
});
59 changes: 59 additions & 0 deletions lib/processResult.js
@@ -0,0 +1,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);
}
};
34 changes: 34 additions & 0 deletions package.json
@@ -0,0 +1,34 @@
{
"name": "postcss-log-warnings",
"version": "0.3.1",
"description": "Log PostCSS warnings in the console",
"main": "index.js",
"scripts": {
"lint": "eslint .",
"test": "npm run lint && tape test",
"visual": "node test/visual.js"
},
"repository": {
"type": "git",
"url": "https://github.com/davidtheclark/postcss-log-warnings.git"
},
"author": {
"name": "David Clark",
"email": "david.dave.clark@gmail.com",
"url": "http://davidtheclark.com"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/davidtheclark/postcss-log-warnings/issues"
},
"homepage": "https://github.com/davidtheclark/postcss-log-warnings",
"devDependencies": {
"eslint": "0.20.0",
"lodash": "3.8.0",
"tape": "4.0.0"
},
"dependencies": {
"chalk": "^1.0.0",
"postcss": "^4.1.0"
}
}
8 changes: 8 additions & 0 deletions test/forVisual.css
@@ -0,0 +1,8 @@
/** @define HamSandwich */
.Ham.Sandwich {
color: pink;
}

:root a {
font-weight: bold;
}
3 changes: 3 additions & 0 deletions test/index.js
@@ -0,0 +1,3 @@
'use strict';

require('./processResult');

2 comments on commit dc0c06a

@MoOx
Copy link

@MoOx MoOx commented on dc0c06a Jun 8, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you just transferred this repo instead of making a copy ?

@davidtheclark
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I was unsure that this was the right move, so I wanted to put the files here to start discussion without affecting postcss-log-warning. Since the new plugin will have a different name, won't I need to keep log-warnings where it is and add a deprecation notice, instead of quietly switching locations?

Please sign in to comment.