From 9b85a5dffb428bc46e7d8a2532de9a12ac1cc7a0 Mon Sep 17 00:00:00 2001 From: Artyom Osepyan Date: Fri, 17 Oct 2025 10:28:46 +0300 Subject: [PATCH 1/8] refactor: second stage - InitCommand cleanup Refs: https://github.com/RonasIT/laravel-project-initializator/issues/67 --- src/Commands/InitCommand.php | 528 ++++++++++++++++++----------------- 1 file changed, 270 insertions(+), 258 deletions(-) diff --git a/src/Commands/InitCommand.php b/src/Commands/InitCommand.php index 3c5ab1e..56f20c0 100644 --- a/src/Commands/InitCommand.php +++ b/src/Commands/InitCommand.php @@ -18,6 +18,9 @@ class InitCommand extends Command implements Isolatable { + protected $signature = 'init {application-name : The application name }'; + protected $description = 'Initialize required project parameters to run DEV environment'; + public const array RESOURCES_ITEMS = [ 'issue_tracker' => 'Issue Tracker', 'figma' => 'Figma', @@ -42,20 +45,10 @@ class InitCommand extends Command implements Isolatable 'nova', ]; - protected $signature = 'init {application-name : The application name }'; - - protected $description = 'Initialize required project parameters to run DEV environment'; - - protected string $codeOwnerEmail; - protected array $resources = []; protected array $adminCredentials = []; - protected AuthTypeEnum $authType; - - protected string $appUrl; - protected array $emptyValuesList = []; protected array $shellCommands = [ @@ -72,88 +65,181 @@ class InitCommand extends Command implements Isolatable ]; protected bool $shouldUninstallPackage = false; + protected bool $shouldGenerateReadme; protected string $appName; - - protected string $dbConnection = 'pgsql'; - protected string $dbHost = 'pgsql'; - protected string $dbPort = '5432'; - protected string $dbName = 'postgres'; - protected string $dbUserName = 'postgres'; - + protected string $kebabName; + protected string $appUrl; protected AppTypeEnum $appType; + protected AuthTypeEnum $authType; + protected string $codeOwnerEmail; - protected $readmeGenerator; + protected string $envFile; + protected array $envConfig = [ + 'dbConnection' => 'pgsql', + 'dbHost' => 'pgsql', + 'dbPort' => '5432', + 'dbName' => 'postgres', + 'dbUserName' => 'postgres', + ]; - public function __construct(ReadmeGenerator $readme) - { + public function __construct( + protected ReadmeGenerator $readmeGenerator + ) { parent::__construct(); - $this->readmeGenerator = $readme; } public function handle(): void { $this->prepareAppName(); - $kebabName = Str::kebab($this->appName); - $this->codeOwnerEmail = $this->validateInput( method: fn () => $this->ask('Please specify a Code Owner/Team Lead\'s email'), field: 'email of code owner / team lead', rules: 'required|email', ); - $this->appUrl = $this->ask('Please enter an application URL', "https://api.dev.{$kebabName}.com"); + $this->appUrl = $this->ask('Please enter an application URL', "https://api.dev.{$this->kebabName}.com"); + + $this->updateEnvFile(); + + $this->info('Project initialized successfully!'); + + $this->appType = AppTypeEnum::from( + $this->choice( + question: 'What type of application will your API serve?', + choices: AppTypeEnum::values(), + default: AppTypeEnum::Multiplatform->value, + ), + ); + + $this->authType = AuthTypeEnum::from($this->choice( + question: 'Please choose the authentication type', + choices: AuthTypeEnum::values(), + default: AuthTypeEnum::None->value, + )); + + $this->configureClerk(); + + if ($this->confirm('Do you want to generate an admin user?', true)) { + $this->createAdminUser(); + } + + if ($this->shouldGenerateReadme = $this->confirm('Do you want to generate a README file?', true)) { + $this->generateReadme(); + } + + if ($this->confirm('Would you use Renovate dependabot?', true)) { + $this->saveRenovateJSON(); + + if ($this->shouldGenerateReadme) { + $this->readmeGenerator->fillRenovate(); + + $this->readmeGenerator->saveReadme(); + } + } + + if (!class_exists(\Laravel\Telescope\TelescopeServiceProvider::class)) { + array_push( + $this->shellCommands, + 'composer require ronasit/laravel-telescope-extension', + 'php artisan telescope:install', + ); + } + + if ($this->confirm('Do you want to install media package?')) { + $this->shellCommands[] = 'composer require ronasit/laravel-media'; + } + + if ($this->confirm('Do you want to uninstall project-initializator package?', true)) { + $this->shouldUninstallPackage = true; + } + + $this->setupComposerHooks(); + + $this->changeMiddlewareForTelescopeAuthorization(); + + $this->setAutoDocContactEmail($this->codeOwnerEmail); + + foreach ($this->shellCommands as $shellCommand) { + shell_exec("{$shellCommand} --ansi"); + } + + $this->publishWebLogin(); + + if ($this->shouldUninstallPackage) { + shell_exec('composer remove --dev ronasit/laravel-project-initializator --ansi'); + } + + $this->runMigrations(); + } + + protected function validateInput(callable $method, string $field, string|array $rules): string + { + $value = $method(); + + $validator = Validator::make([$field => $value], [$field => $rules]); + + if ($validator->fails()) { + $this->warn($validator->errors()->first()); + + $value = $this->validateInput($method, $field, $rules); + } + + return $value; + } + + protected function prepareAppName(): void + { + $this->appName = $this->argument('application-name'); + + $pascalCaseAppName = ucfirst(Str::camel($this->appName)); + + if ($this->appName !== $pascalCaseAppName && $this->confirm("The application name is not in PascalCase, would you like to use {$pascalCaseAppName}", true)) { + $this->appName = $pascalCaseAppName; + } - $envFile = (file_exists('.env')) ? '.env' : '.env.example'; + $this->kebabName = Str::kebab($this->appName); + } + + protected function updateEnvFile(): void + { + $this->envFile = (file_exists('.env')) ? '.env' : '.env.example'; $envConfig = [ 'APP_NAME' => $this->appName, - 'DB_CONNECTION' => $this->dbConnection, - 'DB_HOST' => $this->dbHost, - 'DB_PORT' => $this->dbPort, - 'DB_DATABASE' => $this->dbName, - 'DB_USERNAME' => $this->dbUserName, + 'DB_CONNECTION' => $this->envConfig['dbConnection'], + 'DB_HOST' => $this->envConfig['dbHost'], + 'DB_PORT' => $this->envConfig['dbPort'], + 'DB_DATABASE' => $this->envConfig['dbName'], + 'DB_USERNAME' => $this->envConfig['dbUserName'], 'DB_PASSWORD' => '', ]; - $this->updateEnvFile($envFile, $envConfig); + $this->writeEnvFile($this->envFile, $envConfig); if (!file_exists('.env.development')) { copy('.env.example', '.env.development'); } - $this->updateEnvFile('.env.development', [ + $this->writeEnvFile('.env.development', [ 'APP_NAME' => $this->appName, 'APP_URL' => $this->appUrl, 'APP_MAINTENANCE_DRIVER' => 'cache', 'CACHE_STORE' => 'redis', 'QUEUE_CONNECTION' => 'redis', 'SESSION_DRIVER' => 'redis', - 'DB_CONNECTION' => $this->dbConnection, + 'DB_CONNECTION' => $this->envConfig['dbConnection'], 'DB_HOST' => '', 'DB_PORT' => '', 'DB_DATABASE' => '', 'DB_USERNAME' => '', 'DB_PASSWORD' => '', ]); + } - $this->info('Project initialized successfully!'); - - $this->appType = AppTypeEnum::from( - $this->choice( - question: 'What type of application will your API serve?', - choices: AppTypeEnum::values(), - default: AppTypeEnum::Multiplatform->value, - ), - ); - - $this->authType = AuthTypeEnum::from($this->choice( - question: 'Please choose the authentication type', - choices: AuthTypeEnum::values(), - default: AuthTypeEnum::None->value, - )); - + protected function configureClerk(): void + { if ($this->authType === AuthTypeEnum::Clerk) { $this->enableClerk(); @@ -168,20 +254,104 @@ public function handle(): void $data['CLERK_ALLOWED_ORIGINS'] = ''; } - $this->updateEnvFile('.env.development', $data); - $this->updateEnvFile($envFile, $data); + $this->writeEnvFile('.env.development', $data); + $this->writeEnvFile($this->envFile, $data); - if ($envFile !== '.env.example') { - $this->updateEnvFile('.env.example', $data); + if ($this->envFile !== '.env.example') { + $this->writeEnvFile('.env.example', $data); } } + } - if ($this->confirm('Do you want to generate an admin user?', true)) { - $this->createAdminUser($kebabName); + protected function writeEnvFile(string $fileName, array $data): void + { + $env = EnvFile::open($fileName); + + // TODO: After updating wintercms/laravel-config-writer, remove the key comparison check and keep only $env->addEmptyLine(); + $envKeys = array_column($env->getAst(), 'match'); + $dataKeys = array_keys($data); + + $hasMissingKeys = count(array_intersect($dataKeys, $envKeys)) !== count($dataKeys); + + if ($hasMissingKeys) { + $env->addEmptyLine(); + } + + $env->set($data); + + $env->write(); + } + + protected function enableClerk(): void + { + array_push( + $this->shellCommands, + 'composer require ronasit/laravel-clerk', + 'php artisan laravel-clerk:install', + ); + + $this->publishMigration( + view: view('initializator::users_add_clerk_id_field'), + migrationName: 'users_add_clerk_id_field', + ); + + $this->publishClass( + template: view('initializator::clerk_user_repository'), + fileName: 'ClerkUserRepository', + filePath: 'app/Support/Clerk', + ); + } + + protected function createAdminUser(): void + { + $defaultPassword = substr(md5(uniqid()), 0, 8); + + $this->adminCredentials = [ + 'email' => $this->ask('Please enter an admin email', "admin@{$this->kebabName}.com"), + 'password' => $this->ask('Please enter an admin password', $defaultPassword), + ]; + + if ($this->authType === AuthTypeEnum::Clerk) { + $this->publishMigration( + view: view('initializator::admins_create_table')->with($this->adminCredentials), + migrationName: 'admins_create_table', + ); + } else { + $this->adminCredentials['name'] = $this->ask('Please enter an admin name', 'Admin'); + $this->adminCredentials['role_id'] = $this->ask('Please enter an admin role id', RoleEnum::Admin->value); + + $this->publishMigration( + view: view('initializator::add_default_user')->with($this->adminCredentials), + migrationName: 'add_default_user', + ); } + } + + protected function publishMigration(View $view, string $migrationName): void + { + $time = Carbon::now()->format('Y_m_d_His'); + + $migrationName = "{$time}_{$migrationName}"; + + $this->publishClass($view, $migrationName, 'database/migrations'); + } - if ($shouldGenerateReadme = $this->confirm('Do you want to generate a README file?', true)) { - $this->readmeGenerator->fillReadme($this->appName, $this->appType->value); + protected function publishClass(View $template, string $fileName, string $filePath): void + { + $fileName = "{$fileName}.php"; + + if (!is_dir($filePath)) { + mkdir($filePath, 0777, true); + } + + $data = $template->render(); + + file_put_contents("{$filePath}/{$fileName}", "readmeGenerator->fillReadme($this->appName, $this->appType->value); if ($this->confirm('Do you need a `Resources & Contacts` part?', true)) { $this->readmeGenerator->fillResourcesAndContacts(); @@ -202,7 +372,7 @@ public function handle(): void } if ($this->confirm('Do you need a `Credentials and Access` part?', true)) { - $this->fillCredentialsAndAccess($kebabName); + $this->fillCredentialsAndAccess(); if ($this->authType === AuthTypeEnum::Clerk) { $this->readmeGenerator->fillClerkAuthType(); @@ -220,51 +390,6 @@ public function handle(): void $this->warn("- {$value}"); } } - } - - if ($this->confirm('Would you use Renovate dependabot?', true)) { - $this->saveRenovateJSON(); - - if ($shouldGenerateReadme) { - $this->readmeGenerator->fillRenovate(); - - $this->readmeGenerator->saveReadme(); - } - } - - if (!class_exists(\Laravel\Telescope\TelescopeServiceProvider::class)) { - array_push( - $this->shellCommands, - 'composer require ronasit/laravel-telescope-extension', - 'php artisan telescope:install', - ); - } - - if ($this->confirm('Do you want to install media package?')) { - $this->shellCommands[] = 'composer require ronasit/laravel-media'; - } - - if ($this->confirm('Do you want to uninstall project-initializator package?', true)) { - $this->shouldUninstallPackage = true; - } - - $this->setupComposerHooks(); - - $this->changeMiddlewareForTelescopeAuthorization(); - - $this->setAutoDocContactEmail($this->codeOwnerEmail); - - foreach ($this->shellCommands as $shellCommand) { - shell_exec("{$shellCommand} --ansi"); - } - - $this->publishWebLogin(); - - if ($this->shouldUninstallPackage) { - shell_exec('composer remove --dev ronasit/laravel-project-initializator --ansi'); - } - - $this->runMigrations(); } protected function fillResources(): void @@ -332,7 +457,7 @@ protected function fillGettingStarted(): void $this->readmeGenerator->updateReadmeFile($filePart); } - protected function fillCredentialsAndAccess(string $kebabName): void + protected function fillCredentialsAndAccess(): void { $filePart = $this->readmeGenerator->loadReadmePart('CREDENTIALS_AND_ACCESS.md'); @@ -356,7 +481,7 @@ protected function fillCredentialsAndAccess(string $kebabName): void } else { $defaultPassword = substr(md5(uniqid()), 0, 8); - $email = $this->ask("Please enter a {$title}'s admin email", "admin@{$kebabName}.com"); + $email = $this->ask("Please enter a {$title}'s admin email", "admin@{$this->kebabName}.com"); $password = $this->ask("Please enter a {$title}'s admin password", $defaultPassword); } @@ -368,6 +493,23 @@ protected function fillCredentialsAndAccess(string $kebabName): void $this->readmeGenerator->updateReadmeFile($filePart); } + protected function saveRenovateJSON(): void + { + $reviewer = $this->validateInput( + method: fn () => $this->ask('Please type username of the project reviewer', Str::before($this->codeOwnerEmail, '@')), + field: 'username of the project reviewer', + rules: 'required|alpha_dash', + ); + + $data = [ + '$schema' => 'https://docs.renovatebot.com/renovate-schema.json', + 'extends' => ['config:recommended'], + 'enabledManagers' => ['composer'], + 'assignees' => [$reviewer], + ]; + + file_put_contents('renovate.json', json_encode($data, JSON_PRETTY_PRINT)); + } protected function setupComposerHooks(): void { @@ -398,160 +540,26 @@ protected function addArrayItemIfMissing(array &$data, string $path, string $val } } - protected function setAutoDocContactEmail(string $email): void + protected function changeMiddlewareForTelescopeAuthorization(): void { - $config = ArrayFile::open(base_path('config/auto-doc.php')); - - $config->set('info.contact.email', $email); - - $config->write(); - } + $config = ArrayFile::open(base_path('config/telescope.php')); - protected function runMigrations(): void - { - config([ - 'database.default' => $this->dbConnection, - 'database.connections.pgsql' => [ - 'driver' => $this->dbConnection, - 'host' => $this->dbHost, - 'port' => $this->dbPort, - 'database' => $this->dbName, - 'username' => $this->dbUserName, - 'password' => '', - ], + // TODO: add Authorize::class middleware after inplementing an ability to modify functions in the https://github.com/RonasIT/larabuilder package + $config->set('middleware', [ + 'web', + 'auth:web', ]); - shell_exec('php artisan migrate --ansi'); - } - - protected function createAdminUser(string $kebabName): void - { - $defaultPassword = substr(md5(uniqid()), 0, 8); - - $this->adminCredentials = [ - 'email' => $this->ask('Please enter an admin email', "admin@{$kebabName}.com"), - 'password' => $this->ask('Please enter an admin password', $defaultPassword), - ]; - - if ($this->authType === AuthTypeEnum::Clerk) { - $this->publishMigration( - view: view('initializator::admins_create_table')->with($this->adminCredentials), - migrationName: 'admins_create_table', - ); - } else { - $this->adminCredentials['name'] = $this->ask('Please enter an admin name', 'Admin'); - $this->adminCredentials['role_id'] = $this->ask('Please enter an admin role id', RoleEnum::Admin->value); - - $this->publishMigration( - view: view('initializator::add_default_user')->with($this->adminCredentials), - migrationName: 'add_default_user', - ); - } - } - - protected function publishClass(View $template, string $fileName, string $filePath): void - { - $fileName = "{$fileName}.php"; - - if (!is_dir($filePath)) { - mkdir($filePath, 0777, true); - } - - $data = $template->render(); - - file_put_contents("{$filePath}/{$fileName}", "format('Y_m_d_His'); - - $migrationName = "{$time}_{$migrationName}"; - - $this->publishClass($view, $migrationName, 'database/migrations'); - } - - protected function updateEnvFile(string $fileName, array $data): void - { - $env = EnvFile::open($fileName); - - // TODO: After updating wintercms/laravel-config-writer, remove the key comparison check and keep only $env->addEmptyLine(); - $envKeys = array_column($env->getAst(), 'match'); - $dataKeys = array_keys($data); - - $hasMissingKeys = count(array_intersect($dataKeys, $envKeys)) !== count($dataKeys); - - if ($hasMissingKeys) { - $env->addEmptyLine(); - } - - $env->set($data); - - $env->write(); - } - - protected function prepareAppName(): void - { - $this->appName = $this->argument('application-name'); - - $pascalCaseAppName = ucfirst(Str::camel($this->appName)); - - if ($this->appName !== $pascalCaseAppName && $this->confirm("The application name is not in PascalCase, would you like to use {$pascalCaseAppName}", true)) { - $this->appName = $pascalCaseAppName; - } - } - - protected function saveRenovateJSON(): void - { - $reviewer = $this->validateInput( - method: fn () => $this->ask('Please type username of the project reviewer', Str::before($this->codeOwnerEmail, '@')), - field: 'username of the project reviewer', - rules: 'required|alpha_dash', - ); - - $data = [ - '$schema' => 'https://docs.renovatebot.com/renovate-schema.json', - 'extends' => ['config:recommended'], - 'enabledManagers' => ['composer'], - 'assignees' => [$reviewer], - ]; - - file_put_contents('renovate.json', json_encode($data, JSON_PRETTY_PRINT)); - } - - protected function validateInput(callable $method, string $field, string|array $rules): string - { - $value = $method(); - - $validator = Validator::make([$field => $value], [$field => $rules]); - - if ($validator->fails()) { - $this->warn($validator->errors()->first()); - - $value = $this->validateInput($method, $field, $rules); - } - - return $value; + $config->write(); } - protected function enableClerk(): void + protected function setAutoDocContactEmail(string $email): void { - array_push( - $this->shellCommands, - 'composer require ronasit/laravel-clerk', - 'php artisan laravel-clerk:install', - ); + $config = ArrayFile::open(base_path('config/auto-doc.php')); - $this->publishMigration( - view: view('initializator::users_add_clerk_id_field'), - migrationName: 'users_add_clerk_id_field', - ); + $config->set('info.contact.email', $email); - $this->publishClass( - template: view('initializator::clerk_user_repository'), - fileName: 'ClerkUserRepository', - filePath: 'app/Support/Clerk', - ); + $config->write(); } protected function publishWebLogin(): void @@ -561,16 +569,20 @@ protected function publishWebLogin(): void file_put_contents(base_path('routes/web.php'), "\nAuth::routes();\n", FILE_APPEND); } - protected function changeMiddlewareForTelescopeAuthorization(): void + protected function runMigrations(): void { - $config = ArrayFile::open(base_path('config/telescope.php')); - - // TODO: add Authorize::class middleware after inplementing an ability to modify functions in the https://github.com/RonasIT/larabuilder package - $config->set('middleware', [ - 'web', - 'auth:web', + config([ + 'database.default' => $this->envConfig['dbConnection'], + 'database.connections.pgsql' => [ + 'driver' => $this->envConfig['dbConnection'], + 'host' => $this->envConfig['dbHost'], + 'port' => $this->envConfig['dbPort'], + 'database' => $this->envConfig['dbName'], + 'username' => $this->envConfig['dbUserName'], + 'password' => '', + ], ]); - $config->write(); + shell_exec('php artisan migrate --ansi'); } } From c8f6e00ff412f6a54ca1235f087d530cac709fad Mon Sep 17 00:00:00 2001 From: DenTray Date: Thu, 23 Oct 2025 09:58:35 +0600 Subject: [PATCH 2/8] Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/Commands/InitCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/InitCommand.php b/src/Commands/InitCommand.php index 56f20c0..f4c37ef 100644 --- a/src/Commands/InitCommand.php +++ b/src/Commands/InitCommand.php @@ -202,7 +202,7 @@ protected function prepareAppName(): void $this->kebabName = Str::kebab($this->appName); } - protected function updateEnvFile(): void + protected function updateEnvFile(): void { $this->envFile = (file_exists('.env')) ? '.env' : '.env.example'; From 8d9579c2ddcda57a6c5ef633eeef3805f5aec075 Mon Sep 17 00:00:00 2001 From: DenTray Date: Thu, 23 Oct 2025 10:01:52 +0600 Subject: [PATCH 3/8] Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- tests/InitCommandTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/InitCommandTest.php b/tests/InitCommandTest.php index 6070466..ca3453a 100644 --- a/tests/InitCommandTest.php +++ b/tests/InitCommandTest.php @@ -389,7 +389,6 @@ public function testRunWithAdminAndPartialReadmeCreation() $this->callFileGetContent($this->generateResourcePath('md/readme/CONTACTS.md'), $this->getReadmeTemplateContent('CONTACTS.md')), $this->callFileGetContent($this->generateResourcePath('md/readme/ENVIRONMENTS.md'), $this->getReadmeTemplateContent('ENVIRONMENTS.md')), $this->callFileGetContent($this->generateResourcePath('md/readme/CREDENTIALS_AND_ACCESS.md'), $this->getReadmeTemplateContent('CREDENTIALS_AND_ACCESS.md')), - $this->callFilePutContent('README.md', $this->getFixture('partial_readme.md')), ); From fb4793e2f4519cac3d057ce03d1b113a1ab69675 Mon Sep 17 00:00:00 2001 From: DenTray Date: Thu, 23 Oct 2025 10:02:21 +0600 Subject: [PATCH 4/8] Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- tests/InitCommandTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/InitCommandTest.php b/tests/InitCommandTest.php index ca3453a..7c718d8 100644 --- a/tests/InitCommandTest.php +++ b/tests/InitCommandTest.php @@ -636,7 +636,6 @@ public function testRunWithoutAdminAndUsingTelescope() $this->callFileGetContent($this->generateResourcePath('md/readme/CONTACTS.md'), $this->getReadmeTemplateContent('CONTACTS.md')), $this->callFileGetContent($this->generateResourcePath('md/readme/ENVIRONMENTS.md'), $this->getReadmeTemplateContent('ENVIRONMENTS.md')), $this->callFileGetContent($this->generateResourcePath('md/readme/CREDENTIALS_AND_ACCESS.md'), $this->getReadmeTemplateContent('CREDENTIALS_AND_ACCESS.md')), - $this->callFilePutContent('README.md', $this->getFixture('partial_readme_with_telescope.md')), ); From 44b4c689088b9a730590d4b7b4770d799a05ac33 Mon Sep 17 00:00:00 2001 From: DenTray Date: Thu, 23 Oct 2025 10:02:41 +0600 Subject: [PATCH 5/8] Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- tests/InitCommandTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/InitCommandTest.php b/tests/InitCommandTest.php index 7c718d8..68fb601 100644 --- a/tests/InitCommandTest.php +++ b/tests/InitCommandTest.php @@ -512,7 +512,6 @@ public function testRunWithAdminAndFullReadmeCreationAndRemovingInitializatorIns $this->callFileGetContent($this->generateResourcePath('md/readme/ENVIRONMENTS.md'), $this->getReadmeTemplateContent('ENVIRONMENTS.md')), $this->callFileGetContent($this->generateResourcePath('md/readme/CREDENTIALS_AND_ACCESS.md'), $this->getReadmeTemplateContent('CREDENTIALS_AND_ACCESS.md')), $this->callFileGetContent($this->generateResourcePath('md/readme/RENOVATE.md'), $this->getReadmeTemplateContent('RENOVATE.md')), - $this->callFilePutContent('README.md', $this->getFixture('full_readme.md')), $this->callFilePutContent('README.md', $this->getFixture('full_readme_after_using_renovate.md')), ); From f2689fe72e5479199227467566053ef36af22ed0 Mon Sep 17 00:00:00 2001 From: DenTray Date: Thu, 23 Oct 2025 10:02:58 +0600 Subject: [PATCH 6/8] Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- tests/InitCommandTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/InitCommandTest.php b/tests/InitCommandTest.php index 68fb601..3b1a97b 100644 --- a/tests/InitCommandTest.php +++ b/tests/InitCommandTest.php @@ -767,7 +767,6 @@ public function testRunWithClerkMobileAppWithPintInstalled(): void $this->callFileGetContent($this->generateResourcePath('md/readme/CREDENTIALS_AND_ACCESS.md'), $this->getReadmeTemplateContent('CREDENTIALS_AND_ACCESS.md')), $this->callFileGetContent($this->generateResourcePath('md/readme/CLERK.md'), $this->getReadmeTemplateContent('CLERK.md')), $this->callFileGetContent($this->generateResourcePath('md/readme/RENOVATE.md'), $this->getReadmeTemplateContent('RENOVATE.md')), - $this->callFilePutContent('README.md', $this->getFixture('default_readme_with_mobile_app.md')), $this->callFilePutContent('README.md', $this->getFixture('default_readme_with_mobile_app_after_using_renovate.md')), ); From 463c889538206b5791f4ca8b66cc7094b1ddcec7 Mon Sep 17 00:00:00 2001 From: Artyom Osepyan Date: Thu, 23 Oct 2025 11:35:54 +0300 Subject: [PATCH 7/8] style: fix code style --- src/Commands/InitCommand.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Commands/InitCommand.php b/src/Commands/InitCommand.php index bb4187d..2185678 100644 --- a/src/Commands/InitCommand.php +++ b/src/Commands/InitCommand.php @@ -590,6 +590,7 @@ protected function runMigrations(): void shell_exec('php artisan migrate --ansi'); } + protected function publishAdminMigration(array $adminCredentials, ?string $serviceKey): void { $migrationName = (empty($serviceKey)) ? 'add_default_admin' : "add_{$serviceKey}_admin"; From 41b7780bf4192affa1a6f665525066e145faf30b Mon Sep 17 00:00:00 2001 From: Artyom Osepyan Date: Tue, 4 Nov 2025 15:12:47 +0300 Subject: [PATCH 8/8] fix: remarks from reviewer --- src/Commands/InitCommand.php | 77 ++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 39 deletions(-) diff --git a/src/Commands/InitCommand.php b/src/Commands/InitCommand.php index 50b4dc2..ff76fd1 100644 --- a/src/Commands/InitCommand.php +++ b/src/Commands/InitCommand.php @@ -19,6 +19,7 @@ class InitCommand extends Command implements Isolatable { protected $signature = 'init {application-name : The application name }'; + protected $description = 'Initialize required project parameters to run DEV environment'; public const array RESOURCES_ITEMS = [ @@ -93,12 +94,12 @@ public function handle(): void { $this->prepareAppName(); - $this->codeOwnerEmail = $this->validateInput( - method: fn () => $this->ask('Please specify a Code Owner/Team Lead\'s email'), + $this->codeOwnerEmail = $this->askWithValidation( + parameter: 'Please specify a Code Owner/Team Lead\'s email', field: 'email of code owner / team lead', - rules: 'required|email', + rules: 'required|email' ); - + $this->appUrl = $this->ask('Please enter an application URL', "https://api.dev.{$this->kebabName}.com"); $this->updateEnvFile(); @@ -119,7 +120,9 @@ public function handle(): void default: AuthTypeEnum::None->value, )); - $this->configureClerk(); + if ($this->authType === AuthTypeEnum::Clerk) { + $this->configureClerk(); + } if ($this->confirm('Do you want to generate an admin user?', true)) { if ($this->authType === AuthTypeEnum::Clerk) { @@ -154,6 +157,7 @@ public function handle(): void } } } + if (!class_exists(\Laravel\Telescope\TelescopeServiceProvider::class)) { array_push( $this->shellCommands, @@ -189,16 +193,18 @@ public function handle(): void $this->runMigrations(); } - protected function validateInput(callable $method, string $field, string|array $rules): string + protected function askWithValidation(string $parameter, string $field, string|array $rules, ?string $default = null): string { - $value = $method(); + $value = $default + ? $this->ask($parameter, $default) + : $this->ask($parameter); $validator = Validator::make([$field => $value], [$field => $rules]); if ($validator->fails()) { $this->warn($validator->errors()->first()); - $value = $this->validateInput($method, $field, $rules); + $value = $this->askWithValidation($parameter, $field,$rules, $default); } return $value; @@ -206,14 +212,16 @@ protected function validateInput(callable $method, string $field, string|array $ protected function prepareAppName(): void { - $this->appName = $this->argument('application-name'); + $appName = $this->argument('application-name'); - $pascalCaseAppName = ucfirst(Str::camel($this->appName)); + $pascalCaseAppName = ucfirst(Str::camel($appName)); - if ($this->appName !== $pascalCaseAppName && $this->confirm("The application name is not in PascalCase, would you like to use {$pascalCaseAppName}", true)) { - $this->appName = $pascalCaseAppName; + if ($appName !== $pascalCaseAppName && $this->confirm("The application name is not in PascalCase, would you like to use {$pascalCaseAppName}", true)) { + $appName = $pascalCaseAppName; } + $this->appName = $appName; + $this->kebabName = Str::kebab($this->appName); } @@ -255,26 +263,24 @@ protected function updateEnvFile(): void protected function configureClerk(): void { - if ($this->authType === AuthTypeEnum::Clerk) { - $this->enableClerk(); + $this->enableClerk(); - $data = [ - 'AUTH_GUARD' => 'clerk', - 'CLERK_ALLOWED_ISSUER' => '', - 'CLERK_SECRET_KEY' => '', - 'CLERK_SIGNER_KEY_PATH' => '', - ]; + $data = [ + 'AUTH_GUARD' => 'clerk', + 'CLERK_ALLOWED_ISSUER' => '', + 'CLERK_SECRET_KEY' => '', + 'CLERK_SIGNER_KEY_PATH' => '', + ]; - if ($this->appType !== AppTypeEnum::Mobile) { - $data['CLERK_ALLOWED_ORIGINS'] = ''; - } + if ($this->appType !== AppTypeEnum::Mobile) { + $data['CLERK_ALLOWED_ORIGINS'] = ''; + } - $this->writeEnvFile('.env.development', $data); - $this->writeEnvFile($this->envFile, $data); + $this->writeEnvFile('.env.development', $data); + $this->writeEnvFile($this->envFile, $data); - if ($this->envFile !== '.env.example') { - $this->writeEnvFile('.env.example', $data); - } + if ($this->envFile !== '.env.example') { + $this->writeEnvFile('.env.example', $data); } } @@ -282,15 +288,7 @@ protected function writeEnvFile(string $fileName, array $data): void { $env = EnvFile::open($fileName); - // TODO: After updating wintercms/laravel-config-writer, remove the key comparison check and keep only $env->addEmptyLine(); - $envKeys = array_column($env->getAst(), 'match'); - $dataKeys = array_keys($data); - - $hasMissingKeys = count(array_intersect($dataKeys, $envKeys)) !== count($dataKeys); - - if ($hasMissingKeys) { - $env->addEmptyLine(); - } + $env->addEmptyLine(); $env->set($data); @@ -499,10 +497,11 @@ protected function fillCredentialsAndAccess(): void protected function saveRenovateJSON(): void { - $reviewer = $this->validateInput( - method: fn () => $this->ask('Please type username of the project reviewer', Str::before($this->codeOwnerEmail, '@')), + $reviewer = $this->askWithValidation( + parameter: 'Please type username of the project reviewer', field: 'username of the project reviewer', rules: 'required|alpha_dash', + default: Str::before($this->codeOwnerEmail, '@') ); $data = [