build-log.mjs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import chalk from 'chalk'
  2. import dayjs from 'dayjs'
  3. export const PLATFORM_LABELS = {
  4. mac: 'macOS',
  5. win: 'Windows',
  6. linux: 'Linux',
  7. }
  8. const PLATFORM_COLORS = {
  9. mac: chalk.magenta,
  10. win: chalk.cyan,
  11. linux: chalk.green,
  12. }
  13. export function formatTimestamp(date = new Date()) {
  14. return dayjs(date).format('YYYY-MM-DD HH:mm:ss')
  15. }
  16. function formatLogLine(message) {
  17. return `${formatTimestamp()} ${message}`
  18. }
  19. export function formatDuration(ms) {
  20. const totalSeconds = Math.floor(ms / 1000)
  21. const hours = Math.floor(totalSeconds / 3600)
  22. const minutes = Math.floor((totalSeconds % 3600) / 60)
  23. const seconds = totalSeconds % 60
  24. if (hours > 0) {
  25. return `${hours}h ${minutes}m ${seconds}s`
  26. }
  27. if (minutes > 0) {
  28. return `${minutes}m ${seconds}s`
  29. }
  30. return `${seconds}s`
  31. }
  32. export function logBanner(message) {
  33. console.log(chalk.bold.blue(`\n${formatLogLine(`=== ${message} ===`)}`))
  34. }
  35. export function logStep(message) {
  36. console.log(chalk.blue(formatLogLine(`-> ${message}`)))
  37. }
  38. export function logSuccess(message) {
  39. console.log(chalk.green(formatLogLine(`✓ ${message}`)))
  40. }
  41. export function logWarning(message) {
  42. console.log(chalk.yellow(formatLogLine(`! ${message}`)))
  43. }
  44. export function logPlatform(platform, message) {
  45. const color = PLATFORM_COLORS[platform] || chalk.white
  46. const label = PLATFORM_LABELS[platform] || platform
  47. console.log(color(formatLogLine(`[${label}] ${message}`)))
  48. }