Skip to content

Commit 6ac0efd

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'? Fixes #63543. Props westonruter, jorbin, SirLouen. git-svn-id: https://develop.svn.wordpress.org/trunk@60305 602fd350-edb4-49c9-b593-d223f7449a82
1 parent b29a5c2 commit 6ac0efd

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 local_env_utils = require( './utils' );
79

810
dotenvExpand.expand( dotenv.config() );
@@ -11,7 +13,7 @@ dotenvExpand.expand( dotenv.config() );
1113
local_env_utils.determine_auth_option();
1214

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

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

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

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

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

4758
/**

0 commit comments

Comments
 (0)