-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2245 from bugsnag/PLAT-12980-plugin-console-bread…
…crumbs Convert @bugsnag/plugin-console-breadcrumbs to TypeScript
- Loading branch information
Showing
8 changed files
with
123 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export default function reduce<T, U>(arr: T[], fn: (accum: any, item: T) => any, accum: any): any | ||
export default function reduce<T, U>(arr: T[], fn: (accum: any, item: T, index: number, arr: T[]) => any, accum: any): any |
47 changes: 0 additions & 47 deletions
47
packages/plugin-console-breadcrumbs/console-breadcrumbs.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import createRollupConfig from "../../.rollup/index.mjs"; | ||
|
||
export default createRollupConfig({ | ||
input: "src/console-breadcrumbs.ts", | ||
external: ["@bugsnag/core/lib/es-utils/filter", "@bugsnag/core/lib/es-utils/map", "@bugsnag/core/lib/es-utils/reduce"] | ||
}); |
84 changes: 84 additions & 0 deletions
84
packages/plugin-console-breadcrumbs/src/console-breadcrumbs.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { Client, Config, Plugin } from '@bugsnag/core' | ||
import filter from '@bugsnag/core/lib/es-utils/filter' | ||
import map from '@bugsnag/core/lib/es-utils/map' | ||
import reduce from '@bugsnag/core/lib/es-utils/reduce' | ||
|
||
type ConsoleMethod = 'log' | 'debug' | 'info' | 'warn' | 'error' | ||
|
||
interface ClientWithInternals extends Client { | ||
_config: Required<Config> | ||
_isBreadcrumbTypeEnabled: (type: ConsoleMethod) => boolean | ||
} | ||
|
||
type ConsoleWithRestore = Console & { | ||
[key in ConsoleMethod]: typeof console[ConsoleMethod] & { _restore: () => void } | ||
} | ||
|
||
/* | ||
* Leaves breadcrumbs when console log methods are called | ||
*/ | ||
const plugin: Plugin = { | ||
load: (client) => { | ||
const isDev = /^(local-)?dev(elopment)?$/.test((client as ClientWithInternals)._config.releaseStage) | ||
|
||
if (isDev || !(client as ClientWithInternals)._isBreadcrumbTypeEnabled('log')) return | ||
|
||
map(CONSOLE_LOG_METHODS, method => { | ||
const original = console[method] | ||
console[method] = (...args: any) => { | ||
client.leaveBreadcrumb( | ||
'Console output', | ||
reduce( | ||
args, | ||
(accum, arg, i) => { | ||
// do the best/simplest stringification of each argument | ||
let stringified = '[Unknown value]' | ||
// this may fail if the input is: | ||
// - an object whose [[Prototype]] is null (no toString) | ||
// - an object with a broken toString or @@toPrimitive implementation | ||
try { | ||
stringified = String(arg) | ||
} catch (e) {} | ||
// if it stringifies to [object Object] attempt to JSON stringify | ||
if (stringified === '[object Object]') { | ||
// catch stringify errors and fallback to [object Object] | ||
try { | ||
stringified = JSON.stringify(arg) | ||
} catch (e) {} | ||
} | ||
accum[`[${i}]`] = stringified | ||
return accum | ||
}, | ||
{ | ||
severity: method.indexOf('group') === 0 ? 'log' : method | ||
} | ||
), | ||
'log' | ||
) | ||
original.apply(console, args) | ||
} | ||
(console as ConsoleWithRestore)[method]._restore = () => { | ||
console[method] = original | ||
} | ||
}) | ||
} | ||
} | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
plugin.destroy = () => { | ||
const consoleWithRestore = console as ConsoleWithRestore | ||
CONSOLE_LOG_METHODS.forEach(method => { | ||
if (typeof consoleWithRestore[method]._restore === 'function') { | ||
consoleWithRestore[method]._restore() | ||
} | ||
}) | ||
} | ||
} | ||
|
||
const CONSOLE_LOG_METHODS: ConsoleMethod[] = filter( | ||
['log', 'debug', 'info', 'warn', 'error'], | ||
method => | ||
typeof console !== 'undefined' && typeof console[method] === 'function' | ||
) | ||
|
||
export default plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": ["src/**/*.ts"], | ||
"compilerOptions": { | ||
"target": "ES2020", | ||
"types": ["node"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters