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

wp-env: Remove platform-specific destroy commands #30638

Merged
merged 2 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions packages/env/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Bug Fix

- `wp-env destroy` will now work in environments which don't include the `grep` or `awk` commands, such as Windows PowerShell.

## 4.0.0 (2021-03-17)

### Breaking Change
Expand Down
43 changes: 23 additions & 20 deletions packages/env/lib/commands/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,31 +68,34 @@ module.exports = async function destroy( { spinner, debug } ) {

const directoryHash = path.basename( workDirectoryPath );

spinner.text = 'Removing docker networks and volumes.';
const getVolumes = `docker volume ls | grep "${ directoryHash }" | awk '/ / { print $2 }'`;
const removeVolumes = `docker volume rm $(${ getVolumes })`;
spinner.text = 'Removing docker volumes.';
await removeDockerItems( 'volume', directoryHash );

const getNetworks = `docker network ls | grep "${ directoryHash }" | awk '/ / { print $1 }'`;
const removeNetworks = `docker network rm $(${ getNetworks })`;

const command = `${ removeVolumes } && ${ removeNetworks }`;

if ( debug ) {
spinner.info(
`Running command to remove volumes and networks:\n${ command }\n`
);
}

const { stdout } = await exec( command );
if ( debug && stdout ) {
// Disable reason: Logging information in debug mode.
// eslint-disable-next-line no-console
console.log( `Removed volumes and networks:\n${ stdout }` );
}
spinner.text = 'Removing docker networks.';
await removeDockerItems( 'network', directoryHash );

spinner.text = 'Removing local files.';

await rimraf( workDirectoryPath );

spinner.text = 'Removed WordPress environment.';
};

/**
* Removes docker items, like networks or volumes, matching the given name.
*
* @param {string} itemType The item type, like "network" or "volume"
* @param {string} name Remove items whose name match this string.
*/
async function removeDockerItems( itemType, name ) {
const { stdout: items } = await exec(
`docker ${ itemType } ls -q --filter name=${ name }`
);
if ( items ) {
await exec(
`docker ${ itemType } rm ${ items
.split( '\n' ) // TODO: use os.EOL?
.join( ' ' ) }`
);
}
}