Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions src/modules/cli/lib/cli-server-process.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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();
Expand Down
7 changes: 6 additions & 1 deletion src/modules/cli/lib/cli-site-creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,19 @@ 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 );
if ( ! parsed.success ) {
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' ) {
Expand Down
9 changes: 8 additions & 1 deletion src/modules/cli/lib/execute-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface ExecuteCliCommandOptions {
* - 'capture': capture stdout/stderr, available in success/failure events
*/
output: 'ignore' | 'capture';
logPrefix?: string;
}

export function executeCliCommand(
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 4 additions & 1 deletion src/site-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading