Skip to content

Commit

Permalink
feature: support console.table
Browse files Browse the repository at this point in the history
  • Loading branch information
stagas committed Feb 4, 2022
1 parent 99b8a04 commit 78c2d04
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// credits: https://github.com/andywer/puppet-run/blob/40a7eb06ccd8b2b9dcfdeeac273ec27dce6d3c54/src/host-bindings.ts

import chalk from '@stagas/chalk'
import type { Page } from 'puppeteer'
import chalk from '@stagas/chalk'

/**
* Setups console output for a puppeteer page.
Expand All @@ -16,11 +16,6 @@ import type { Page } from 'puppeteer'
export function puppeteerPrettyConsole(page: Page) {
page.on('console', async message => {
const type = message.type()
const text = message.text()
const messageArgs = message.args()
let args = await Promise.all(messageArgs.map(arg => arg.jsonValue()))
messageArgs.forEach(arg => arg.dispose()) // gc
if (text.length) args = [text]

if (type === 'clear') {
return console.clear()
Expand All @@ -30,36 +25,35 @@ export function puppeteerPrettyConsole(page: Page) {
return console.groupEnd()
}

if (type === 'error') {
console.error(...args)
const text = message.text()
const messageArgs = message.args()
let args = await Promise.all(messageArgs.map(arg => arg.jsonValue()))
messageArgs.forEach(arg => arg.dispose()) // gc

if (['log', 'warn', 'error'].includes(type)) {
if (text.length) args = [text]
}

if (type === 'startGroup') {
console.group(...args)
} else if (type === 'warning') {
console.warn(...args)
} else if (type === 'debug') {
console.debug(...args)
} else if (type === 'startGroup') {
console.group(...args)
} else {
console.log(...args)
;((console as any)[type] ?? console.log)(...args)
}
})

page.on('requestfailed', request => {
const failure = request.failure()
console.error(
chalk.redBright(`Request failed: ${request.method()} ${request.url()}`)
)
console.error(
chalk.gray(` ${failure ? failure.errorText : 'Unknown error'}`)
)
console.error(chalk.redBright(`Request failed: ${request.method()} ${request.url()}`))
console.error(chalk.gray(` ${failure ? failure.errorText : 'Unknown error'}`))
})

page.on('requestfinished', request => {
const response = request.response()
if (response && response.status() >= 400) {
console.error(
chalk.redBright(
`HTTP ${response.status()} ${request.method()} ${request.url()}`
)
chalk.redBright(`HTTP ${response.status()} ${request.method()} ${request.url()}`)
)
}
})
Expand Down

0 comments on commit 78c2d04

Please sign in to comment.