1+ import os from 'node:os' ;
12import { __ , _n , sprintf } from '@wordpress/i18n' ;
23import Table from 'cli-table3' ;
3- import { PreviewCommandLoggerAction as LoggerAction } from 'common/logger-actions' ;
4- import { readAppdata , type SiteData } from 'cli/lib/appdata' ;
4+ import { SiteCommandLoggerAction as LoggerAction } from 'common/logger-actions' ;
5+ import { getSiteUrl , readAppdata , type SiteData } from 'cli/lib/appdata' ;
6+ import { connect , disconnect } from 'cli/lib/pm2-manager' ;
7+ import { getColumnWidths } from 'cli/lib/utils' ;
8+ import { isServerRunning } from 'cli/lib/wordpress-server-manager' ;
59import { Logger , LoggerError } from 'cli/logger' ;
610import { StudioArgv } from 'cli/types' ;
711
8- interface SiteTable {
9- id : string ;
12+ interface SiteListEntry {
13+ status : string ;
1014 name : string ;
1115 path : string ;
12- phpVersion : string ;
16+ url : string ;
1317}
1418
15- function getSitesCliTable ( sites : SiteData [ ] ) {
16- const table = new Table ( {
17- head : [ __ ( 'Name' ) , __ ( 'Path' ) , __ ( 'ID' ) , __ ( 'PHP' ) ] ,
18- style : {
19- head : [ 'cyan' ] ,
20- border : [ 'grey' ] ,
21- } ,
22- wordWrap : true ,
23- wrapOnWordBoundary : false ,
24- } ) ;
19+ function getPrettyPath ( path : string ) : string {
20+ return path . replace ( process . cwd ( ) , '.' ) . replace ( os . homedir ( ) , '~' ) ;
21+ }
2522
26- sites . forEach ( ( site ) => {
27- table . push ( [ site . name , site . path , site . id , site . phpVersion ] ) ;
28- } ) ;
23+ async function getSiteListData ( sites : SiteData [ ] ) {
24+ const result : SiteListEntry [ ] = [ ] ;
2925
30- return table ;
31- }
26+ for await ( const site of sites ) {
27+ const isOnline = await isServerRunning ( site . id ) ;
28+ const status = isOnline ? '🟢 Online' : '🔴 Offline' ;
29+ const url = getSiteUrl ( site ) ;
30+
31+ result . push ( {
32+ status,
33+ name : site . name ,
34+ path : getPrettyPath ( site . path ) ,
35+ url,
36+ } ) ;
37+ }
3238
33- function getSitesCliJson ( sites : SiteData [ ] ) : SiteTable [ ] {
34- return sites . map ( ( site ) => ( {
35- id : site . id ,
36- name : site . name ,
37- path : site . path ,
38- phpVersion : site . phpVersion ,
39- } ) ) ;
39+ return result ;
4040}
4141
4242export async function runCommand ( format : 'table' | 'json' ) : Promise < void > {
4343 const logger = new Logger < LoggerAction > ( ) ;
4444
4545 try {
46- logger . reportStart ( LoggerAction . LOAD , __ ( 'Loading sites…' ) ) ;
46+ logger . reportStart ( LoggerAction . LOAD_SITES , __ ( 'Loading sites…' ) ) ;
4747 const appdata = await readAppdata ( ) ;
48- const allSites = appdata . sites ;
4948
50- if ( allSites . length === 0 ) {
49+ if ( appdata . sites . length === 0 ) {
5150 logger . reportSuccess ( __ ( 'No sites found' ) ) ;
5251 return ;
5352 }
5453
5554 const sitesMessage = sprintf (
56- _n ( 'Found %d site' , 'Found %d sites' , allSites . length ) ,
57- allSites . length
55+ _n ( 'Found %d site' , 'Found %d sites' , appdata . sites . length ) ,
56+ appdata . sites . length
5857 ) ;
5958
6059 logger . reportSuccess ( sitesMessage ) ;
6160
61+ logger . reportStart ( LoggerAction . START_DAEMON , __ ( 'Connecting to process daemon...' ) ) ;
62+ await connect ( ) ;
63+ logger . reportSuccess ( __ ( 'Connected to process daemon' ) ) ;
64+
65+ const sitesData = await getSiteListData ( appdata . sites ) ;
66+
6267 if ( format === 'table' ) {
63- const table = getSitesCliTable ( allSites ) ;
68+ const colWidths = getColumnWidths ( [ 0.1 , 0.2 , 0.3 , 0.4 ] ) ;
69+
70+ const table = new Table ( {
71+ head : [ __ ( 'Status' ) , __ ( 'Name' ) , __ ( 'Path' ) , __ ( 'URL' ) ] ,
72+ wordWrap : true ,
73+ wrapOnWordBoundary : false ,
74+ colWidths,
75+ style : {
76+ head : [ ] ,
77+ border : [ ] ,
78+ } ,
79+ } ) ;
80+
81+ table . push (
82+ ...sitesData . map ( ( site ) => [
83+ site . status ,
84+ site . name ,
85+ site . path ,
86+ { href : new URL ( site . url ) . toString ( ) , content : site . url } ,
87+ ] )
88+ ) ;
89+
6490 console . log ( table . toString ( ) ) ;
6591 } else {
66- console . log ( JSON . stringify ( getSitesCliJson ( allSites ) , null , 2 ) ) ;
92+ console . log ( JSON . stringify ( sitesData , null , 2 ) ) ;
6793 }
6894 } catch ( error ) {
6995 if ( error instanceof LoggerError ) {
@@ -72,6 +98,8 @@ export async function runCommand( format: 'table' | 'json' ): Promise< void > {
7298 const loggerError = new LoggerError ( __ ( 'Failed to load sites' ) , error ) ;
7399 logger . reportError ( loggerError ) ;
74100 }
101+ } finally {
102+ disconnect ( ) ;
75103 }
76104}
77105
0 commit comments