|
| 1 | +import { __, _n, sprintf } from '@wordpress/i18n'; |
| 2 | +import Table from 'cli-table3'; |
| 3 | +import { PreviewCommandLoggerAction as LoggerAction } from 'common/logger-actions'; |
| 4 | +import { ensureDaemonRunning, listProcesses } from 'cli/lib/pm2-manager'; |
| 5 | +import { Logger, LoggerError } from 'cli/logger'; |
| 6 | +import { StudioArgv } from 'cli/types'; |
| 7 | + |
| 8 | +export async function runCommand( format: 'table' | 'json' ): Promise< void > { |
| 9 | + const logger = new Logger< LoggerAction >(); |
| 10 | + |
| 11 | + try { |
| 12 | + logger.reportStart( LoggerAction.LOAD, __( 'Loading PM2 processes…' ) ); |
| 13 | + await ensureDaemonRunning(); |
| 14 | + const processes = await listProcesses(); |
| 15 | + |
| 16 | + if ( processes.length === 0 ) { |
| 17 | + logger.reportSuccess( __( 'No processes found' ) ); |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + const message = sprintf( |
| 22 | + _n( 'Found %d process', 'Found %d processes', processes.length ), |
| 23 | + processes.length |
| 24 | + ); |
| 25 | + logger.reportSuccess( message ); |
| 26 | + |
| 27 | + if ( format === 'table' ) { |
| 28 | + const table = new Table( { |
| 29 | + head: [ __( 'Name' ), __( 'PID' ), __( 'Status' ), __( 'Uptime' ) ], |
| 30 | + style: { |
| 31 | + head: [ 'cyan' ], |
| 32 | + border: [ 'grey' ], |
| 33 | + }, |
| 34 | + wordWrap: true, |
| 35 | + wrapOnWordBoundary: false, |
| 36 | + } ); |
| 37 | + |
| 38 | + processes.forEach( ( proc ) => { |
| 39 | + const uptimeSeconds = Math.floor( proc.pm2_env.uptime / 1000 ); |
| 40 | + const uptimeMinutes = Math.floor( uptimeSeconds / 60 ); |
| 41 | + const uptimeHours = Math.floor( uptimeMinutes / 60 ); |
| 42 | + const uptimeDisplay = |
| 43 | + uptimeHours > 0 |
| 44 | + ? sprintf( '%dh %dm', uptimeHours, uptimeMinutes % 60 ) |
| 45 | + : sprintf( '%dm %ds', uptimeMinutes, uptimeSeconds % 60 ); |
| 46 | + |
| 47 | + table.push( [ proc.name, proc.pid.toString(), proc.status, uptimeDisplay ] ); |
| 48 | + } ); |
| 49 | + |
| 50 | + console.log( table.toString() ); |
| 51 | + } else { |
| 52 | + const jsonOutput = processes.map( ( proc ) => ( { |
| 53 | + name: proc.name, |
| 54 | + pid: proc.pid, |
| 55 | + pm_id: proc.pm_id, |
| 56 | + status: proc.status, |
| 57 | + uptime: proc.pm2_env.uptime, |
| 58 | + } ) ); |
| 59 | + console.log( JSON.stringify( jsonOutput, null, 2 ) ); |
| 60 | + } |
| 61 | + } catch ( error ) { |
| 62 | + if ( error instanceof LoggerError ) { |
| 63 | + logger.reportError( error ); |
| 64 | + } else { |
| 65 | + const loggerError = new LoggerError( __( 'Failed to list PM2 processes' ), error ); |
| 66 | + logger.reportError( loggerError ); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +export const registerCommand = ( yargs: StudioArgv ) => { |
| 72 | + return yargs.command( { |
| 73 | + command: 'list', |
| 74 | + describe: __( 'List all PM2 processes' ), |
| 75 | + builder: ( yargs ) => { |
| 76 | + return yargs.option( 'format', { |
| 77 | + type: 'string', |
| 78 | + choices: [ 'table', 'json' ], |
| 79 | + default: 'table', |
| 80 | + description: __( 'Output format' ), |
| 81 | + } ); |
| 82 | + }, |
| 83 | + handler: async ( argv ) => { |
| 84 | + await runCommand( argv.format as 'table' | 'json' ); |
| 85 | + }, |
| 86 | + } ); |
| 87 | +}; |
| 88 | + |
0 commit comments