mirror of https://github.com/thumbsup/thumbsup
feat(core): skip files that can’t be processed + show a summary at the end
Thumbsup used to stop at the first problem processing a file. This was a problem on large galleries where you’d need to run it again and again, fixing files as you went. This change: - skips problematic files and shows a summary at the end - logs all warnings/errors to <thumbsup.log> when running the default output Also refactor and cleanup of the logging logic.pull/107/head
parent
7208642d95
commit
e9beb830a2
@ -1,17 +1,98 @@
|
||||
const debug = require('debug')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const util = require('util')
|
||||
const tty = require('tty')
|
||||
|
||||
exports.init = (logLevel) => {
|
||||
// enable particular debug() prefixes
|
||||
/*
|
||||
Thumbsup uses the <debug> package for logging.
|
||||
It supports two main ways of logging:
|
||||
|
||||
1. when --log is not specified
|
||||
- This ignores all detailed logging calls
|
||||
- It simply renders progress using Listr on <stdout>
|
||||
- Warning and error are sent to <thumbsup.log> in the output folder for troubleshooting
|
||||
|
||||
2. when --log is specified
|
||||
- This switches Listr to text mode, e.g. "Task XYZ [completed]"
|
||||
- It renders log messages to <stdout>
|
||||
- The logging level can be configured between info|debug|trace
|
||||
- Warnings and errors are always shown, because level is > info|debug|trace
|
||||
- <thumbsup.log> is not created by default
|
||||
- You can always redirect or tee the output to a file if needed
|
||||
|
||||
If --log is not specified, but the output doesn't support ANSI (e.g. non-TTY terminal, or file redirection)
|
||||
then the mode is automatically switched to "--log info"
|
||||
*/
|
||||
exports.init = (logLevel, outputFolder) => {
|
||||
// if the output doesn't support ANSI codes (e.g. pipe, redirect to file)
|
||||
// then switch to full-text mode, because Listr's output won't make much sense
|
||||
if (!logLevel && !tty.isatty(process.stdout.fd)) {
|
||||
logLevel = 'info'
|
||||
}
|
||||
|
||||
// Configure the loggers
|
||||
if (!logLevel) {
|
||||
configureDefaultMode(outputFolder)
|
||||
} else {
|
||||
configureDebugMode(logLevel)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
The <debug> package repeats the date + prefix for every log line, but only when using colors
|
||||
This fix adds the same behaviour in non-color mode
|
||||
It's important so the log files are easy to grep
|
||||
*/
|
||||
function overrideDebugFormat () {
|
||||
debug.formatArgs = function (args) {
|
||||
const prefix = new Date().toISOString() + ' ' + this.namespace + ' '
|
||||
args[0] = prefix + args[0].split('\n').join('\n' + prefix)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
If --log is not specified, we won't show any detailed log on stdout
|
||||
Instead we send all errors to a file for troubleshooting
|
||||
*/
|
||||
function configureDefaultMode (outputFolder) {
|
||||
const logfile = path.join(outputFolder, 'thumbsup.log')
|
||||
const stream = fs.createWriteStream(logfile, {flags: 'a'})
|
||||
overrideDebugFormat()
|
||||
debug.enable('thumbsup:error,thumbsup:warn')
|
||||
debug.useColors = () => false
|
||||
debug.log = function () {
|
||||
const line = util.format.apply(util, arguments) + '\n'
|
||||
stream.write(line)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
--log mode configuration
|
||||
*/
|
||||
function configureDebugMode (logLevel) {
|
||||
// because Listr logs to stdout (not configurable), make debug() do the same
|
||||
// otherwise file redirection gets very confusing
|
||||
debug.log = function () {
|
||||
const line = util.format.apply(util, arguments) + '\n'
|
||||
process.stdout.write(line)
|
||||
}
|
||||
|
||||
// TTY-related configuration
|
||||
const supportsANSI = tty.isatty(process.stdout.fd)
|
||||
debug.useColors = () => supportsANSI
|
||||
if (!supportsANSI) {
|
||||
overrideDebugFormat()
|
||||
}
|
||||
|
||||
// enable the right log levels
|
||||
if (logLevel === 'trace') debug.enable('*')
|
||||
if (logLevel === 'debug') debug.enable('thumbsup:error,thumbsup:warn,thumbsup:info,thumbsup:debug')
|
||||
if (logLevel === 'info') debug.enable('thumbsup:error,thumbsup:warn,thumbsup:info')
|
||||
|
||||
// when running in text-mode, make sure all console.log() calls go through debug()
|
||||
// don't touch them in normal-mode, since it would affect Listr's dnyamic rendering
|
||||
if (typeof logLevel === 'string') {
|
||||
console.log = require('debug')('thumbsup:info')
|
||||
console.info = require('debug')('thumbsup:info')
|
||||
console.warn = require('debug')('thumbsup:warn')
|
||||
console.error = require('debug')('thumbsup:error')
|
||||
}
|
||||
// capture any additional console.log() calls, including Listr task updates
|
||||
console.log = require('debug')('thumbsup:info')
|
||||
console.info = require('debug')('thumbsup:info')
|
||||
console.warn = require('debug')('thumbsup:warn')
|
||||
console.error = require('debug')('thumbsup:error')
|
||||
}
|
||||
|
@ -0,0 +1,26 @@
|
||||
const warn = require('debug')('thumbsup:warn')
|
||||
const messages = require('../bin/messages')
|
||||
|
||||
/*
|
||||
Keeps track of which source files we failed to process
|
||||
*/
|
||||
module.exports = class Problems {
|
||||
constructor () {
|
||||
this.files = {}
|
||||
}
|
||||
|
||||
addFile (path) {
|
||||
this.files[path] = true
|
||||
}
|
||||
|
||||
print () {
|
||||
// only print the number of failed files in the standard output
|
||||
const paths = Object.keys(this.files)
|
||||
if (paths.length > 0) {
|
||||
// print a short summary on stdout
|
||||
console.warn(messages.PROBLEMS(paths.length))
|
||||
// and a full log to the log file
|
||||
warn('The following sources files were not processed:\n' + paths.join(','))
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue