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 --env-cwd Option To wp-env run #49908

Merged
merged 7 commits into from
Apr 20, 2023
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

### Breaking Changes

- `run` command now has a `--env-cwd` option to set the working directory in the container for the command to execute from.

## 5.16.0 (2023-04-12)

## 5.15.0 (2023-03-29)
Expand Down
5 changes: 4 additions & 1 deletion packages/env/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ The run command can be used to open shell sessions or invoke WP-CLI commands.
<div class="callout callout-alert">
In some cases, `wp-env` may consume options that you are attempting to pass to
the container. This happens with options that `wp-env` has already declared,
such as `--debug`, `--help`, and `--version`. When this happens, you should fall
such as `--env-cwd`, `--debug`, `--help`, and `--version`. When this happens, you should fall
back to using quotation marks; `wp-env` considers everything inside the
quotation marks to be command argument.

Expand Down Expand Up @@ -349,6 +349,9 @@ Options:
--help Show help [boolean]
--version Show version number [boolean]
--debug Enable debug output. [boolean] [default: false]
--env-cwd The command's working directory inside of the container. Paths
without a leading slash are relative to the WordPress root.
[string] [default: "."]
```

For example:
Expand Down
7 changes: 7 additions & 0 deletions packages/env/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ module.exports = function cli() {
'run <container> [command..]',
'Runs an arbitrary command in one of the underlying Docker containers. The "container" param should reference one of the underlying Docker services like "development", "tests", or "cli". To run a wp-cli command, use the "cli" or "tests-cli" service. You can also use this command to open shell sessions like bash and the WordPress shell in the WordPress instance. For example, `wp-env run cli bash` will open bash in the development WordPress instance. When using long commands with arguments and quotation marks, you need to wrap the "command" param in quotation marks. For example: `wp-env run tests-cli "wp post create --post_type=page --post_title=\'Test\'"` will create a post on the tests WordPress instance.',
( args ) => {
args.option( 'env-cwd', {
type: 'string',
requiresArg: true,
default: '.',
describe:
"The command's working directory inside of the container. Paths without a leading slash are relative to the WordPress root.",
} );
args.positional( 'container', {
type: 'string',
describe: 'The container to run the command on.',
Expand Down
34 changes: 21 additions & 13 deletions packages/env/lib/commands/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
const { spawn } = require( 'child_process' );
const path = require( 'path' );

/**
* Internal dependencies
Expand All @@ -18,41 +19,48 @@ const initConfig = require( '../init-config' );
* @param {Object} options
* @param {string} options.container The Docker container to run the command on.
* @param {string[]} options.command The command to run.
* @param {string} options.envCwd The working directory for the command to be executed from.
* @param {Object} options.spinner A CLI spinner which indicates progress.
* @param {boolean} options.debug True if debug mode is enabled.
*/
module.exports = async function run( { container, command, spinner, debug } ) {
module.exports = async function run( {
container,
command,
envCwd,
spinner,
debug,
} ) {
const config = await initConfig( { spinner, debug } );

command = command.join( ' ' );

// Shows a contextual tip for the given command.
showCommandTips( command, container, spinner );

await spawnCommandDirectly( {
container,
command,
spinner,
config,
} );
await spawnCommandDirectly( config, container, command, envCwd, spinner );

spinner.text = `Ran \`${ command }\` in '${ container }'.`;
};

/**
* Runs an arbitrary command on the given Docker container.
*
* @param {Object} options
* @param {string} options.container The Docker container to run the command on.
* @param {string} options.command The command to run.
* @param {WPConfig} options.config The wp-env configuration.
* @param {Object} options.spinner A CLI spinner which indicates progress.
* @param {WPConfig} config The wp-env configuration.
* @param {string} container The Docker container to run the command on.
* @param {string} command The command to run.
* @param {string} envCwd The working directory for the command to be executed from.
* @param {Object} spinner A CLI spinner which indicates progress.
*/
function spawnCommandDirectly( { container, command, config, spinner } ) {
function spawnCommandDirectly( config, container, command, envCwd, spinner ) {
// We need to pass absolute paths to the container.
envCwd = path.resolve( '/var/www/html', envCwd );
noahtallen marked this conversation as resolved.
Show resolved Hide resolved

const composeCommand = [
'-f',
config.dockerComposeConfigPath,
'run',
'-w',
envCwd,
'--rm',
container,
...command.split( ' ' ), // The command will fail if passed as a complete string.
Expand Down