@sap/cds-dk
Version:
Command line client and development toolkit for the SAP Cloud Application Programming Model
34 lines (31 loc) • 1.63 kB
JavaScript
/**
* Sorts, filters, and writes compilation messages to console
* @param {import('@sap/cds-compiler').CompileMessage[]} messages
* @param {object} options
*/
module.exports = (messages, options={}) => {
const cds = require('../../lib')
const { format } = require('../../lib/util/term')
const logLevel = options && options['log-level'] || require('../../lib').env.log.levels.cli || ''
const levels = {
debug: { Error:4, undefined:4, Warning:3, Info:2, Debug:1 },
info: { Error:4, undefined:4, Warning:3, Info:2 },
warn: { Error:4, undefined:4, Warning:3 },
error: { Error:4, undefined:4 },
} [logLevel.toLowerCase()] || { Error:4, undefined:4, Warning:3 }
const log = options.log || console.error
// cds10: Remove cds.utils.colors.for check
if (log === console.error && cds.utils.colors.for) format.colors = cds.utils.colors.for(process.stderr).enabled
if (!Array.isArray (messages)) messages = [messages]
for (let m of messages.filter (m => m.severity in levels)) {
// show stack for resolution issues since there the requiring code location is in the stack
// check for standard Error classes, but not Error itself
const internalError = m.name === 'EvalError' || m.name === 'InternalError' || m.name === 'RangeError'
|| m.name === 'ReferenceError' || m.name === 'SyntaxError' || m.name === 'TypeError'
|| m.name === 'URIError'
if (m.$location?.file) m.$location.file = cds.utils.local(m.$location.file) // recompute file locations based on original cwd
const mf = format (m, m.severity, internalError, true)
log (mf)
}
}
/* eslint no-console:off */