Skip to content

Commit

Permalink
Env: Support wp-config.php overrides. (#20352)
Browse files Browse the repository at this point in the history
* Env: Support wp-config.php overrides.

* Env: Move WORDPRESS_DEBUG to work through the new config.

* Fix bugs.

* Handle string values.
  • Loading branch information
epiqueras authored and jorgefilipecosta committed Mar 2, 2020
1 parent 804536f commit ead3acf
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
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 @@ -421,6 +421,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

0 comments on commit ead3acf

Please sign in to comment.