From da3159fcfc6dcd9e65a2a685a786f983169540e6 Mon Sep 17 00:00:00 2001 From: bcotrim Date: Fri, 19 Dec 2025 09:07:05 +0000 Subject: [PATCH] add studio cli logs to studio --- src/modules/cli/lib/cli-server-process.ts | 26 +++++++++++++++++------ src/modules/cli/lib/cli-site-creator.ts | 7 +++++- src/modules/cli/lib/execute-command.ts | 9 +++++++- src/site-server.ts | 5 ++++- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/modules/cli/lib/cli-server-process.ts b/src/modules/cli/lib/cli-server-process.ts index 013c94a86..9e3d851b4 100644 --- a/src/modules/cli/lib/cli-server-process.ts +++ b/src/modules/cli/lib/cli-server-process.ts @@ -1,6 +1,14 @@ +import { z } from 'zod'; +import { SiteCommandLoggerAction } from 'common/logger-actions'; import { executeCliCommand } from './execute-command'; import type { WordPressServerProcess } from 'src/lib/wordpress-provider/types'; +const cliEventSchema = z.object( { + action: z.nativeEnum( SiteCommandLoggerAction ), + status: z.enum( [ 'inprogress', 'fail', 'success', 'warning' ] ), + message: z.string(), +} ); + /** * A WordPressServerProcess implementation that delegates to CLI commands. * Used when a site is started via CLI and we need to represent it in the desktop app. @@ -19,13 +27,17 @@ export class CliServerProcess implements WordPressServerProcess { async start(): Promise< void > { return new Promise( ( resolve, reject ) => { - const [ emitter ] = executeCliCommand( [ - 'site', - 'start', - '--path', - this.sitePath, - '--skip-browser', - ] ); + const [ emitter ] = executeCliCommand( + [ 'site', 'start', '--path', this.sitePath, '--skip-browser' ], + { output: 'capture', logPrefix: this.siteId } + ); + + emitter.on( 'data', ( { data } ) => { + const parsed = cliEventSchema.safeParse( data ); + if ( parsed.success && parsed.data.status === 'inprogress' ) { + console.log( `[CLI - ${ this.siteId }] ${ parsed.data.message }` ); + } + } ); emitter.on( 'success', () => { resolve(); diff --git a/src/modules/cli/lib/cli-site-creator.ts b/src/modules/cli/lib/cli-site-creator.ts index 5b3270be8..3212aed33 100644 --- a/src/modules/cli/lib/cli-site-creator.ts +++ b/src/modules/cli/lib/cli-site-creator.ts @@ -52,7 +52,7 @@ export async function createSiteViaCli( options: CreateSiteOptions ): Promise< C const result: Partial< CreateSiteResult > = {}; let lastErrorMessage: string | null = null; - const [ emitter ] = executeCliCommand( args ); + const [ emitter ] = executeCliCommand( args, { output: 'capture', logPrefix: siteId } ); emitter.on( 'data', ( { data } ) => { const parsed = cliEventSchema.safeParse( data ); @@ -60,6 +60,11 @@ export async function createSiteViaCli( options: CreateSiteOptions ): Promise< C return; } + if ( parsed.data.action !== 'keyValuePair' && parsed.data.status === 'inprogress' ) { + const prefix = siteId ? `[CLI - ${ siteId }]` : '[CLI]'; + console.log( `${ prefix } ${ parsed.data.message }` ); + } + if ( parsed.data.action === 'keyValuePair' ) { const { key, value } = parsed.data; if ( key === 'id' ) { diff --git a/src/modules/cli/lib/execute-command.ts b/src/modules/cli/lib/execute-command.ts index 0cdb0d01d..2ba177960 100644 --- a/src/modules/cli/lib/execute-command.ts +++ b/src/modules/cli/lib/execute-command.ts @@ -40,6 +40,7 @@ export interface ExecuteCliCommandOptions { * - 'capture': capture stdout/stderr, available in success/failure events */ output: 'ignore' | 'capture'; + logPrefix?: string; } export function executeCliCommand( @@ -69,8 +70,14 @@ export function executeCliCommand( let stderr = ''; if ( options.output === 'capture' ) { + const logPrefix = options.logPrefix ? `[CLI - ${ options.logPrefix }]` : '[CLI]'; child.stdout?.on( 'data', ( data: Buffer ) => { - stdout += data.toString(); + const text = data.toString(); + stdout += text; + const trimmed = text.trimEnd(); + if ( trimmed ) { + console.log( `${ logPrefix } ${ trimmed }` ); + } } ); child.stderr?.on( 'data', ( data: Buffer ) => { stderr += data.toString(); diff --git a/src/site-server.ts b/src/site-server.ts index 18795213d..b2122854b 100644 --- a/src/site-server.ts +++ b/src/site-server.ts @@ -380,7 +380,10 @@ export class SiteServer { let timeoutId: NodeJS.Timeout; return new Promise< WpCliResult >( ( resolve ) => { - const [ emitter, childProcess ] = executeCliCommand( cliArgs, { output: 'capture' } ); + const [ emitter, childProcess ] = executeCliCommand( cliArgs, { + output: 'capture', + logPrefix: this.details.id, + } ); timeoutId = setTimeout( () => { childProcess.kill();