@@ -3,8 +3,7 @@ import { readFile, writeFile } from "fs/promises";
33import { join } from "path" ;
44import { spawn } from "child_process" ;
55import { env } from "~/env" ;
6- import { observable } from '@trpc/server/observable' ;
7- import { existsSync , statSync , createWriteStream } from "fs" ;
6+ import { existsSync , createWriteStream } from "fs" ;
87
98interface GitHubRelease {
109 tag_name : string ;
@@ -127,52 +126,45 @@ export const versionRouter = createTRPCRouter({
127126 }
128127 } ) ,
129128
130- // Stream update progress by monitoring the log file
131- streamUpdateProgress : publicProcedure
132- . subscription ( ( ) => {
133- return observable < { type : 'log' | 'complete' | 'error' ; message : string } > ( ( emit ) => {
129+ // Get update logs from the log file
130+ getUpdateLogs : publicProcedure
131+ . query ( async ( ) => {
132+ try {
134133 const logPath = join ( process . cwd ( ) , 'update.log' ) ;
135- let lastSize = 0 ;
136-
137- const checkLogFile = async ( ) => {
138- try {
139- if ( ! existsSync ( logPath ) ) {
140- return ;
141- }
142-
143- const stats = statSync ( logPath ) ;
144- const currentSize = stats . size ;
145-
146- if ( currentSize > lastSize ) {
147- // Read only the new content
148- const fileHandle = await readFile ( logPath , 'utf-8' ) ;
149- const newContent = fileHandle . slice ( lastSize ) ;
150- lastSize = currentSize ;
151-
152- // Emit new log lines
153- const lines = newContent . split ( '\n' ) . filter ( line => line . trim ( ) ) ;
154- for ( const line of lines ) {
155- emit . next ( { type : 'log' , message : line } ) ;
156- }
157- }
158- } catch {
159- // File might be being written to, ignore errors
160- }
161- } ;
162-
163- // Start monitoring the log file
164- const checkInterval = setInterval ( ( ) => {
165- void checkLogFile ( ) ;
166- } , 500 ) ;
134+
135+ if ( ! existsSync ( logPath ) ) {
136+ return {
137+ success : true ,
138+ logs : [ ] ,
139+ isComplete : false
140+ } ;
141+ }
167142
168- // Also check immediately
169- void checkLogFile ( ) ;
143+ const logs = await readFile ( logPath , 'utf-8' ) ;
144+ const logLines = logs . split ( '\n' ) . filter ( line => line . trim ( ) ) ;
145+
146+ // Check if update is complete by looking for completion indicators
147+ const isComplete = logLines . some ( line =>
148+ line . includes ( 'Update complete' ) ||
149+ line . includes ( 'Server restarting' ) ||
150+ line . includes ( 'npm start' ) ||
151+ line . includes ( 'Restarting server' )
152+ ) ;
170153
171- // Cleanup function
172- return ( ) => {
173- clearInterval ( checkInterval ) ;
154+ return {
155+ success : true ,
156+ logs : logLines ,
157+ isComplete
174158 } ;
175- } ) ;
159+ } catch ( error ) {
160+ console . error ( 'Error reading update logs:' , error ) ;
161+ return {
162+ success : false ,
163+ error : error instanceof Error ? error . message : 'Failed to read update logs' ,
164+ logs : [ ] ,
165+ isComplete : false
166+ } ;
167+ }
176168 } ) ,
177169
178170 // Execute update script
0 commit comments