Skip to content

Commit

Permalink
wp-env: Remove platform-specific destroy commands (#30638)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahtallen authored Apr 8, 2021
1 parent e294d9c commit 616dc9a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
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( ' ' ) }`
);
}
}

0 comments on commit 616dc9a

Please sign in to comment.