Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Collina <[email protected]>
  • Loading branch information
mcollina committed Jan 14, 2025
1 parent 152ad63 commit 2dba8b2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
10 changes: 7 additions & 3 deletions lib/colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ const plain = {
20: nocolor,
10: nocolor,
message: nocolor,
greyMessage: nocolor
greyMessage: nocolor,
property: nocolor
}

const { createColors } = require('colorette')
const getLevelLabelData = require('./utils/get-level-label-data')
const availableColors = createColors({ useColor: true })
const { white, bgRed, red, yellow, green, blue, gray, cyan } = availableColors
const { white, bgRed, red, yellow, green, blue, gray, cyan, magenta } = availableColors

const colored = {
default: white,
Expand All @@ -27,7 +28,8 @@ const colored = {
20: blue,
10: gray,
message: cyan,
greyMessage: gray
greyMessage: gray,
property: magenta
}

function resolveCustomColoredColorizer (customColors) {
Expand Down Expand Up @@ -56,6 +58,7 @@ function plainColorizer (useOnlyCustomProps) {
}
customColoredColorizer.message = plain.message
customColoredColorizer.greyMessage = plain.greyMessage
customColoredColorizer.property = plain.property
customColoredColorizer.colors = createColors({ useColor: false })
return customColoredColorizer
}
Expand All @@ -66,6 +69,7 @@ function coloredColorizer (useOnlyCustomProps) {
return newColoredColorizer(level, colored, opts)
}
customColoredColorizer.message = colored.message
customColoredColorizer.property = colored.property
customColoredColorizer.greyMessage = colored.greyMessage
customColoredColorizer.colors = availableColors
return customColoredColorizer
Expand Down
4 changes: 3 additions & 1 deletion lib/utils/prettify-error-log.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const tap = require('tap')
const prettifyErrorLog = require('./prettify-error-log')
const colors = require('../colors')
const {
ERROR_LIKE_KEYS,
MESSAGE_KEY
Expand All @@ -13,7 +14,8 @@ const context = {
customPrettifiers: {},
errorLikeObjectKeys: ERROR_LIKE_KEYS,
errorProps: [],
messageKey: MESSAGE_KEY
messageKey: MESSAGE_KEY,
objectColorizer: colors()
}

tap.test('returns string with default settings', async t => {
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/prettify-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function prettifyObject ({
lines = lines.replace(/\\\\/gi, '\\')

const joinedLines = joinLinesWithIndentation({ input: lines, ident, eol })
result += `${ident}${keyName}:${joinedLines.startsWith(eol) ? '' : ' '}${joinedLines}${eol}`
result += `${ident}${objectColorizer.property(keyName)}:${joinedLines.startsWith(eol) ? '' : ' '}${joinedLines}${eol}`
})
}

Expand Down
16 changes: 14 additions & 2 deletions lib/utils/prettify-object.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const prettifyObject = require('./prettify-object')
const {
ERROR_LIKE_KEYS
} = require('../constants')
const getColorizer = require('../colors')

const context = {
EOL: '\n',
Expand All @@ -15,7 +14,7 @@ const context = {
errorLikeObjectKeys: ERROR_LIKE_KEYS,
objectColorizer: colors(),
singleLine: false,
colorizer: getColorizer()
colorizer: colors()
}

tap.test('returns empty string if no properties present', async t => {
Expand Down Expand Up @@ -152,3 +151,16 @@ tap.test('errors skips prettifying if no lines are present', async t => {
})
t.equal(str, '')
})

tap.test('works with single level properties', async t => {
const colorizer = colors(true)
const str = prettifyObject({
log: { foo: 'bar' },
context: {
...context,
objectColorizer: colorizer,
colorizer
}
})
t.equal(str, ` ${colorizer.colors.magenta('foo')}: "bar"\n`)
})

0 comments on commit 2dba8b2

Please sign in to comment.