orionsoft-react-scripts
Version:
Orionsoft Configuration and scripts for Create React App.
313 lines (233 loc) • 6.92 kB
text/coffeescript
{object, array} = require 'utila'
defaultStyle = require './defaultStyle'
ParsedError = require './ParsedError'
nodePaths = require './nodePaths'
RenderKid = require 'renderkid'
instance = null
module.exports = class PrettyError
self = @
:
'module.exports': (item) ->
return unless item.what?
item.what = item.what.replace /\.module\.exports\./g, ' - '
return
: ->
defaultStyle()
: ->
unless instance?
instance = new self
instance.start()
instance
: ->
instance?.stop()
constructor: ->
= yes
= 50
= []
= []
= []
= []
= []
= []
= new RenderKid
= self._getDefaultStyle()
.style
start: ->
= Error.prepareStackTrace
prepeare = or (exc, frames) ->
result = exc.toString()
frames = frames.map (frame) -> " at #{frame.toString()}"
result + "\n" + frames.join "\n"
# https://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
Error.prepareStackTrace = (exc, trace) =>
stack = prepeare.apply(null, arguments)
{stack, message: exc.toString().replace /^.*: /, ''}, no
@
stop: ->
Error.prepareStackTrace =
= null
config: (c) ->
if c.skipPackages?
if c.skipPackages is no
else
.apply @, c.skipPackages
if c.skipPaths?
if c.skipPaths is no
else
.apply @, c.skipPaths
if c.skip?
if c.skip is no
else
.apply @, c.skip
if c.maxItems?
c.maxItems
if c.skipNodeFiles is yes
else if c.skipNodeFiles is no
if c.filters?
if c.filters is no
else
.apply @, c.filters
if c.parsedErrorFilters?
if c.parsedErrorFilters is no
else
.apply @, c.parsedErrorFilters
if c.aliases?
if object.isBareObject c.aliases
path, alias for path, alias of c.aliases
else if c.aliases is no
@
withoutColors: ->
= false
@
withColors: ->
= true
@
skipPackage: (packages...) ->
.push String pkg for pkg in packages
@
unskipPackage: (packages...) ->
array.pluckOneItem( , pkg) for pkg in packages
@
unskipAllPackages: ->
.length = 0
@
skipPath: (paths...) ->
.push path for path in paths
@
unskipPath: (paths...) ->
array.pluckOneItem( , path) for path in paths
@
unskipAllPaths: ->
.length = 0
@
skip: (callbacks...) ->
.push cb for cb in callbacks
@
unskip: (callbacks...) ->
array.pluckOneItem( , cb) for cb in callbacks
@
unskipAll: ->
.length = 0
@
skipNodeFiles: ->
.apply @, nodePaths
unskipNodeFiles: ->
.apply @, nodePaths
filter: (callbacks...) ->
.push cb for cb in callbacks
@
removeFilter: (callbacks...) ->
array.pluckOneItem( , cb) for cb in callbacks
@
removeAllFilters: ->
.length = 0
@
filterParsedError: (callbacks...) ->
.push cb for cb in callbacks
@
removeParsedErrorFilter: (callbacks...) ->
array.pluckOneItem( , cb) for cb in callbacks
@
removeAllParsedErrorFilters: ->
.length = 0
@
setMaxItems: (maxItems = 50) ->
if maxItems is 0 then maxItems = 50
= maxItems|0
@
alias: (stringOrRx, alias) ->
.push {stringOrRx, alias}
@
removeAlias: (stringOrRx) ->
array.pluckByCallback , (pair) ->
pair.stringOrRx is stringOrRx
@
removeAllAliases: ->
.length = 0
@
_getStyle: ->
appendStyle: (toAppend) ->
object.appendOnto , toAppend
.style toAppend
@
_getRenderer: ->
render: (e, logIt = no, useColors = ) ->
obj = e
rendered = .render(obj, useColors)
console.error rendered if logIt is yes
rendered
getObject: (e) ->
unless e instanceof ParsedError
e = new ParsedError e
e
header =
title: do ->
ret = {}
# some errors are thrown to display other errors.
# we call them wrappers here.
if e.wrapper isnt ''
ret.wrapper = "#{e.wrapper}"
ret.kind = e.kind
ret
colon: ':'
message: String(e.message).trim()
traceItems = []
count = -1
for item, i in e.trace
continue unless item?
continue if is yes
count++
break if count >
if typeof item is 'string'
traceItems.push item: custom: item
continue
traceItems.push do ->
markupItem = item:
header:
pointer: do ->
return '' unless item.file?
file: item.file
colon: ':'
line: item.line
footer: do ->
foooter = addr: item.shortenedAddr
if item.extra? then foooter.extra = item.extra
foooter
markupItem.item.header.what = item.what if typeof item.what is 'string' and item.what.trim().length > 0
markupItem
obj = 'pretty-error':
header: header
if traceItems.length > 0
obj['pretty-error'].trace = traceItems
obj
_skipOrFilter: (item, itemNumber) ->
if typeof item is 'object'
return yes if item.modName in
return yes if item.path in
for modName in item.packages
return yes if modName in
if typeof item.shortenedAddr is 'string'
for pair in
item.shortenedAddr = item.shortenedAddr.replace pair.stringOrRx, pair.alias
for cb in
return yes if cb(item, itemNumber) is yes
for cb in
cb(item, itemNumber)
return no
_applyParsedErrorFiltersOn: (error) ->
for cb in
cb error
return
for prop in ['renderer', 'style'] then do ->
methodName = '_get' + prop[0].toUpperCase() + prop.substr(1, prop.length)
PrettyError::__defineGetter__ prop, -> do @[methodName]