Skip to content

Commit cc7d2d7

Browse files
committed
Better formatting of runtime logs. Typing logs. Fixing ShipResult bug
1 parent fa54fac commit cc7d2d7

File tree

6 files changed

+104
-13
lines changed

6 files changed

+104
-13
lines changed

src/components/Go.tsx

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import {useContext, useState} from 'react'
2+
import {Box, Text} from 'ink'
23

3-
import {getShortUUID, useProjectJobListener, useStartShipOnMount} from '@cli/utils/index.js'
4+
import {TruncatedText} from './common/TruncatedText.js'
5+
import {
6+
getRuntimeLogLevelColor,
7+
getShortTime,
8+
getShortUUID,
9+
useGoRuntimeLogListener,
10+
useProjectJobListener,
11+
useStartShipOnMount,
12+
} from '@cli/utils/index.js'
413
import {getJobBuildsRetry} from '@cli/api/index.js'
514

6-
import {CommandContext, GameContext, JobProgress, QRCodeTerminal} from './index.js'
15+
import {CommandContext, GameContext, JobProgress, QRCodeTerminal, Title} from './index.js'
716
import {Job, Platform} from '@cli/types/api.js'
8-
import {useGoRuntimeLogListener} from '@cli/utils/hooks/useGoRuntimeLogListener.js'
917

1018
interface Props {
1119
onComplete: () => void
@@ -25,8 +33,27 @@ interface GoCommandProps extends Props {
2533
}
2634

2735
const LogListener = ({projectId, buildId}: {projectId: string; buildId: string}) => {
28-
useGoRuntimeLogListener({projectId, buildId})
29-
return null
36+
const {tail, messages} = useGoRuntimeLogListener({projectId, buildId})
37+
38+
return (
39+
<>
40+
<Box flexDirection="column">
41+
{messages.map((log, i) => {
42+
const messageColor = getRuntimeLogLevelColor(log.level)
43+
return (
44+
<Box flexDirection="row" height={1} key={i} overflow="hidden">
45+
<Box>
46+
<Text>{getShortTime(log.sentAt)}</Text>
47+
</Box>
48+
<Box height={1} marginLeft={1} marginRight={2} overflow="hidden">
49+
<TruncatedText color={messageColor}>{log.message}</TruncatedText>
50+
</Box>
51+
</Box>
52+
)
53+
})}
54+
</Box>
55+
</>
56+
)
3057
}
3158

3259
const GoCommand = ({command, gameId, onComplete, onError}: GoCommandProps): JSX.Element | null => {
@@ -60,15 +87,22 @@ const GoCommand = ({command, gameId, onComplete, onError}: GoCommandProps): JSX.
6087

6188
if (qrCodeData && buildId) {
6289
return (
63-
<>
90+
<Box flexDirection='column'>
91+
<Title>Go Build QR Code</Title>
6492
<QRCodeTerminal data={qrCodeData} />
93+
<Text>{`Go build ID: ${getShortUUID(buildId)}`}</Text>
6594
<LogListener projectId={gameId} buildId={buildId} />
66-
</>
95+
</Box>
6796
)
6897
}
6998

7099
if (startedJobs && startedJobs?.length > 0) {
71-
return <JobProgress job={startedJobs[0]} />
100+
return (
101+
<Box flexDirection='column'>
102+
<Text>Generating Go build, please wait...</Text>
103+
<JobProgress job={startedJobs[0]} />
104+
</Box>
105+
)
72106
}
73107

74108
return null

src/components/Ship/ShipResult.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const ShipResult = ({gameId, failedJobs, gameFlags}: ShipResultProps) =>
2222
templateVars={{
2323
gameBuildsUrl: `${WEB_URL}games/${getShortUUID(gameId)}/builds`,
2424
wasPublished: !gameFlags?.skipPublish,
25+
usedDemoCredentials: !!gameFlags?.useDemoCredentials,
2526
}}
2627
/>
2728
)}

src/types/api.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,19 @@ export interface TermsResponse {
270270
changes: AgreementVersion[]
271271
current: AgreementVersion[]
272272
}
273+
274+
export enum RuntimeLogLevel {
275+
VERBOSE = 'VERBOSE',
276+
DEBUG = 'DEBUG',
277+
INFO = 'INFO',
278+
WARNING = 'WARNING',
279+
ERROR = 'ERROR',
280+
}
281+
282+
export interface RuntimeLogEntry {
283+
buildId: string
284+
level: RuntimeLogLevel
285+
message: string
286+
details?: any
287+
sentAt: DateTime
288+
}

src/utils/hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from './useAndroidServiceAccount.js'
22
export * from './useGoogleStatusWatching.js'
3+
export * from './useGoRuntimeLogListener.js'
34
export * from './useJobLogTail.js'
45
export * from './useJobWatching.js'
56
export * from './useProjectJobListener.js'
Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,44 @@
1+
import {useState} from 'react'
2+
import {DateTime} from 'luxon'
3+
14
import {WebSocketListener, useWebSocket} from './useWebSocket.js'
5+
import {RuntimeLogEntry} from '@cli/types/api.js'
26

3-
export interface GoRuntimeLogListenerProps {
7+
interface Props {
48
projectId: string
59
buildId: string
10+
tailLength?: number
11+
}
12+
13+
interface Response {
14+
messages: RuntimeLogEntry[]
15+
tail: RuntimeLogEntry[]
616
}
717

8-
export function useGoRuntimeLogListener({projectId, buildId}: GoRuntimeLogListenerProps) {
18+
// Listens for Go runtime logs for a build via WebSocket and gives a tail
19+
export function useGoRuntimeLogListener({projectId, buildId, tailLength = 10}: Props): Response {
20+
const [messages, setMessages] = useState<RuntimeLogEntry[]>([])
21+
const [tail, setTail] = useState<RuntimeLogEntry[]>([])
22+
923
const listener: WebSocketListener = {
10-
async eventHandler(pattern: string, rawLog: any) {
11-
console.log(`[Go Runtime Log] ${JSON.stringify(rawLog)}`)
24+
async eventHandler(_: string, rawLog: any) {
25+
const log: RuntimeLogEntry = {
26+
...rawLog,
27+
sentAt: DateTime.fromISO(rawLog.sentAt),
28+
}
29+
setMessages((prev) => [...prev, log])
30+
setTail((prev) => {
31+
const next = [...prev, log]
32+
if (next.length > tailLength) next.shift()
33+
return next
34+
})
35+
//console.log(log.message)
1236
},
37+
1338
getPattern: () => [`project.${projectId}:build.${buildId}:runtime-log`],
1439
}
1540

1641
useWebSocket([listener])
42+
43+
return {messages, tail}
1744
}

src/utils/index.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {fileURLToPath} from 'node:url'
66

77
import readlineSync from 'readline-sync'
88

9-
import {JobStage, JobStatus, LogLevel, Platform, ScalarDict} from '@cli/types'
9+
import {JobStage, JobStatus, LogLevel, Platform, RuntimeLogLevel, ScalarDict} from '@cli/types'
1010

1111
export * from './dates.js'
1212
export * from './dictionary.js'
@@ -85,6 +85,18 @@ export function getJobStatusColor(status: JobStatus) {
8585
}
8686
}
8787

88+
// For the "Go" runtime logs
89+
const RUNTIME_LOG_LEVEL_COLORS: Record<RuntimeLogLevel, string> = {
90+
[RuntimeLogLevel.VERBOSE]: '#E8E8FF',
91+
[RuntimeLogLevel.DEBUG]: '#E6F0FF',
92+
[RuntimeLogLevel.INFO]: '#E6FFE6',
93+
[RuntimeLogLevel.WARNING]: '#FFF6CC',
94+
[RuntimeLogLevel.ERROR]: '#FFD6D6',
95+
}
96+
97+
export const getRuntimeLogLevelColor = (level: RuntimeLogLevel) =>
98+
RUNTIME_LOG_LEVEL_COLORS[level]
99+
88100
export async function getFileHash(filename: string): Promise<string> {
89101
return new Promise((resolve, reject) => {
90102
const hash = crypto.createHash('sha256')

0 commit comments

Comments
 (0)