Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vip dev-env purge command logic #1537

Merged
merged 9 commits into from
Nov 22, 2023
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"vip-dev-env-start": "dist/bin/vip-dev-env-start.js",
"vip-dev-env-stop": "dist/bin/vip-dev-env-stop.js",
"vip-dev-env-logs": "dist/bin/vip-dev-env-logs.js",
"vip-dev-env-purge": "dist/bin/vip-dev-env-purge.js",
"vip-export": "dist/bin/vip-export.js",
"vip-export-sql": "dist/bin/vip-export-sql.js",
"vip-dev-env-sync": "dist/bin/vip-dev-env-sync.js",
Expand Down
91 changes: 91 additions & 0 deletions src/bin/vip-dev-env-purge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env node
rebeccahum marked this conversation as resolved.
Show resolved Hide resolved

/**
* External dependencies
*/
import chalk from 'chalk';
import debugLib from 'debug';

/**
* Internal dependencies
*/
import command from '../lib/cli/command';
import { DEV_ENVIRONMENT_FULL_COMMAND } from '../lib/constants/dev-environment';
import {
getEnvTrackingInfo,
handleCLIException,
validateDependencies,
promptForBoolean,
} from '../lib/dev-environment/dev-environment-cli';
import {
destroyEnvironment,
getAllEnvironmentNames,
} from '../lib/dev-environment/dev-environment-core';
import { bootstrapLando } from '../lib/dev-environment/dev-environment-lando';
import { trackEvent } from '../lib/tracker';

const debug = debugLib( '@automattic/vip:bin:dev-environment' );

const examples = [
{
usage: `${ DEV_ENVIRONMENT_FULL_COMMAND } purge`,
description: 'Destroys all local dev environments',
},
{
usage: `${ DEV_ENVIRONMENT_FULL_COMMAND } purge --force`,
description: 'Destroys all local dev environments without prompting',
},
];

command()
.option( 'soft', 'Keep config files needed to start an environment intact' )
.option( 'force', 'Removes prompt that verifies if user wants to destroy all environments' )
.examples( examples )
.argv( process.argv, async ( arg, opt ) => {
debug( 'Args: ', arg, 'Options: ', opt );

const allEnvNames = getAllEnvironmentNames();
const lando = await bootstrapLando();

if ( allEnvNames.length === 0 ) {
console.log( 'No environments to purge!' );
return;
}

if ( ! opt.force ) {
const purge = await promptForBoolean(
'Are you sure you want to purge ALL existing dev environments?',
true
);

if ( ! purge ) {
return;
}
}

const trackingInfo = { all: true };
await trackEvent( 'dev_env_purge_command_execute', trackingInfo );
await validateDependencies( lando, '' );
const removeFiles = ! ( opt.soft || false );

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

const message = chalk.green( '✓' ) + ' Environment destroyed.\n';
console.log( message );
} catch ( error ) {
const trackingInfoChild = getEnvTrackingInfo( slug );
// eslint-disable-next-line no-await-in-loop
await handleCLIException( error, 'dev_env_purge_command_error', trackingInfoChild );
process.exitCode = 1;
}
}
await trackEvent( 'dev_env_purge_command_success', trackingInfo );
} catch ( error ) {
await handleCLIException( error, 'dev_env_purge_command_error', trackingInfo );
process.exitCode = 1;
}
} );
1 change: 1 addition & 0 deletions src/bin/vip-dev-env.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ command( {
.command( 'shell', 'Spawns a shell in a dev environment' )
.command( 'logs', 'View logs from a local WordPress environment' )
.command( 'sync', 'Pull data from production to local development environment' )
.command( 'purge', 'Destroy all existing environments' )
.argv( process.argv );
Loading