Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 1153x 193x 193x 193x 193x 193x 193x 63x 193x 193x 193x 193x 193x 193x 1x 193x 193x 193x 193x 193x 193x 98x 98x 98x 98x 98x 98x 98x 98x 98x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 18x 193x 193x 193x 193x 2x 193x 193x 193x 193x 6x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 1217x 1217x 1217x 1216x 1216x 1x 1x 1217x 193x 193x 193x 193x 193x 193x 98x 98x 98x 193x 193x 193x 193x 193x 193x 193x 99x 99x 99x 99x 99x 99x 99x 193x 193x 193x 193x 193x 193x 98x 98x 98x 98x 98x 98x 98x 98x | import { format } from 'node:util';
import { styleText } from './lib/style-text.js';
/**
* @typedef {import('./logger.types.js').ApiError} ApiError
*/
const IS_QUIET = Boolean(process.env.CLEVER_QUIET);
const IS_VERBOSE = Boolean(process.env.CLEVER_VERBOSE);
export const Logger = {
/**
* @param {string} message
*/
debug(message) {
consoleLog('debug', message);
},
/**
* @param {string} message
*/
info(message) {
consoleLog('info', message);
},
/**
* @param {string} message
*/
warn(message) {
consoleLog('warn', message);
},
/**
* @param {Error|string} error
*/
error(error) {
if (IS_QUIET) {
return;
}
const prefix = '[ERROR] ';
const styledPrefix = styleText(['bold', 'red'], prefix);
const message = error instanceof Error ? error.message : error;
const formatted = formatLines(prefix.length, processApiError(message));
if (IS_VERBOSE) {
writeStderr('[STACKTRACE]');
writeStderr(error);
writeStderr('[/STACKTRACE]');
}
writeStderr(`${styledPrefix}${formatted}`);
},
println: console.log,
/**
* @param {string} text
* @param {number} indentLevel
*/
printlnWithIndent(text, indentLevel) {
console.log(' '.repeat(indentLevel) + text);
},
/** @param {string} message */
printSuccess(message) {
console.log(`${styleText(['bold', 'green'], '✓')} ${message}`);
},
/** @param {string} message */
printInfo(message) {
console.log(`${styleText('blue', 'i')} ${message}`);
},
/** @param {unknown} obj */
printJson(obj) {
console.log(JSON.stringify(obj, null, 2));
},
printErrorLine: writeStderr,
};
/**
* Logs a message to the console with severity prefix.
* @param {'debug'|'info'|'warn'} severity
* @param {string} message
* @returns {void}
*/
function consoleLog(severity, message) {
if (IS_QUIET) {
return;
}
if (!IS_VERBOSE && severity !== 'warn') {
return;
}
const prefix = `[${severity.toUpperCase()}] `;
console.log(`${prefix}${formatLines(prefix.length, message)}`);
}
/**
* Writes a formatted line to stderr.
* @param {Error|string} value
* @returns {void}
*/
function writeStderr(value) {
process.stderr.write(format(value) + '\n');
}
/**
* Formats a multiline message with indentation for continuation lines.
* @param {number} prefixLength
* @param {string} message
* @returns {string}
*/
function formatLines(prefixLength, message) {
const indent = ' '.repeat(prefixLength);
return message
.split('\n')
.map((line, i) => (i === 0 ? line : indent + line))
.join('\n');
}
/**
* Transforms an API error object into a formatted message string.
* @param {ApiError|string} error
* @returns {string}
*/
function processApiError(error) {
if (typeof error === 'string') {
return error;
}
const { id, message, fields } = error;
if (id == null || message == null) {
return String(error);
}
const fieldLines = Object.entries(fields ?? {}).map(([name, msg]) => `${name}: ${msg}`);
return [`${message} [${id}]`, ...fieldLines].join('\n');
}
|