Skip to content

Commit

Permalink
feat: better stack formater
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Oct 5, 2018
1 parent 134ff54 commit f5acb3c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/reporters/basic.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { formatStack } from '../utils'

export default class BasicReporter {
constructor (stream) {
this.stream = stream || process.stdout
Expand All @@ -23,7 +25,11 @@ export default class BasicReporter {
}

if (logObj.stack) {
this.stream.write(logObj.stack + '\n')
const stack = formatStack(logObj.stack, {
prefix: '> '
})

this.stream.write(stack + '\n')
}
}
}
9 changes: 6 additions & 3 deletions src/reporters/fancy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import chalk from 'chalk'
import figures from 'figures'
import startCase from 'lodash/startCase'
import { formatStack } from '../utils'

const NS_SEPARATOR = chalk.blue(figures(' › '))

Expand Down Expand Up @@ -61,9 +62,11 @@ export default class FancyReporter {
}

if (logObj.stack) {
const stack = Array.isArray(logObj.stack) ? logObj.stack : logObj.stack.split('\n')
const lines = stack.map(s => ' ' + s.trim()).join('\n')
this.stream.write(chalk[logObj.additionalStyle || 'grey'](lines) + '\n')
const stack = formatStack(logObj.stack, {
suffix: ' ↲'
})

this.stream.write(chalk[logObj.additionalStyle || 'grey'](stack) + '\n')
}
}
}
12 changes: 12 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ export function assignToLogObj (logObj, obj) {
}
}
}

export function formatStack (stack, { prefix = ' ', suffix = '' } = {}) {
let lines = stack
.split('\n')
.map(l => l.trim().replace(/^at /, ''))

if (lines[0].indexOf('Error: ') === 0) {
lines = lines.splice(1)
}

return prefix + lines.join(suffix + '\n' + prefix) + suffix
}

0 comments on commit f5acb3c

Please sign in to comment.