Skip to content

Commit

Permalink
Merge pull request #1854 from Automattic/add/stop-all
Browse files Browse the repository at this point in the history
feat(dev-env): add `--all` to `vip dev-env stop`
  • Loading branch information
sjinks authored Jun 7, 2024
2 parents 7ece0f0 + de22e2c commit 278fd1c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 16 deletions.
28 changes: 25 additions & 3 deletions __tests__/devenv-e2e/004-stop.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe( 'vip dev-env stop', () => {
env,
} );
expect( result.rc ).toBeGreaterThan( 0 );
expect( result.stderr ).toContain( "Error: Environment doesn't exist." );
expect( result.stderr ).toContain( 'Error: Environment not found.' );

return expect( checkEnvExists( slug ) ).resolves.toBe( false );
} );
Expand All @@ -73,7 +73,7 @@ describe( 'vip dev-env stop', () => {
true
);
expect( result.rc ).toBe( 0 );
expect( result.stdout ).toContain( 'environment stopped' );
expect( result.stdout ).toContain( `environment "${ slug }" stopped` );

const containersAfterStop = await getContainersForProject( docker, slug );
expect( containersAfterStop ).toHaveLength( 0 );
Expand All @@ -91,11 +91,33 @@ describe( 'vip dev-env stop', () => {
true
);
expect( result.rc ).toBe( 0 );
expect( result.stdout ).toContain( 'environment stopped' );
expect( result.stdout ).toContain( `environment "${ slug }" stopped` );

const containersAfterStop = await getContainersForProject( docker, slug );
expect( containersAfterStop ).toHaveLength( 0 );

await destroyEnvironment( cliTest, slug, env );
} );

it( 'should be able to stop all environments', async () => {
const slug1 = getProjectSlug();
const slug2 = getProjectSlug();
expect( await checkEnvExists( slug1 ) ).toBe( false );
expect( await checkEnvExists( slug2 ) ).toBe( false );

await createAndStartEnvironment( cliTest, slug1, env );
await createAndStartEnvironment( cliTest, slug2, env );

const result = await cliTest.spawn(
[ process.argv[ 0 ], vipDevEnvStop, '--all' ],
{ env },
true
);
expect( result.rc ).toBe( 0 );
expect( result.stdout ).toContain( `environment "${ slug1 }" stopped` );
expect( result.stdout ).toContain( `environment "${ slug2 }" stopped` );

await destroyEnvironment( cliTest, slug1, env );
await destroyEnvironment( cliTest, slug2, env );
} );
} );
65 changes: 52 additions & 13 deletions src/bin/vip-dev-env-stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import command from '../lib/cli/command';
import {
getEnvTrackingInfo,
getEnvironmentName,
handleCLIException,
processSlug,
validateDependencies,
} from '../lib/dev-environment/dev-environment-cli';
import { stopEnvironment } from '../lib/dev-environment/dev-environment-core';
import {
getAllEnvironmentNames,
stopEnvironment,
} from '../lib/dev-environment/dev-environment-core';
import { bootstrapLando } from '../lib/dev-environment/dev-environment-lando';
import { trackEvent } from '../lib/tracker';

Expand All @@ -24,6 +26,10 @@ const examples = [
usage: `${ exampleUsage } --slug=example-site`,
description: 'Stop a local environment named "example-site".',
},
{
usage: `${ exampleUsage } --all`,
description: 'Stops all local environments.',
},
];

command( {
Expand All @@ -35,27 +41,60 @@ command( {
undefined,
processSlug
)
.option( 'all', 'Stop all local environments.' )
.examples( examples )
.argv( process.argv, async ( arg, opt ) => {
const slug = await getEnvironmentName( opt );

const lando = await bootstrapLando();
await validateDependencies( lando, slug );
await validateDependencies( lando, '' );

debug( 'Args: ', arg, 'Options: ', opt );

const trackingInfo = getEnvTrackingInfo( slug );
/** @type {Record< string, unknown >} */
let trackingInfo;
/** @type {string[]} */
let environments;
if ( opt.all ) {
trackingInfo = { all: true };
environments = getAllEnvironmentNames();
} else {
const slug = await getEnvironmentName( opt );
trackingInfo = getEnvTrackingInfo( slug );
environments = [ slug ];
}

await trackEvent( 'dev_env_stop_command_execute', trackingInfo );

try {
await stopEnvironment( lando, slug );
for ( const slug of environments ) {
try {
// eslint-disable-next-line no-await-in-loop
await stopEnvironment( lando, slug );

const message = chalk.green( '✓' ) + ` environment "${ slug }" stopped.\n`;
console.log( message );
} catch ( error ) {
let err;
if ( ! ( error instanceof Error ) ) {
err = new Error( error?.toString() );
} else {
err = error;
}

process.exitCode = 1;
const errorTrackingInfo = {
...trackingInfo,
failure: err.message,
stack: err.stack,
};

const message = chalk.green( '✓' ) + ' environment stopped.\n';
console.log( message );
// trackEvent does not throw
// eslint-disable-next-line no-await-in-loop
await trackEvent( 'dev_env_stop_command_error', errorTrackingInfo );

console.error( chalk.red( 'Error:' ), err.message.replace( 'ERROR: ', '' ) );
}
}

if ( process.exitCode === 0 ) {
await trackEvent( 'dev_env_stop_command_success', trackingInfo );
} catch ( error ) {
await handleCLIException( error, 'dev_env_stop_command_error', trackingInfo );
process.exitCode = 1;
}
} );

0 comments on commit 278fd1c

Please sign in to comment.