Jonathan Reinink cd5cb00266
Implement one-time logging to prevent duplicate warnings (#5661)
Co-Authored-By: Adam Wathan <4323180+adamwathan@users.noreply.github.com>
2021-09-30 21:16:01 -04:00

26 lines
679 B
JavaScript

import chalk from 'chalk'
let alreadyShown = new Set()
function log(chalk, messages, key) {
if (process.env.JEST_WORKER_ID !== undefined) return
if (key && alreadyShown.has(key)) return
if (key) alreadyShown.add(key)
console.warn('')
messages.forEach((message) => console.warn(chalk, '-', message))
}
export default {
info(key, messages) {
log(chalk.bold.cyan('info'), ...(Array.isArray(key) ? [key] : [messages, key]))
},
warn(key, messages) {
log(chalk.bold.yellow('warn'), ...(Array.isArray(key) ? [key] : [messages, key]))
},
risk(key, messages) {
log(chalk.bold.magenta('risk'), ...(Array.isArray(key) ? [key] : [messages, key]))
},
}