@@ -8,45 +8,7 @@ import { asBold, asTimestamp, withUnderline } from 'src/utils/format'
88import { listSandboxes } from './list'
99import { wait } from 'src/utils/wait'
1010import { handleE2BRequestError } from '../../utils/errors'
11-
12- const maxRuntime = 24 * 60 * 60 * 1000 // 24 hours in milliseconds
13-
14- function getShortID ( sandboxID : string ) {
15- return sandboxID . split ( '-' ) [ 0 ]
16- }
17-
18- function waitForSandboxEnd ( sandboxID : string ) {
19- let isRunning = true
20-
21- async function monitor ( ) {
22- const startTime = new Date ( ) . getTime ( )
23-
24- // eslint-disable-next-line no-constant-condition
25- while ( true ) {
26- const currentTime = new Date ( ) . getTime ( )
27- const elapsedTime = currentTime - startTime // Time elapsed in milliseconds
28-
29- // Check if 24 hours (in milliseconds) have passed
30- if ( elapsedTime >= maxRuntime ) {
31- break
32- }
33-
34- const response = await listSandboxes ( )
35- const sandbox = response . find (
36- ( s ) => s . sandboxID === getShortID ( sandboxID )
37- )
38- if ( ! sandbox ) {
39- isRunning = false
40- break
41- }
42- await wait ( 5000 )
43- }
44- }
45-
46- monitor ( )
47-
48- return ( ) => isRunning
49- }
11+ import { waitForSandboxEnd , formatEnum , Format , getShortID } from './utils'
5012
5113enum LogLevel {
5214 DEBUG = 'DEBUG' ,
@@ -76,17 +38,6 @@ function isLevelIncluded(level: LogLevel, allowedLevel?: LogLevel) {
7638 }
7739}
7840
79- function formatEnum ( e : { [ key : string ] : string } ) {
80- return Object . values ( e )
81- . map ( ( level ) => asBold ( level ) )
82- . join ( ', ' )
83- }
84-
85- enum LogFormat {
86- JSON = 'json' ,
87- PRETTY = 'pretty' ,
88- }
89-
9041function cleanLogger ( logger ?: string ) {
9142 if ( ! logger ) {
9243 return ''
@@ -112,8 +63,8 @@ export const logsCommand = new commander.Command('logs')
11263 . option ( '-f, --follow' , 'keep streaming logs until the sandbox is closed' )
11364 . option (
11465 '--format <format>' ,
115- `specify format for printing logs (${ formatEnum ( LogFormat ) } )` ,
116- LogFormat . PRETTY
66+ `specify format for printing logs (${ formatEnum ( Format ) } )` ,
67+ Format . PRETTY
11768 )
11869 . option (
11970 '--loggers [loggers]' ,
@@ -126,7 +77,7 @@ export const logsCommand = new commander.Command('logs')
12677 opts ?: {
12778 level : string
12879 follow : boolean
129- format : LogFormat
80+ format : Format
13081 loggers ?: string [ ]
13182 }
13283 ) => {
@@ -136,8 +87,8 @@ export const logsCommand = new commander.Command('logs')
13687 throw new Error ( `Invalid log level: ${ level } ` )
13788 }
13889
139- const format = opts ?. format . toLowerCase ( ) as LogFormat | undefined
140- if ( format && ! Object . values ( LogFormat ) . includes ( format ) ) {
90+ const format = opts ?. format . toLowerCase ( ) as Format | undefined
91+ if ( format && ! Object . values ( Format ) . includes ( format ) ) {
14192 throw new Error ( `Invalid log format: ${ format } ` )
14293 }
14394
@@ -149,7 +100,7 @@ export const logsCommand = new commander.Command('logs')
149100 let isFirstRun = true
150101 let firstLogsPrinted = false
151102
152- if ( format === LogFormat . PRETTY ) {
103+ if ( format === Format . PRETTY ) {
153104 console . log ( `\nLogs for sandbox ${ asBold ( sandboxID ) } :` )
154105 }
155106
@@ -178,7 +129,7 @@ export const logsCommand = new commander.Command('logs')
178129 const isRunning = await isRunningPromise
179130
180131 if ( ! isRunning && logs . length === 0 && isFirstRun ) {
181- if ( format === LogFormat . PRETTY ) {
132+ if ( format === Format . PRETTY ) {
182133 console . log (
183134 `\nStopped printing logs — sandbox ${ withUnderline (
184135 'not found'
@@ -189,7 +140,7 @@ export const logsCommand = new commander.Command('logs')
189140 }
190141
191142 if ( ! isRunning ) {
192- if ( format === LogFormat . PRETTY ) {
143+ if ( format === Format . PRETTY ) {
193144 console . log (
194145 `\nStopped printing logs — sandbox is ${ withUnderline (
195146 'closed'
@@ -219,7 +170,7 @@ function printLog(
219170 timestamp : string ,
220171 line : string ,
221172 allowedLevel : LogLevel | undefined ,
222- format : LogFormat | undefined ,
173+ format : Format | undefined ,
223174 allowedLoggers ?: string [ ] | undefined
224175) {
225176 const log = JSON . parse ( line )
@@ -266,7 +217,7 @@ function printLog(
266217 delete log [ 'envID' ]
267218 delete log [ 'sandboxID' ]
268219
269- if ( format === LogFormat . JSON ) {
220+ if ( format === Format . JSON ) {
270221 console . log (
271222 JSON . stringify ( {
272223 timestamp : new Date ( timestamp ) . toISOString ( ) ,
0 commit comments