1- import { __ } from '@wordpress/i18n' ;
1+ import { __ , _n , sprintf } from '@wordpress/i18n' ;
22import { SiteCommandLoggerAction as LoggerAction } from 'common/logger-actions' ;
3- import { clearSiteLatestCliPid , getSiteByFolder , updateSiteAutoStart } from 'cli/lib/appdata' ;
3+ import {
4+ clearSiteLatestCliPid ,
5+ getSiteByFolder ,
6+ readAppdata ,
7+ updateSiteAutoStart ,
8+ type SiteData ,
9+ } from 'cli/lib/appdata' ;
410import { connect , disconnect } from 'cli/lib/pm2-manager' ;
511import { stopProxyIfNoSitesNeedIt } from 'cli/lib/site-utils' ;
612import { isServerRunning , stopWordPressServer } from 'cli/lib/wordpress-server-manager' ;
@@ -9,6 +15,20 @@ import { StudioArgv } from 'cli/types';
915
1016const logger = new Logger < LoggerAction > ( ) ;
1117
18+ const filterRunningSites = async ( sites : SiteData [ ] ) : Promise < SiteData [ ] > => {
19+ const runningSites = [ ] ;
20+
21+ for ( const site of sites ) {
22+ const runningProcess = await isServerRunning ( site . id ) ;
23+
24+ if ( runningProcess ) {
25+ runningSites . push ( site ) ;
26+ }
27+ }
28+
29+ return runningSites ;
30+ } ;
31+
1232export async function runCommand ( siteFolder : string , autoStart : boolean ) : Promise < void > {
1333 try {
1434 const site = await getSiteByFolder ( siteFolder ) ;
@@ -36,26 +56,122 @@ export async function runCommand( siteFolder: string, autoStart: boolean ): Prom
3656 }
3757}
3858
59+ export async function runCommandAll ( autoStart : boolean ) : Promise < void > {
60+ try {
61+ const appdata = await readAppdata ( ) ;
62+ const allSites = appdata . sites ;
63+
64+ if ( ! allSites . length ) {
65+ logger . reportSuccess ( __ ( 'No sites found' ) ) ;
66+ return ;
67+ }
68+
69+ await connect ( ) ;
70+
71+ const runningSites = await filterRunningSites ( allSites ) ;
72+
73+ if ( ! runningSites . length ) {
74+ logger . reportSuccess ( __ ( 'No sites are currently running' ) ) ;
75+ return ;
76+ }
77+
78+ const stoppedSiteIds : string [ ] = [ ] ;
79+
80+ logger . reportStart (
81+ LoggerAction . STOP_ALL_SITES ,
82+ sprintf (
83+ __ ( 'Stopping all WordPress sites... (%d/%d)' ) ,
84+ stoppedSiteIds . length ,
85+ runningSites . length
86+ )
87+ ) ;
88+
89+ for ( const site of runningSites ) {
90+ try {
91+ logger . reportProgress (
92+ sprintf (
93+ __ ( 'Stopping site "%s" (%d/%d)…' ) ,
94+ site . name ,
95+ stoppedSiteIds . length + 1 ,
96+ runningSites . length
97+ )
98+ ) ;
99+ await stopWordPressServer ( site . id ) ;
100+ await clearSiteLatestCliPid ( site . id ) ;
101+ await updateSiteAutoStart ( site . id , autoStart ) ;
102+
103+ stoppedSiteIds . push ( site . id ) ;
104+ } catch ( error ) {
105+ logger . reportError (
106+ new LoggerError ( sprintf ( __ ( 'Failed to stop site %s' ) , site . name ) )
107+ ) ;
108+ }
109+ }
110+
111+ try {
112+ await stopProxyIfNoSitesNeedIt ( stoppedSiteIds , logger ) ;
113+ } catch ( error ) {
114+ throw new LoggerError ( __ ( 'Failed to stop proxy server' ) , error ) ;
115+ }
116+
117+ if ( stoppedSiteIds . length === runningSites . length ) {
118+ logger . reportSuccess (
119+ sprintf (
120+ _n (
121+ 'Successfully stopped %d site' ,
122+ 'Successfully stopped %d sites' ,
123+ runningSites . length
124+ ) ,
125+ runningSites . length
126+ )
127+ ) ;
128+ } else if ( stoppedSiteIds . length === 0 ) {
129+ throw new LoggerError (
130+ sprintf ( __ ( 'Failed to stop all (%d) sites' ) , runningSites . length )
131+ ) ;
132+ } else {
133+ throw new LoggerError (
134+ sprintf ( __ ( 'Stopped %d sites out of %d' ) , stoppedSiteIds . length , runningSites . length )
135+ ) ;
136+ }
137+ } finally {
138+ disconnect ( ) ;
139+ }
140+ }
141+
39142export const registerCommand = ( yargs : StudioArgv ) => {
40143 return yargs . command ( {
41144 command : 'stop' ,
42- describe : __ ( 'Stop local site' ) ,
145+ describe : __ ( 'Stop local site(s) ' ) ,
43146 builder : ( yargs ) => {
44- return yargs . option ( 'auto-start' , {
45- type : 'boolean' ,
46- describe : __ ( 'Set auto-start flag for the site' ) ,
47- default : false ,
48- hidden : true ,
49- } ) ;
147+ return yargs
148+ . option ( 'all' , {
149+ type : 'boolean' ,
150+ describe : __ ( 'Stop all local sites' ) ,
151+ default : false ,
152+ } )
153+ . option ( 'auto-start' , {
154+ type : 'boolean' ,
155+ describe : __ ( 'Set auto-start flag for the site(s)' ) ,
156+ default : false ,
157+ hidden : true ,
158+ } ) ;
50159 } ,
51160 handler : async ( argv ) => {
52161 try {
53- await runCommand ( argv . path , argv . autoStart ) ;
162+ if ( argv . all ) {
163+ await runCommandAll ( argv . autoStart ) ;
164+ } else {
165+ await runCommand ( argv . path , argv . autoStart ) ;
166+ }
54167 } catch ( error ) {
55168 if ( error instanceof LoggerError ) {
56169 logger . reportError ( error ) ;
57170 } else {
58- const loggerError = new LoggerError ( __ ( 'Failed to stop site' ) , error ) ;
171+ const loggerError = new LoggerError (
172+ argv . all ? __ ( 'Failed to stop sites' ) : __ ( 'Failed to stop site' ) ,
173+ error
174+ ) ;
59175 logger . reportError ( loggerError ) ;
60176 }
61177 }
0 commit comments