From 529bc00f05856eebfd0cd112eeb5b2fa4d8a3836 Mon Sep 17 00:00:00 2001 From: Artyom Osepyan Date: Wed, 5 Nov 2025 16:14:09 +0300 Subject: [PATCH] feat: auto-create .env file from .env.example if missing Refs: https://github.com/RonasIT/laravel-project-initializator/issues/77 --- src/Commands/InitCommand.php | 17 +++++++++-------- tests/InitCommandTest.php | 27 +++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/Commands/InitCommand.php b/src/Commands/InitCommand.php index 3bb02f1..dbe74ce 100644 --- a/src/Commands/InitCommand.php +++ b/src/Commands/InitCommand.php @@ -103,8 +103,6 @@ public function handle(): void $this->appUrl = $this->ask('Please enter an application URL', "https://api.dev.{$kebabName}.com"); - $envFile = (file_exists('.env')) ? '.env' : '.env.example'; - $envConfig = [ 'APP_NAME' => $this->appName, 'DB_CONNECTION' => $this->dbConnection, @@ -115,7 +113,13 @@ public function handle(): void 'DB_PASSWORD' => '', ]; - $this->updateEnvFile($envFile, $envConfig); + $this->updateEnvFile('.env.example', $envConfig); + + if (!file_exists('.env')) { + copy('.env.example', '.env'); + } else { + $this->updateEnvFile('.env', $envConfig); + } if (!file_exists('.env.development')) { copy('.env.example', '.env.development'); @@ -166,12 +170,9 @@ public function handle(): void $data['CLERK_ALLOWED_ORIGINS'] = ''; } + $this->updateEnvFile('.env', $data); + $this->updateEnvFile('.env.example', $data); $this->updateEnvFile('.env.development', $data); - $this->updateEnvFile($envFile, $data); - - if ($envFile !== '.env.example') { - $this->updateEnvFile('.env.example', $data); - } } if ($this->confirm('Do you want to generate an admin user?', true)) { diff --git a/tests/InitCommandTest.php b/tests/InitCommandTest.php index f3e3402..48f223e 100644 --- a/tests/InitCommandTest.php +++ b/tests/InitCommandTest.php @@ -23,6 +23,7 @@ public function testRunWithoutAdminAndReadmeCreationConvertAppNameToPascalCaseTe $this->callFileExists('.env', false), $this->callFileExists('.env.development', false), + $this->callCopy('.env.example', '.env'), $this->callCopy('.env.example', '.env.development'), $this->callClassExists('Laravel\Telescope\TelescopeServiceProvider'), @@ -66,6 +67,7 @@ public function testRunWithoutAdminAndReadmeCreation() { $this->mockNativeFunction( '\Winter\LaravelConfigWriter', + $this->changeEnvFileCall('.env.example', 'env.example.yml', 'env.example_app_name_pascal_case.yml'), $this->changeEnvFileCall('.env', 'env.example.yml', 'env.example_app_name_pascal_case.yml'), $this->changeEnvFileCall('.env.development', 'env.development.yml', 'env.development_app_name_pascal_case.yml'), $this->changeConfigFileCall('config/auto-doc.php', 'auto_doc.php', 'auto_doc_after_changes.php'), @@ -130,6 +132,8 @@ public function testRunWithAdminAndWithoutReadmeCreation() $this->callFileExists('.env', false), $this->callFileExists('.env.development'), + $this->callCopy('.env.example', '.env'), + $this->callFileGetContent(base_path('composer.json'), $this->getFixture('composer_with_pint_settings.json')), $this->callFilePutContent('database/migrations/2018_11_11_111111_add_default_admin.php', $this->getFixture('migration.php')), @@ -178,8 +182,9 @@ public function testRunWithAdminAndDefaultReadmeCreation() '\Winter\LaravelConfigWriter', $this->changeEnvFileCall('.env.example', 'env.example.yml', 'env.example_app_name_not_pascal_case.yml'), $this->changeEnvFileCall('.env.development', 'env.development.yml', 'env.development_app_name_not_pascal_case.yml'), - $this->changeEnvFileCall('.env.development', 'env.development_app_name_not_pascal_case.yml', 'env.development_clerk_credentials_added.yml'), + $this->changeEnvFileCall('.env', 'env.example_app_name_not_pascal_case.yml', 'env.example_clerk_credentials_added.yml'), $this->changeEnvFileCall('.env.example', 'env.example_app_name_not_pascal_case.yml', 'env.example_clerk_credentials_added.yml'), + $this->changeEnvFileCall('.env.development', 'env.development_app_name_not_pascal_case.yml', 'env.development_clerk_credentials_added.yml'), $this->changeConfigFileCall('config/auto-doc.php', 'auto_doc.php', 'auto_doc_after_changes.php'), $this->changeConfigFileCall('config/telescope.php', 'telescope_config.php', 'telescope_config_after_initialization.php'), ); @@ -189,6 +194,8 @@ public function testRunWithAdminAndDefaultReadmeCreation() $this->callFileExists('.env', false), $this->callFileExists('.env.development'), + $this->callCopy('.env.example', '.env'), + $this->callFileGetContent(base_path('composer.json'), $this->getFixture('composer_with_pint_settings.json')), $this->callFilePutContent('database/migrations/2018_11_11_111111_users_add_clerk_id_field.php', $this->getFixture('users_add_clerk_id_field_migration.php')), @@ -322,6 +329,8 @@ public function testRunWithAdminAndPartialReadmeCreation() $this->callFileExists('.env', false), $this->callFileExists('.env.development'), + $this->callCopy('.env.example', '.env'), + $this->callFileGetContent(base_path('composer.json'), $this->getFixture('composer_with_pint_settings.json')), $this->callFilePutContent(base_path('composer.json'), $this->getFixture('composer_with_pint_settings.json')), @@ -429,6 +438,8 @@ public function testRunWithAdminAndFullReadmeCreationAndRemovingInitializatorIns $this->callFileExists('.env', false), $this->callFileExists('.env.development'), + $this->callCopy('.env.example', '.env'), + $this->callFileGetContent(base_path('composer.json'), $this->getFixture('composer_with_pint_settings.json')), $this->callFilePutContent('database/migrations/2018_11_11_111111_add_default_admin.php', $this->getFixture('migration.php')), @@ -554,6 +565,8 @@ public function testRunWithoutAdminAndUsingTelescope() $this->callFileExists('.env', false), $this->callFileExists('.env.development'), + $this->callCopy('.env.example', '.env'), + $this->callFileGetContent(base_path('composer.json'), $this->getFixture('composer_with_pint_settings.json')), $this->callFilePutContent('database/migrations/2018_11_11_111111_add_telescope_admin.php', $this->getFixture('telescope_users_table_migration.php')), @@ -662,8 +675,9 @@ public function testRunWithClerkMobileAppWithPintInstalled(): void '\Winter\LaravelConfigWriter', $this->changeEnvFileCall('.env.example', 'env.example.yml', 'env.example_app_name_not_pascal_case.yml'), $this->changeEnvFileCall('.env.development', 'env.development.yml', 'env.development_app_name_not_pascal_case.yml'), - $this->changeEnvFileCall('.env.development', 'env.development_app_name_not_pascal_case.yml', 'env.development_clerk_credentials_added_mobile_app.yml'), + $this->changeEnvFileCall('.env', 'env.example_app_name_not_pascal_case.yml', 'env.example_clerk_credentials_added_mobile_app.yml'), $this->changeEnvFileCall('.env.example', 'env.example_app_name_not_pascal_case.yml', 'env.example_clerk_credentials_added_mobile_app.yml'), + $this->changeEnvFileCall('.env.development', 'env.development_app_name_not_pascal_case.yml', 'env.development_clerk_credentials_added_mobile_app.yml'), $this->changeConfigFileCall('config/auto-doc.php', 'auto_doc.php', 'auto_doc_after_changes.php'), $this->changeConfigFileCall('config/telescope.php', 'telescope_config.php', 'telescope_config_after_initialization.php'), ); @@ -673,6 +687,8 @@ public function testRunWithClerkMobileAppWithPintInstalled(): void $this->callFileExists('.env', false), $this->callFileExists('.env.development'), + $this->callCopy('.env.example', '.env'), + $this->callFileGetContent(base_path('composer.json'), $this->getFixture('composer_with_pint_settings.json')), $this->callFilePutContent('database/migrations/2018_11_11_111111_users_add_clerk_id_field.php', $this->getFixture('users_add_clerk_id_field_migration.php')), @@ -793,8 +809,9 @@ public function testRunWithClerkAdditionalAdminsWithoutDefaultAdmin(): void '\Winter\LaravelConfigWriter', $this->changeEnvFileCall('.env.example', 'env.example.yml', 'env.example_app_name_not_pascal_case.yml'), $this->changeEnvFileCall('.env.development', 'env.development.yml', 'env.development_app_name_not_pascal_case.yml'), - $this->changeEnvFileCall('.env.development', 'env.development_app_name_not_pascal_case.yml', 'env.development_clerk_credentials_added.yml'), + $this->changeEnvFileCall('.env', 'env.example.yml', 'env.example_clerk_credentials_added.yml'), $this->changeEnvFileCall('.env.example', 'env.example.yml', 'env.example_clerk_credentials_added.yml'), + $this->changeEnvFileCall('.env.development', 'env.development_app_name_not_pascal_case.yml', 'env.development_clerk_credentials_added.yml'), $this->changeConfigFileCall('config/auto-doc.php', 'auto_doc.php', 'auto_doc_after_changes.php'), $this->changeConfigFileCall('config/telescope.php', 'telescope_config.php', 'telescope_config_after_initialization.php'), ); @@ -804,7 +821,9 @@ public function testRunWithClerkAdditionalAdminsWithoutDefaultAdmin(): void $this->callFileExists('.env', false), $this->callFileExists('.env.development'), - $this->callFileGetContent(base_path('composer.json'), $this->getFixture('composer_with_pint_settings.json')), + $this->callCopy('.env.example', '.env'), + + $this->callFileGetContent(base_path('composer.json'), $this->getFixture('composer_with_pint_settings.json')), $this->callFilePutContent('database/migrations/2018_11_11_111111_users_add_clerk_id_field.php', $this->getFixture('users_add_clerk_id_field_migration.php')), $this->callFilePutContent('app/Support/Clerk/ClerkUserRepository.php', $this->getFixture('clerk_user_repository.php')),