Skip to content

Commit d287e9b

Browse files
authored
Merge pull request #78 from RonasIT/generate-env-if-missing
feat: auto-create .env file from .env.example if missing
2 parents c9521d0 + 14c9434 commit d287e9b

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

src/Commands/InitCommand.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ public function handle(): void
103103

104104
$this->appUrl = $this->ask('Please enter an application URL', "https://api.dev.{$kebabName}.com");
105105

106-
$envFile = (file_exists('.env')) ? '.env' : '.env.example';
107-
108106
$envConfig = [
109107
'APP_NAME' => $this->appName,
110108
'DB_CONNECTION' => $this->dbConnection,
@@ -115,7 +113,13 @@ public function handle(): void
115113
'DB_PASSWORD' => '',
116114
];
117115

118-
$this->updateEnvFile($envFile, $envConfig);
116+
$this->updateEnvFile('.env.example', $envConfig);
117+
118+
if (!file_exists('.env')) {
119+
copy('.env.example', '.env');
120+
} else {
121+
$this->updateEnvFile('.env', $envConfig);
122+
}
119123

120124
if (!file_exists('.env.development')) {
121125
copy('.env.example', '.env.development');
@@ -166,12 +170,9 @@ public function handle(): void
166170
$data['CLERK_ALLOWED_ORIGINS'] = '';
167171
}
168172

173+
$this->updateEnvFile('.env', $data);
174+
$this->updateEnvFile('.env.example', $data);
169175
$this->updateEnvFile('.env.development', $data);
170-
$this->updateEnvFile($envFile, $data);
171-
172-
if ($envFile !== '.env.example') {
173-
$this->updateEnvFile('.env.example', $data);
174-
}
175176
}
176177

177178
if ($this->confirm('Do you want to generate an admin user?', true)) {

tests/InitCommandTest.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function testRunWithoutAdminAndReadmeCreationConvertAppNameToPascalCaseTe
2323
$this->callFileExists('.env', false),
2424
$this->callFileExists('.env.development', false),
2525

26+
$this->callCopy('.env.example', '.env'),
2627
$this->callCopy('.env.example', '.env.development'),
2728

2829
$this->callClassExists('Laravel\Telescope\TelescopeServiceProvider'),
@@ -66,6 +67,7 @@ public function testRunWithoutAdminAndReadmeCreation()
6667
{
6768
$this->mockNativeFunction(
6869
'\Winter\LaravelConfigWriter',
70+
$this->changeEnvFileCall('.env.example', 'env.example.yml', 'env.example_app_name_pascal_case.yml'),
6971
$this->changeEnvFileCall('.env', 'env.example.yml', 'env.example_app_name_pascal_case.yml'),
7072
$this->changeEnvFileCall('.env.development', 'env.development.yml', 'env.development_app_name_pascal_case.yml'),
7173
$this->changeConfigFileCall('config/auto-doc.php', 'auto_doc.php', 'auto_doc_after_changes.php'),
@@ -130,6 +132,8 @@ public function testRunWithAdminAndWithoutReadmeCreation()
130132
$this->callFileExists('.env', false),
131133
$this->callFileExists('.env.development'),
132134

135+
$this->callCopy('.env.example', '.env'),
136+
133137
$this->callFileGetContent(base_path('composer.json'), $this->getFixture('composer_with_pint_settings.json')),
134138

135139
$this->callFilePutContent('database/migrations/2018_11_11_111111_add_default_admin.php', $this->getFixture('migration.php')),
@@ -178,8 +182,9 @@ public function testRunWithAdminAndDefaultReadmeCreation()
178182
'\Winter\LaravelConfigWriter',
179183
$this->changeEnvFileCall('.env.example', 'env.example.yml', 'env.example_app_name_not_pascal_case.yml'),
180184
$this->changeEnvFileCall('.env.development', 'env.development.yml', 'env.development_app_name_not_pascal_case.yml'),
181-
$this->changeEnvFileCall('.env.development', 'env.development_app_name_not_pascal_case.yml', 'env.development_clerk_credentials_added.yml'),
185+
$this->changeEnvFileCall('.env', 'env.example_app_name_not_pascal_case.yml', 'env.example_clerk_credentials_added.yml'),
182186
$this->changeEnvFileCall('.env.example', 'env.example_app_name_not_pascal_case.yml', 'env.example_clerk_credentials_added.yml'),
187+
$this->changeEnvFileCall('.env.development', 'env.development_app_name_not_pascal_case.yml', 'env.development_clerk_credentials_added.yml'),
183188
$this->changeConfigFileCall('config/auto-doc.php', 'auto_doc.php', 'auto_doc_after_changes.php'),
184189
$this->changeConfigFileCall('config/telescope.php', 'telescope_config.php', 'telescope_config_after_initialization.php'),
185190
);
@@ -189,6 +194,8 @@ public function testRunWithAdminAndDefaultReadmeCreation()
189194
$this->callFileExists('.env', false),
190195
$this->callFileExists('.env.development'),
191196

197+
$this->callCopy('.env.example', '.env'),
198+
192199
$this->callFileGetContent(base_path('composer.json'), $this->getFixture('composer_with_pint_settings.json')),
193200

194201
$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()
322329
$this->callFileExists('.env', false),
323330
$this->callFileExists('.env.development'),
324331

332+
$this->callCopy('.env.example', '.env'),
333+
325334
$this->callFileGetContent(base_path('composer.json'), $this->getFixture('composer_with_pint_settings.json')),
326335

327336
$this->callFilePutContent(base_path('composer.json'), $this->getFixture('composer_with_pint_settings.json')),
@@ -429,6 +438,8 @@ public function testRunWithAdminAndFullReadmeCreationAndRemovingInitializatorIns
429438
$this->callFileExists('.env', false),
430439
$this->callFileExists('.env.development'),
431440

441+
$this->callCopy('.env.example', '.env'),
442+
432443
$this->callFileGetContent(base_path('composer.json'), $this->getFixture('composer_with_pint_settings.json')),
433444

434445
$this->callFilePutContent('database/migrations/2018_11_11_111111_add_default_admin.php', $this->getFixture('migration.php')),
@@ -554,6 +565,8 @@ public function testRunWithoutAdminAndUsingTelescope()
554565
$this->callFileExists('.env', false),
555566
$this->callFileExists('.env.development'),
556567

568+
$this->callCopy('.env.example', '.env'),
569+
557570
$this->callFileGetContent(base_path('composer.json'), $this->getFixture('composer_with_pint_settings.json')),
558571

559572
$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
662675
'\Winter\LaravelConfigWriter',
663676
$this->changeEnvFileCall('.env.example', 'env.example.yml', 'env.example_app_name_not_pascal_case.yml'),
664677
$this->changeEnvFileCall('.env.development', 'env.development.yml', 'env.development_app_name_not_pascal_case.yml'),
665-
$this->changeEnvFileCall('.env.development', 'env.development_app_name_not_pascal_case.yml', 'env.development_clerk_credentials_added_mobile_app.yml'),
678+
$this->changeEnvFileCall('.env', 'env.example_app_name_not_pascal_case.yml', 'env.example_clerk_credentials_added_mobile_app.yml'),
666679
$this->changeEnvFileCall('.env.example', 'env.example_app_name_not_pascal_case.yml', 'env.example_clerk_credentials_added_mobile_app.yml'),
680+
$this->changeEnvFileCall('.env.development', 'env.development_app_name_not_pascal_case.yml', 'env.development_clerk_credentials_added_mobile_app.yml'),
667681
$this->changeConfigFileCall('config/auto-doc.php', 'auto_doc.php', 'auto_doc_after_changes.php'),
668682
$this->changeConfigFileCall('config/telescope.php', 'telescope_config.php', 'telescope_config_after_initialization.php'),
669683
);
@@ -673,6 +687,8 @@ public function testRunWithClerkMobileAppWithPintInstalled(): void
673687
$this->callFileExists('.env', false),
674688
$this->callFileExists('.env.development'),
675689

690+
$this->callCopy('.env.example', '.env'),
691+
676692
$this->callFileGetContent(base_path('composer.json'), $this->getFixture('composer_with_pint_settings.json')),
677693

678694
$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
793809
'\Winter\LaravelConfigWriter',
794810
$this->changeEnvFileCall('.env.example', 'env.example.yml', 'env.example_app_name_not_pascal_case.yml'),
795811
$this->changeEnvFileCall('.env.development', 'env.development.yml', 'env.development_app_name_not_pascal_case.yml'),
796-
$this->changeEnvFileCall('.env.development', 'env.development_app_name_not_pascal_case.yml', 'env.development_clerk_credentials_added.yml'),
812+
$this->changeEnvFileCall('.env', 'env.example.yml', 'env.example_clerk_credentials_added.yml'),
797813
$this->changeEnvFileCall('.env.example', 'env.example.yml', 'env.example_clerk_credentials_added.yml'),
814+
$this->changeEnvFileCall('.env.development', 'env.development_app_name_not_pascal_case.yml', 'env.development_clerk_credentials_added.yml'),
798815
$this->changeConfigFileCall('config/auto-doc.php', 'auto_doc.php', 'auto_doc_after_changes.php'),
799816
$this->changeConfigFileCall('config/telescope.php', 'telescope_config.php', 'telescope_config_after_initialization.php'),
800817
);
@@ -804,7 +821,9 @@ public function testRunWithClerkAdditionalAdminsWithoutDefaultAdmin(): void
804821
$this->callFileExists('.env', false),
805822
$this->callFileExists('.env.development'),
806823

807-
$this->callFileGetContent(base_path('composer.json'), $this->getFixture('composer_with_pint_settings.json')),
824+
$this->callCopy('.env.example', '.env'),
825+
826+
$this->callFileGetContent(base_path('composer.json'), $this->getFixture('composer_with_pint_settings.json')),
808827

809828
$this->callFilePutContent('database/migrations/2018_11_11_111111_users_add_clerk_id_field.php', $this->getFixture('users_add_clerk_id_field_migration.php')),
810829
$this->callFilePutContent('app/Support/Clerk/ClerkUserRepository.php', $this->getFixture('clerk_user_repository.php')),

0 commit comments

Comments
 (0)