Adding in support for multi-parameter logs and actual logging to console.

This commit is contained in:
mike12345567 2025-01-17 11:06:55 +00:00
parent e6d536bcc8
commit e146d995eb
3 changed files with 31 additions and 21 deletions

View File

@ -19,7 +19,7 @@
$: success = !error && !empty $: success = !error && !empty
$: highlightedResult = highlight(expressionResult) $: highlightedResult = highlight(expressionResult)
$: highlightedLogs = expressionLogs.map(l => ({ $: highlightedLogs = expressionLogs.map(l => ({
log: highlight(l.log), log: highlight(l.log.join(", ")),
line: l.line, line: l.line,
})) }))

View File

@ -124,7 +124,16 @@ export function processJS(handlebars: string, context: any) {
if (!isBackendService()) { if (!isBackendService()) {
// this counts the lines in the wrapped JS *before* the user's code, so that we can minus it // this counts the lines in the wrapped JS *before* the user's code, so that we can minus it
const jsLineCount = frontendWrapJS(js).split(js)[0].split("\n").length const jsLineCount = frontendWrapJS(js).split(js)[0].split("\n").length
const log = (log: string) => { const buildLogResponse = (
type: "log" | "info" | "debug" | "warn" | "error" | "trace" | "table"
) => {
return (...props: any[]) => {
console[type](...props)
props.forEach((prop, index) => {
if (typeof prop === "object") {
props[index] = JSON.stringify(prop)
}
})
// quick way to find out what line this is being called from // quick way to find out what line this is being called from
// its an anonymous function and we look for the overall length to find the // its an anonymous function and we look for the overall length to find the
// line number we care about (from the users function) // line number we care about (from the users function)
@ -133,19 +142,20 @@ export function processJS(handlebars: string, context: any) {
/<anonymous>:(\d+):\d+/ /<anonymous>:(\d+):\d+/
)?.[1] )?.[1]
logs.push({ logs.push({
log, log: props,
line: lineNumber ? parseInt(lineNumber) - jsLineCount : undefined, line: lineNumber ? parseInt(lineNumber) - jsLineCount : undefined,
}) })
} }
}
sandboxContext.console = { sandboxContext.console = {
log: log, log: buildLogResponse("log"),
info: log, info: buildLogResponse("info"),
debug: log, debug: buildLogResponse("debug"),
warn: log, warn: buildLogResponse("warn"),
error: log, error: buildLogResponse("error"),
// two below may need special cases // two below may need special cases
trace: log, trace: buildLogResponse("trace"),
table: log, table: buildLogResponse("table"),
} }
} }

View File

@ -10,6 +10,6 @@ export interface ProcessOptions {
} }
export interface Log { export interface Log {
log: string log: any[]
line?: number line?: number
} }