stylish.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. 'use strict';
  2. var chalk = require('chalk');
  3. var table = require('text-table');
  4. var logSymbols = require('log-symbols');
  5. var stringLength = require('string-length');
  6. function pluralize(str, count) {
  7. return str + (count === 1 ? '' : 's');
  8. }
  9. module.exports = {
  10. reporter: function (result, config, options) {
  11. var total = result.length;
  12. var ret = '';
  13. var headers = [];
  14. var prevfile;
  15. var errorCount = 0;
  16. var warningCount = 0;
  17. options = options || {};
  18. ret += table(result.map(function (el, i) {
  19. var err = el.error;
  20. // E: Error, W: Warning, I: Info
  21. var isError = err.code && err.code[0] === 'E';
  22. var line = [
  23. '',
  24. chalk.gray('line ' + err.line),
  25. chalk.gray('col ' + err.character),
  26. isError ? chalk.red(err.reason) : chalk.blue(err.reason)
  27. ];
  28. if (el.file !== prevfile) {
  29. headers[i] = el.file;
  30. }
  31. if (options.verbose) {
  32. line.push(chalk.gray('(' + err.code + ')'));
  33. }
  34. if (isError) {
  35. errorCount++;
  36. } else {
  37. warningCount++;
  38. }
  39. prevfile = el.file;
  40. return line;
  41. }), {
  42. stringLength: stringLength
  43. }).split('\n').map(function (el, i) {
  44. return headers[i] ? '\n' + chalk.underline(headers[i]) + '\n' + el : el;
  45. }).join('\n') + '\n\n';
  46. if (total > 0) {
  47. if (errorCount > 0) {
  48. ret += ' ' + logSymbols.error + ' ' + errorCount + pluralize(' error', errorCount) + (warningCount > 0 ? '\n' : '');
  49. }
  50. ret += ' ' + logSymbols.warning + ' ' + warningCount + pluralize(' warning', total);
  51. } else {
  52. ret += ' ' + logSymbols.success + ' No problems';
  53. ret = '\n' + ret.trim();
  54. }
  55. console.log(ret + '\n');
  56. }
  57. };