diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2024-08-30 17:21:18 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2024-08-30 17:21:18 +0800 |
| commit | efbc2ffe6a1ee5ec38a4396305561d6515d0943e (patch) | |
| tree | 2b4c94f18f2c792dfda813050755d5c0273fae22 /node_modules/postcss-reporter/lib | |
| parent | 43acceb75957c880a5ba5a31aae221394a36d5f6 (diff) | |
node fixes, updates
Diffstat (limited to 'node_modules/postcss-reporter/lib')
| -rw-r--r-- | node_modules/postcss-reporter/lib/formatter.js | 75 | ||||
| -rw-r--r-- | node_modules/postcss-reporter/lib/reporter.js | 144 | ||||
| -rw-r--r-- | node_modules/postcss-reporter/lib/util.js | 21 |
3 files changed, 147 insertions, 93 deletions
diff --git a/node_modules/postcss-reporter/lib/formatter.js b/node_modules/postcss-reporter/lib/formatter.js index b5719b8f..300efad7 100644 --- a/node_modules/postcss-reporter/lib/formatter.js +++ b/node_modules/postcss-reporter/lib/formatter.js @@ -1,45 +1,54 @@ -var chalk = require('chalk'); +var pico = require('picocolors'); var path = require('path'); -var symbols = require('log-symbols'); -var _ = require('lodash'); +var firstBy = require('thenby'); var util = require('./util'); -module.exports = function(opts) { +function createSortFunction(positionless, sortByPosition) { + var positionValue = 0 + + if (positionless === 'any') { positionValue = 1; } + if (positionless === 'first') { positionValue = 2; } + if (positionless === 'last') { positionValue = 0; } + + var sortFunction = firstBy((m) => { + if (!m.line) return 1; + return positionValue; + }) + + if (sortByPosition) { + sortFunction = sortFunction.thenBy('line').thenBy('column'); + } + + return sortFunction; +} + +module.exports = function (opts) { var options = opts || {}; - var sortByPosition = (typeof options.sortByPosition !== 'undefined') ? options.sortByPosition : true; + var sortByPosition = + typeof options.sortByPosition !== 'undefined' + ? options.sortByPosition + : true; var positionless = options.positionless || 'first'; - return function(input) { - var messages = input.messages; + var sortFunction = createSortFunction(positionless, sortByPosition); + + return function (input) { + var messages = input.messages.filter(function (message) { + return typeof message.text === 'string'; + }); var source = input.source; if (!messages.length) return ''; - var orderedMessages = _.sortBy( - messages, - function(m) { - if (!m.line) return 1; - if (positionless === 'any') return 1; - if (positionless === 'first') return 2; - if (positionless === 'last') return 0; - }, - function(m) { - if (!sortByPosition) return 1; - return m.line; - }, - function(m) { - if (!sortByPosition) return 1; - return m.column; - } - ); + var orderedMessages = messages.sort(sortFunction); var output = '\n'; if (source) { - output += chalk.bold.underline(logFrom(source)) + '\n'; + output += pico.bold(pico.underline(logFrom(source))) + '\n'; } - orderedMessages.forEach(function(w) { + orderedMessages.forEach(function (w) { output += messageToString(w) + '\n'; }); @@ -50,24 +59,28 @@ module.exports = function(opts) { var str = ''; if (location.line) { - str += chalk.bold(location.line); + str += pico.bold(location.line); } if (location.column) { - str += chalk.bold(':' + location.column) + str += pico.bold(':' + location.column); } if (location.line || location.column) { str += '\t'; } - if (!options.noIcon && message.type === 'warning') { - str += chalk.yellow(symbols.warning + ' '); + if (!options.noIcon) { + if (message.type === 'warning') { + str += pico.yellow(util.warningSymbol + ' '); + } else if (message.type === 'error') { + str += pico.red(util.errorSymbol + ' '); + } } str += message.text; if (!options.noPlugin) { - str += chalk.yellow(' [' + message.plugin + ']'); + str += pico.yellow(' [' + message.plugin + ']'); } return str; } diff --git a/node_modules/postcss-reporter/lib/reporter.js b/node_modules/postcss-reporter/lib/reporter.js index 70781fba..ae709c7d 100644 --- a/node_modules/postcss-reporter/lib/reporter.js +++ b/node_modules/postcss-reporter/lib/reporter.js @@ -1,81 +1,111 @@ -var chalk = require('chalk'); -var _ = require('lodash'); var defaultFormatter = require('./formatter'); +var pico = require('picocolors'); var util = require('./util'); -module.exports = function(opts) { - var options = opts || {}; - - var formatter = options.formatter || defaultFormatter({ - sortByPosition: (typeof options.sortByPosition !== 'undefined') ? options.sortByPosition : true, - positionless: options.positionless || 'first', - noIcon: options.noIcon, - noPlugin: options.noPlugin, - }); +module.exports = function (opts = {}) { + var formatter = + opts.formatter || + defaultFormatter({ + noIcon: opts.noIcon, + noPlugin: opts.noPlugin, + }); var pluginFilter; - if (!options.plugins) { + if (!opts.plugins) { // Every plugin - pluginFilter = function() { return true; }; - } else if (options.plugins.every(function(plugin) { return plugin[0] === '!'; })) { - // Blacklist - pluginFilter = function(message) { - return options.plugins.indexOf('!' + message.plugin) === -1; + pluginFilter = function () { + return true; + }; + } else if ( + opts.plugins.every(function (plugin) { + return plugin[0] === '!'; + }) + ) { + // Deny list + pluginFilter = function (message) { + return opts.plugins.indexOf('!' + message.plugin) === -1; }; } else { - // Whitelist - pluginFilter = function(message) { - return options.plugins.indexOf(message.plugin) !== -1; + // Allow list + pluginFilter = function (message) { + return opts.plugins.indexOf(message.plugin) !== -1; }; } - var messageFilter = options.filter || function(message) { return (message.type === 'warning'); }; + var messageFilter = opts.filter || ((message) => message.type === 'warning' || message.type === 'error'); - return function(css, result) { - var messagesToLog = result.messages - .filter(pluginFilter) - .filter(messageFilter); + return { + postcssPlugin: 'postcss-reporter', + OnceExit(css, { result }) { + var messagesToLog = result.messages + .filter(pluginFilter) + .filter(messageFilter); - var resultSource = (!result.root.source) ? '' - : result.root.source.input.file || result.root.source.input.id + var resultSource = !result.root.source + ? '' + : result.root.source.input.file || result.root.source.input.id; - var sourceGroupedMessages = _.groupBy(messagesToLog, function(message) { - return util.getLocation(message).file || resultSource; - }); + let errorCount = 0; + let warningCount = 0; - var report = ''; - _.forOwn(sourceGroupedMessages, function(messages, source) { - report += formatter({ - messages: messages, - source: source, - }); - }); + var sourceGroupedMessages = messagesToLog.reduce((grouped, message) => { + const key = util.getLocation(message).file || resultSource; + + if (!grouped.hasOwnProperty(key)) { + grouped[key] = []; + } + + if (message.type === 'error') { + errorCount++; + } else if (message.type === 'warning') { + warningCount++; + } + + grouped[key].push(message); + + return grouped; + }, {}); + + var report = ''; + for (const source in sourceGroupedMessages) { + if (sourceGroupedMessages.hasOwnProperty(source)) { + report += formatter({ + messages: sourceGroupedMessages[source], + source: source, + }); + } + } + + if (opts.clearReportedMessages) { + result.messages = result.messages.filter(message => !messagesToLog.includes(message)); + } - if (options.clearReportedMessages) { - result.messages = _.difference(result.messages, messagesToLog); - } + if (opts.clearAllMessages) { + var messagesToClear = result.messages.filter(pluginFilter); + result.messages = result.messages.filter(message => !messagesToClear.includes(message)); + } - if (options.clearAllMessages) { - var messagesToClear = result.messages.filter(pluginFilter); - result.messages = _.difference(result.messages, messagesToClear); - } + if (!report) return; + const summaryColor = errorCount > 0 ? 'red' : 'yellow'; + const summarySymbol = errorCount > 0 ? util.errorSymbol : util.warningSymbol; + const summary = `${summarySymbol} ${messagesToLog.length} ${util.plur('problem', messagesToLog.length)} (${errorCount} ${util.plur('error')}, ${warningCount} ${util.plur('warning')})` - if (!report) return; + report += `\n ${pico[summaryColor](pico.bold(summary))}\n`; - console.log(report); + console.log(report); - if (options.throwError && shouldThrowError()) { - throw new Error(chalk.red.bold('\n** postcss-reporter: warnings or errors were found **')); - } + if (shouldThrowError()) { + throw new Error( + pico.red( + pico.bold('\n** postcss-reporter: warnings or errors were found **') + ) + ); + } - function shouldThrowError() { - return ( - messagesToLog.length - && messagesToLog.some(function(message) { - return message.type === 'warning' || message.type === 'error'; - }) - ); - } + function shouldThrowError() { + return opts.throwError || errorCount > 0; + } + }, }; }; diff --git a/node_modules/postcss-reporter/lib/util.js b/node_modules/postcss-reporter/lib/util.js index 76c649bc..54334913 100644 --- a/node_modules/postcss-reporter/lib/util.js +++ b/node_modules/postcss-reporter/lib/util.js @@ -1,6 +1,9 @@ -var _ = require('lodash'); +var supportsLargeCharset = + process.platform !== 'win32' || + process.env.CI || + process.env.TERM === 'xterm-256color'; -exports.getLocation = function(message) { +exports.getLocation = function (message) { var messageNode = message.node; var location = { @@ -8,13 +11,21 @@ exports.getLocation = function(message) { column: message.column, }; - var messageInput = _.get(messageNode, 'source.input'); + var messageInput = messageNode && messageNode.source && messageNode.source.input; if (!messageInput) return location; - var originLocation = messageInput.origin && messageInput.origin(message.line, message.column) - if (originLocation) return originLocation + var originLocation = + messageInput.origin && messageInput.origin(message.line, message.column); + if (originLocation) return originLocation; location.file = messageInput.file || messageInput.id; return location; }; + +exports.plur = function plur(word, count) { + return (count === 1 ? word : `${word}s`); +} + +exports.warningSymbol = supportsLargeCharset ? '⚠' : '!!'; +exports.errorSymbol = supportsLargeCharset ? '✖' : 'xx'; |
