From 0580ae0bf74ac179f73e9a585e4dd81edcda3b59 Mon Sep 17 00:00:00 2001 From: Timothy Jacobs Date: Tue, 17 Sep 2024 13:45:17 -0700 Subject: [PATCH] Allow easier customization of the .env file The .env file allows for configuring how the WordPress Local environment should be configured. However, because the file is version controlled, developers must be careful not to commit their modifications. This commit renames the .env file to be .env.example. During env start, the .env.example file is copied to .env if it does not exist. This allows for contributors to continue using the project without thinking about .env and to make changes when needed. This brings WordPress Core into the dotenv project guidelines. Props johnbillion, afragen, h71. Fixes #52668. --- .env => .env.example | 0 .gitignore | 1 + tools/local-env/scripts/start.js | 14 ++++++++++++++ 3 files changed, 15 insertions(+) rename .env => .env.example (100%) diff --git a/.env b/.env.example similarity index 100% rename from .env rename to .env.example diff --git a/.gitignore b/.gitignore index 44c3769ee314d..f35d743ea866f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # gitignore file for WordPress Core # Configuration files with possibly sensitive information +.env wp-config.php wp-tests-config.php .htaccess diff --git a/tools/local-env/scripts/start.js b/tools/local-env/scripts/start.js index b929dce0b69a8..22bb65ca8426d 100644 --- a/tools/local-env/scripts/start.js +++ b/tools/local-env/scripts/start.js @@ -2,6 +2,20 @@ const dotenv = require( 'dotenv' ); const dotenvExpand = require( 'dotenv-expand' ); const { execSync } = require( 'child_process' ); +try { + execSync( 'test -f .env', { stdio: 'inherit' } ); +} catch ( e ) { + // test exits with a status code of 1 if the test fails. + // Alert the user on any other failure. + if ( e.status !== 1 ) { + throw e; + } + + // The file does not exist, copy over the default example file. + execSync( 'cp .env.example .env', { stdio: 'inherit' } ); +} + + dotenvExpand.expand( dotenv.config() ); // Check if the Docker service is running.