22bbcf7702114ab302de7f8f2951ce3e67c487e408db0a1181fb63465d974638752c3a59a3f3b4809c21289d25330aefbc1b91a0f31a6a0e61fcf7cb19bba0-exec 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # postcss-message-helpers [![Build Status](https://travis-ci.org/MoOx/postcss-message-helpers.png)](https://travis-ci.org/MoOx/postcss-message-helpers)
  2. > [PostCSS](https://github.com/postcss/postcss) helpers to throw or output GNU style messages.
  3. This modules offers you some function to throw or just output messages with [GNU style](https://www.gnu.org/prep/standards/html_node/Errors.html): `sourcefile:lineno:column: message`
  4. ## Installation
  5. ```console
  6. $ npm install postcss-message-helpers
  7. ```
  8. ```js
  9. var messageHelpers = require("postcss-message-helpers")
  10. ```
  11. ## Usage
  12. ### `var fnValue = messageHelpers.try(fn, source)`
  13. Execute `fn` an return the value.
  14. If an exception is thrown during the process, the exception will be catched, enhanced from source & re-throw.
  15. ### `var sourceMessage = messageHelpers.message(message, source)`
  16. Returns a message like `sourcefile:lineno:column: message`.
  17. `source` should be a postcss source object from a node.
  18. ### `var source = messageHelpers.source(source)`
  19. Returns `sourcefile:lineno:column` for a given `source` postcss object.
  20. ### Example
  21. ```js
  22. // dependencies
  23. var fs = require("fs")
  24. var postcss = require("postcss")
  25. var messageHelpers = require("postcss-message-helpers")
  26. // css to be processed
  27. var css = fs.readFileSync("input.css", "utf8")
  28. // process css
  29. var output = postcss()
  30. .use(function(styles) {
  31. styles.eachDecl(function transformDecl(decl) {
  32. // will catch, adjust error stack, line, column & message (gnu style) then re-throw
  33. messageHelpers.try(function IwillThrow() {
  34. if (decl.value.indexOf("error(") > -1) {
  35. throw new Error("error detected: " + decl.value)
  36. }
  37. }, decl.source)
  38. // will output a gnu style warning
  39. if (decl.value.indexOf("warning(") > -1) {
  40. console.warning(messageHelpers.message("warning: " + decl.value, decl.source))
  41. }
  42. })
  43. })
  44. .process(css)
  45. .css
  46. ```
  47. Checkout [tests](test) for more examples.
  48. ---
  49. ## Contributing
  50. Work on a branch, install dev-dependencies, respect coding style & run tests before submitting a bug fix or a feature.
  51. $ git clone https://github.com/MoOx/postcss-message-helpers.git
  52. $ git checkout -b patch-1
  53. $ npm install
  54. $ npm test
  55. ## [Changelog](CHANGELOG.md)
  56. ## [License](LICENSE)