Skip to content

Commit 0d12da0

Browse files
committed
Build/Test Tools: Improve env:install command with better configurability and error handling.
* Force WP-CLI to use the `wp-config.php` in the directory above the `src` directory via the `WP_CONFIG_PATH` environment variable [read by WP-CLI](https://github.com/wp-cli/wp-cli/blob/2800ad0a66747a826ae4221b2f022f1df6779cb6/php/utils.php#L328-L329) in the `wp_locate_config()` function. * Update the `env:install` command to write out the config at the repo root instead of writing it inside of the `ABSPATH` only then to move it one directory up. * Fix JSHint issues. * Add error handling to when `npm run env:install` is executed without having first done `npm run env:start`, in which case the script will end with an exit code of 1 and emit: > Error: It appears the development environment has not been started. Message: Timed out waiting for: tcp:localhost:8000 > Did you forget to do 'npm run env:start'? Reviewed by audrasjb. Merges [60305] to the 6.8 branch. Fixes #63543. Props westonruter, jorbin, SirLouen. git-svn-id: https://develop.svn.wordpress.org/branches/6.8@60431 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 49a6250 commit 0d12da0

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ services:
106106
PHP_FPM_UID: ${PHP_FPM_UID-1000}
107107
PHP_FPM_GID: ${PHP_FPM_GID-1000}
108108
HOST_PATH: ${PWD-}/${LOCAL_DIR-src}
109+
WP_CONFIG_PATH: /var/www/wp-config.php
109110

110111
volumes:
111112
- ./:/var/www

tools/local-env/scripts/install.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
/* jshint node:true */
2+
13
const dotenv = require( 'dotenv' );
24
const dotenvExpand = require( 'dotenv-expand' );
35
const wait_on = require( 'wait-on' );
46
const { execSync } = require( 'child_process' );
5-
const { renameSync, readFileSync, writeFileSync } = require( 'fs' );
7+
const { readFileSync, writeFileSync } = require( 'fs' );
68
const { utils } = require( './utils.js' );
79
const local_env_utils = require( './utils' );
810

@@ -12,7 +14,7 @@ dotenvExpand.expand( dotenv.config() );
1214
local_env_utils.determine_auth_option();
1315

1416
// Create wp-config.php.
15-
wp_cli( 'config create --dbname=wordpress_develop --dbuser=root --dbpass=password --dbhost=mysql --force' );
17+
wp_cli( `config create --dbname=wordpress_develop --dbuser=root --dbpass=password --dbhost=mysql --force --config-file=${process.env.LOCAL_DIR}/../wp-config.php` );
1618

1719
// Add the debug settings to wp-config.php.
1820
// Windows requires this to be done as an additional step, rather than using the --extra-php option in the previous step.
@@ -23,26 +25,35 @@ wp_cli( `config set SCRIPT_DEBUG ${process.env.LOCAL_SCRIPT_DEBUG} --raw --type=
2325
wp_cli( `config set WP_ENVIRONMENT_TYPE ${process.env.LOCAL_WP_ENVIRONMENT_TYPE} --type=constant` );
2426
wp_cli( `config set WP_DEVELOPMENT_MODE ${process.env.LOCAL_WP_DEVELOPMENT_MODE} --type=constant` );
2527

26-
// Move wp-config.php to the base directory, so it doesn't get mixed up in the src or build directories.
27-
renameSync( `${process.env.LOCAL_DIR}/wp-config.php`, 'wp-config.php' );
28-
2928
// Read in wp-tests-config-sample.php, edit it to work with our config, then write it to wp-tests-config.php.
3029
const testConfig = readFileSync( 'wp-tests-config-sample.php', 'utf8' )
3130
.replace( 'youremptytestdbnamehere', 'wordpress_develop_tests' )
3231
.replace( 'yourusernamehere', 'root' )
3332
.replace( 'yourpasswordhere', 'password' )
3433
.replace( 'localhost', 'mysql' )
35-
.replace( "'WP_TESTS_DOMAIN', 'example.org'", `'WP_TESTS_DOMAIN', '${process.env.LOCAL_WP_TESTS_DOMAIN}'` )
36-
.concat( "\ndefine( 'FS_METHOD', 'direct' );\n" );
34+
.replace( `'WP_TESTS_DOMAIN', 'example.org'`, `'WP_TESTS_DOMAIN', '${process.env.LOCAL_WP_TESTS_DOMAIN}'` )
35+
.concat( `\ndefine( 'FS_METHOD', 'direct' );\n` );
3736

3837
writeFileSync( 'wp-tests-config.php', testConfig );
3938

4039
// Once the site is available, install WordPress!
41-
wait_on( { resources: [ `tcp:localhost:${process.env.LOCAL_PORT}`] } )
40+
wait_on( {
41+
resources: [ `tcp:localhost:${process.env.LOCAL_PORT}`],
42+
timeout: 3000,
43+
} )
44+
.catch( err => {
45+
console.error( `Error: It appears the development environment has not been started. Message: ${ err.message }` );
46+
console.error( `Did you forget to do 'npm run env:start'?` );
47+
process.exit( 1 );
48+
} )
4249
.then( () => {
4350
wp_cli( 'db reset --yes' );
4451
const installCommand = process.env.LOCAL_MULTISITE === 'true' ? 'multisite-install' : 'install';
4552
wp_cli( `core ${ installCommand } --title="WordPress Develop" --admin_user=admin --admin_password=password [email protected] --skip-email --url=http://localhost:${process.env.LOCAL_PORT}` );
53+
} )
54+
.catch( err => {
55+
console.error( `Error: Unable to reset DB and install WordPress. Message: ${ err.message }` );
56+
process.exit( 1 );
4657
} );
4758

4859
/**

0 commit comments

Comments
 (0)