|
| 1 | +// Import Third-party Dependencies |
| 2 | +import * as i18n from "@nodesecure/i18n"; |
| 3 | +import ms from "ms"; |
| 4 | + |
| 5 | +// Import Internal Dependencies |
| 6 | +import kleur from "../../utils/styleText.js"; |
| 7 | + |
| 8 | +export function log(token, ...args) { |
| 9 | + console.log(kleur.white().bold(i18n.getTokenSync(token, ...args))); |
| 10 | +} |
| 11 | + |
| 12 | +export function logError(token, ...args) { |
| 13 | + console.log(kleur.red().bold(i18n.getTokenSync(token, ...args))); |
| 14 | +} |
| 15 | + |
| 16 | +export function logScannerStat(stat, isVerbose = true) { |
| 17 | + console.log(kleur.bold.white( |
| 18 | + i18n.getTokenSync("cli.stat", |
| 19 | + isVerbose ? kleur.blue().bold("verbose ") : "", |
| 20 | + stat.name, |
| 21 | + colorExecutionTime(stat.executionTime) |
| 22 | + ))); |
| 23 | +} |
| 24 | + |
| 25 | +export function logScannerError(error, phase) { |
| 26 | + console.log(kleur.bold.white( |
| 27 | + i18n.getTokenSync("cli.error.name", |
| 28 | + kleur.red().bold("error"), |
| 29 | + error.name |
| 30 | + ))); |
| 31 | + |
| 32 | + if (error.message) { |
| 33 | + console.log(i18n.getTokenSync("cli.error.message", |
| 34 | + error.message |
| 35 | + )); |
| 36 | + } |
| 37 | + |
| 38 | + if (phase) { |
| 39 | + console.log(i18n.getTokenSync("cli.error.phase", |
| 40 | + phase |
| 41 | + )); |
| 42 | + } |
| 43 | + |
| 44 | + if (error.statusCode) { |
| 45 | + console.log(i18n.getTokenSync("cli.error.statusCode", |
| 46 | + error.statusCode |
| 47 | + )); |
| 48 | + } |
| 49 | + |
| 50 | + console.log(i18n.getTokenSync("cli.error.executionTime", |
| 51 | + colorExecutionTime(error.executionTime) |
| 52 | + )); |
| 53 | + |
| 54 | + if (error.stack) { |
| 55 | + console.log(i18n.getTokenSync("cli.error.stack", |
| 56 | + error.stack |
| 57 | + )); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +export function formatMs(time) { |
| 62 | + return ms(Number(time.toFixed(2))); |
| 63 | +} |
| 64 | + |
| 65 | +function colorExecutionTime(timeMs) { |
| 66 | + const formatted = formatMs(timeMs); |
| 67 | + if (timeMs <= 1_000) { |
| 68 | + return kleur.green().bold(formatted); |
| 69 | + } |
| 70 | + else if (timeMs <= 5_000) { |
| 71 | + return kleur.cyan().bold(formatted); |
| 72 | + } |
| 73 | + else if (timeMs <= 30_000) { |
| 74 | + return kleur.yellow().bold(formatted); |
| 75 | + } |
| 76 | + |
| 77 | + return kleur.red().bold(formatted); |
| 78 | +} |
0 commit comments