Skip to content

Commit dba1a01

Browse files
committed
refactor(robo/cli): cleaner type errors
1 parent 75d12ab commit dba1a01

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

.changeset/beige-fans-speak.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'robo.js': patch
3+
---
4+
5+
refactor(cli): cleaner type errors

packages/robo/src/cli/compiler/typescript.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { color } from '../../core/color.js'
12
import { IS_BUN_RUNTIME } from '../utils/runtime-utils.js'
23
import { compilerLogger } from '../utils/loggers.js'
34
import { existsSync } from 'node:fs'
@@ -46,7 +47,7 @@ export async function buildDeclarationFiles(tsOptions?: CompilerOptions) {
4647
allDiagnostics.forEach((diagnostic) => {
4748
switch (diagnostic.category) {
4849
case ts.DiagnosticCategory.Error:
49-
compilerLogger.error(formatDiagnostic(diagnostic))
50+
compilerLogger.custom('typeerror', color.red(formatDiagnostic(diagnostic)))
5051
break
5152
case ts.DiagnosticCategory.Warning:
5253
compilerLogger.warn(formatDiagnostic(diagnostic))
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
import { logger } from '../../core/logger.js'
22

3+
// Register custom log levels BEFORE creating any forks
4+
logger({
5+
customLevels: {
6+
typeerror: {
7+
label: 'typeerror',
8+
priority: 7,
9+
color: 'info'
10+
}
11+
}
12+
})
13+
314
export const compilerLogger = logger.fork('compiler')

packages/robo/src/core/logger.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export type LogLevel = 'trace' | 'debug' | 'info' | 'wait' | 'other' | 'event' |
2121
interface CustomLevel {
2222
label: string
2323
priority: number
24+
color?: keyof typeof colorMap
2425
}
2526

2627
export interface LoggerOptions {
@@ -524,7 +525,8 @@ export class Logger {
524525
public setup(options?: LoggerOptions) {
525526
const { customLevels, drain = consoleDrain, enabled = true, level, parent, prefix } = options ?? {}
526527

527-
this._customLevels = customLevels
528+
// Preserve existing customLevels if new ones aren't provided
529+
this._customLevels = customLevels ?? this._customLevels
528530
this._drain = drain
529531
this._enabled = enabled
530532
this._parent = parent
@@ -566,12 +568,18 @@ export class Logger {
566568

567569
// Format the message all pretty and stuff
568570
if (level !== 'other') {
569-
const label = this._customLevels ? this._customLevels[level]?.label : colorizedLogLevels[level]
570-
let levelLabel = (label ?? level.padEnd(5)) + ' -'
571+
const customLevel = this._customLevels?.[level]
572+
const levelColor = (customLevel?.color ? colorMap[customLevel.color] : colorMap[level]) ?? ((s: string) => s)
573+
574+
// Apply color to label if custom level has a color, otherwise use pre-colorized label
575+
let levelLabel =
576+
(customLevel?.color
577+
? levelColor(customLevel.label.padEnd(9))
578+
: customLevel?.label ?? colorizedLogLevels[level] ?? level.padEnd(5)) + ' -'
571579

572580
// Add the prefix if specified
573581
if (prefix) {
574-
levelLabel = color.bold(colorMap[level](prefix + ':')) + levelLabel
582+
levelLabel = color.bold(levelColor(prefix + ':')) + levelLabel
575583
}
576584

577585
data.unshift(levelLabel)

0 commit comments

Comments
 (0)