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

Env: Support wp-config.php overrides. #20352

Merged
merged 4 commits into from
Feb 21, 2020
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
1 change: 1 addition & 0 deletions packages/env/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### New Feature

- The `.wp-env.json` coniguration file now accepts a `config` object for setting `wp-config.php` values.
- A `.wp-env.override.json` configuration file can now be used to override fields from `.wp-env.json`.
- You may now override the directory in which `wp-env` creates generated files with the `WP_ENV_HOME` environment variable. The default directory is `~/.wp-env/` (or `~/wp-env/` on Linux).
- The `.wp-env.json` coniguration file now accepts `port` and `testsPort` options which can be used to set the ports on which the docker instance is mounted.
Expand Down
15 changes: 8 additions & 7 deletions packages/env/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,14 @@ You can customize the WordPress installation, plugins and themes that the develo

`.wp-env.json` supports five fields:

| Field | Type | Default | Description |
| ------------- | ------------- | -------- | ------------------------------------------------------------------------------------------------------------------------- |
| `"core"` | `string|null` | `null` | The WordPress installation to use. If `null` is specified, `wp-env` will use the latest production release of WordPress. |
| `"plugins"` | `string[]` | `[]` | A list of plugins to install and activate in the environment. |
| `"themes"` | `string[]` | `[]` | A list of themes to install in the environment. The first theme in the list will be activated. |
| `"port"` | `string` | `"8888"` | The primary port number to use for the insallation. You'll access the instance through the port: 'http://localhost:8888'. |
| `"testsPort"` | `string` | `"8889"` | The port number to use for the tests instance. |
| Field | Type | Default | Description |
| ------------- | ------------- | ------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------- |
| `"core"` | `string|null` | `null` | The WordPress installation to use. If `null` is specified, `wp-env` will use the latest production release of WordPress. |
| `"plugins"` | `string[]` | `[]` | A list of plugins to install and activate in the environment. |
| `"themes"` | `string[]` | `[]` | A list of themes to install in the environment. The first theme in the list will be activated. |
| `"port"` | `string` | `"8888"` | The primary port number to use for the insallation. You'll access the instance through the port: 'http://localhost:8888'. |
| `"testsPort"` | `string` | `"8889"` | The port number to use for the tests instance. |
| `"config"` | `Object` | `"{ WP_DEBUG: true, SCRIPT_DEBUG: true }"` | Mapping of wp-config.php constants to their desired values. |

_Note: the port number environment variables (`WP_ENV_PORT` and `WP_ENV_TESTS_PORT`) take precedent over the .wp-env.json values._

Expand Down
2 changes: 0 additions & 2 deletions packages/env/lib/build-docker-compose-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ module.exports = function buildDockerComposeConfig( config ) {
image: 'wordpress',
ports: [ developmentPorts ],
environment: {
WORDPRESS_DEBUG: '1',
WORDPRESS_DB_NAME: 'wordpress',
},
volumes: developmentMounts,
Expand All @@ -79,7 +78,6 @@ module.exports = function buildDockerComposeConfig( config ) {
image: 'wordpress',
ports: [ testsPorts ],
environment: {
WORDPRESS_DEBUG: '1',
WORDPRESS_DB_NAME: 'tests-wordpress',
},
volumes: testsMounts,
Expand Down
9 changes: 9 additions & 0 deletions packages/env/lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const HOME_PATH_PREFIX = `~${ path.sep }`;
* @property {Source[]} themeSources Themes to load in the environment.
* @property {number} port The port on which to start the development WordPress environment.
* @property {number} testsPort The port on which to start the testing WordPress environment.
* @property {Object} config Mapping of wp-config.php constants to their desired values.
* @property {boolean} debug True if debug mode is enabled.
*/

Expand Down Expand Up @@ -126,6 +127,7 @@ module.exports = {
themes: [],
port: 8888,
testsPort: 8889,
config: { WP_DEBUG: true, SCRIPT_DEBUG: true },
},
config,
overrideConfig
Expand Down Expand Up @@ -177,6 +179,12 @@ module.exports = {
);
}

if ( typeof config.config !== 'object' ) {
throw new ValidationError(
'Invalid .wp-env.json: "config" must be an object.'
);
}

const workDirectoryPath = path.resolve(
getHomeDirectory(),
md5( configPath )
Expand Down Expand Up @@ -207,6 +215,7 @@ module.exports = {
workDirectoryPath,
} )
),
config: config.config,
};
},
};
Expand Down
13 changes: 13 additions & 0 deletions packages/env/lib/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,19 @@ async function configureWordPress( environment, config ) {
options
);

// Set wp-config.php values.
for ( const [ key, value ] of Object.entries( config.config ) ) {
const command = [ 'wp', 'config', 'set', key, value ];
if ( typeof value !== 'string' ) {
command.push( '--raw' );
}
await dockerCompose.run(
environment === 'development' ? 'cli' : 'tests-cli',
command,
options
);
}

// Activate all plugins.
for ( const pluginSource of config.pluginSources ) {
await dockerCompose.run(
Expand Down