Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
143 changes: 0 additions & 143 deletions cli/commands/site/stop-all.ts

This file was deleted.

149 changes: 124 additions & 25 deletions cli/commands/site/stop.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { __ } from '@wordpress/i18n';
import { __, _n, sprintf } from '@wordpress/i18n';
import { SiteCommandLoggerAction as LoggerAction } from 'common/logger-actions';
import { clearSiteLatestCliPid, getSiteByFolder, updateSiteAutoStart } from 'cli/lib/appdata';
import {
clearSiteLatestCliPid,
getSiteByFolder,
readAppdata,
updateSiteAutoStart,
type SiteData,
} from 'cli/lib/appdata';
import { connect, disconnect } from 'cli/lib/pm2-manager';
import { stopProxyIfNoSitesNeedIt } from 'cli/lib/site-utils';
import { isServerRunning, stopWordPressServer } from 'cli/lib/wordpress-server-manager';
Expand All @@ -9,27 +15,107 @@ import { StudioArgv } from 'cli/types';

const logger = new Logger< LoggerAction >();

export async function runCommand( siteFolder: string, autoStart: boolean ): Promise< void > {
try {
const site = await getSiteByFolder( siteFolder );
export enum Mode {
STOP_SINGLE_SITE,
STOP_ALL_SITES,
}

export async function runCommand(
target: Mode.STOP_SINGLE_SITE,
siteFolder: string,
autoStart: boolean
): Promise< void >;
export async function runCommand(
target: Mode.STOP_ALL_SITES,
siteFolder: undefined,
autoStart: boolean
): Promise< void >;
export async function runCommand(
target: Mode,
siteFolder: string | undefined,
autoStart: boolean
): Promise< void > {
try {
await connect();

const runningProcess = await isServerRunning( site.id );
if ( ! runningProcess ) {
logger.reportSuccess( __( 'WordPress site is not running' ) );
return;
const SITES_TO_TARGET: SiteData[] = [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Should we rename it to sitesToTarget?


if ( target === Mode.STOP_SINGLE_SITE && siteFolder ) {
const site = await getSiteByFolder( siteFolder );
SITES_TO_TARGET.push( site );

const runningProcess = await isServerRunning( site.id );
if ( ! runningProcess ) {
logger.reportSuccess( __( 'WordPress site is not running' ) );
return;
}
} else {
const appdata = await readAppdata();

for ( const site of appdata.sites ) {
const runningProcess = await isServerRunning( site.id );

if ( runningProcess ) {
SITES_TO_TARGET.push( site );
}
}

if ( ! SITES_TO_TARGET.length ) {
logger.reportSuccess( __( 'No sites are currently running' ) );
return;
}

logger.reportStart(
LoggerAction.STOP_ALL_SITES,
sprintf( __( 'Stopping all WordPress sites... (%d/%d)' ), 0, SITES_TO_TARGET.length )
);
}

const stoppedSiteIds: string[] = [];

for ( const site of SITES_TO_TARGET ) {
try {
logger.reportProgress(
sprintf(
__( 'Stopping site "%s" (%d/%d)…' ),
site.name,
stoppedSiteIds.length + 1,
SITES_TO_TARGET.length
)
);
await stopWordPressServer( site.id );
await clearSiteLatestCliPid( site.id );
await updateSiteAutoStart( site.id, autoStart );

stoppedSiteIds.push( site.id );
} catch ( error ) {
logger.reportError(
new LoggerError( sprintf( __( 'Failed to stop site %s' ), site.name ) )
);
}
}

logger.reportStart( LoggerAction.STOP_SITE, __( 'Stopping WordPress site…' ) );
try {
await stopWordPressServer( site.id );
await clearSiteLatestCliPid( site.id );
await updateSiteAutoStart( site.id, autoStart );
logger.reportSuccess( __( 'WordPress site stopped' ) );
await stopProxyIfNoSitesNeedIt( site.id, logger );
await stopProxyIfNoSitesNeedIt( stoppedSiteIds, logger );
} catch ( error ) {
throw new LoggerError( __( 'Failed to stop WordPress server' ), error );
throw new LoggerError( __( 'Failed to stop proxy server' ), error );
}

if ( stoppedSiteIds.length === SITES_TO_TARGET.length ) {
logger.reportSuccess(
sprintf(
_n(
'Successfully stopped %d site',
'Successfully stopped %d sites',
SITES_TO_TARGET.length
),
SITES_TO_TARGET.length
)
);
} else {
throw new LoggerError(
sprintf( __( 'Stopped %d sites out of %d' ), stoppedSiteIds.length, SITES_TO_TARGET.length )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Should we handle single site error message differently?
Stopped 0 sites out of 1 seems strange.

);
}
} finally {
disconnect();
Expand All @@ -39,23 +125,36 @@ export async function runCommand( siteFolder: string, autoStart: boolean ): Prom
export const registerCommand = ( yargs: StudioArgv ) => {
return yargs.command( {
command: 'stop',
describe: __( 'Stop local site' ),
describe: __( 'Stop local site(s)' ),
builder: ( yargs ) => {
return yargs.option( 'auto-start', {
type: 'boolean',
describe: __( 'Set auto-start flag for the site' ),
default: false,
hidden: true,
} );
return yargs
.option( 'all', {
type: 'boolean',
describe: __( 'Stop all local sites' ),
default: false,
} )
.option( 'auto-start', {
type: 'boolean',
describe: __( 'Set auto-start flag for the site(s)' ),
default: false,
hidden: true,
} );
},
handler: async ( argv ) => {
try {
await runCommand( argv.path, argv.autoStart );
if ( argv.all ) {
await runCommand( Mode.STOP_ALL_SITES, undefined, argv.autoStart );
} else {
await runCommand( Mode.STOP_SINGLE_SITE, argv.path, argv.autoStart );
}
} catch ( error ) {
if ( error instanceof LoggerError ) {
logger.reportError( error );
} else {
const loggerError = new LoggerError( __( 'Failed to stop site' ), error );
const loggerError = new LoggerError(
argv.all ? __( 'Failed to stop sites' ) : __( 'Failed to stop site' ),
error
);
logger.reportError( loggerError );
}
}
Expand Down
Loading