logger.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env bun
  2. /**
  3. * Logger utilities for upstream merge automation
  4. */
  5. export type LogLevel = "debug" | "info" | "warn" | "error" | "success"
  6. const colors = {
  7. reset: "\x1b[0m",
  8. dim: "\x1b[2m",
  9. red: "\x1b[31m",
  10. green: "\x1b[32m",
  11. yellow: "\x1b[33m",
  12. blue: "\x1b[34m",
  13. magenta: "\x1b[35m",
  14. cyan: "\x1b[36m",
  15. }
  16. const levelColors: Record<LogLevel, string> = {
  17. debug: colors.dim,
  18. info: colors.blue,
  19. warn: colors.yellow,
  20. error: colors.red,
  21. success: colors.green,
  22. }
  23. const levelPrefixes: Record<LogLevel, string> = {
  24. debug: "[DEBUG]",
  25. info: "[INFO]",
  26. warn: "[WARN]",
  27. error: "[ERROR]",
  28. success: "[OK]",
  29. }
  30. let verbose = false
  31. export function setVerbose(value: boolean): void {
  32. verbose = value
  33. }
  34. export function log(level: LogLevel, message: string, ...args: unknown[]): void {
  35. if (level === "debug" && !verbose) return
  36. const color = levelColors[level]
  37. const prefix = levelPrefixes[level]
  38. console.log(`${color}${prefix}${colors.reset} ${message}`, ...args)
  39. }
  40. export function debug(message: string, ...args: unknown[]): void {
  41. log("debug", message, ...args)
  42. }
  43. export function info(message: string, ...args: unknown[]): void {
  44. log("info", message, ...args)
  45. }
  46. export function warn(message: string, ...args: unknown[]): void {
  47. log("warn", message, ...args)
  48. }
  49. export function error(message: string, ...args: unknown[]): void {
  50. log("error", message, ...args)
  51. }
  52. export function success(message: string, ...args: unknown[]): void {
  53. log("success", message, ...args)
  54. }
  55. export function header(title: string): void {
  56. console.log()
  57. console.log(`${colors.cyan}${"=".repeat(60)}${colors.reset}`)
  58. console.log(`${colors.cyan} ${title}${colors.reset}`)
  59. console.log(`${colors.cyan}${"=".repeat(60)}${colors.reset}`)
  60. console.log()
  61. }
  62. export function step(number: number, total: number, description: string): void {
  63. console.log(`${colors.magenta}[${number}/${total}]${colors.reset} ${description}`)
  64. }
  65. export function list(items: string[], indent = 2): void {
  66. const spaces = " ".repeat(indent)
  67. for (const item of items) {
  68. console.log(`${spaces}${colors.dim}-${colors.reset} ${item}`)
  69. }
  70. }
  71. export function divider(): void {
  72. console.log(`${colors.dim}${"-".repeat(60)}${colors.reset}`)
  73. }