From b34edd10d66a3d018b6ae34563a8a92b9408fdaa Mon Sep 17 00:00:00 2001 From: luanfreitasdev Date: Wed, 12 Mar 2025 11:09:47 -0300 Subject: [PATCH 1/4] Move database ask to top --- src/NewCommand.php | 172 +++++++++++++++++++++++++++------------------ 1 file changed, 103 insertions(+), 69 deletions(-) diff --git a/src/NewCommand.php b/src/NewCommand.php index 3d09c69..63b2234 100644 --- a/src/NewCommand.php +++ b/src/NewCommand.php @@ -48,6 +48,7 @@ protected function configure() ->addOption('github', null, InputOption::VALUE_OPTIONAL, 'Create a new repository on GitHub', false) ->addOption('organization', null, InputOption::VALUE_REQUIRED, 'The GitHub organization to create the new repository for') ->addOption('database', null, InputOption::VALUE_REQUIRED, 'The database driver your application will use') + ->addOption('migrate', null, InputOption::VALUE_NONE, 'Run the default database migrations') ->addOption('react', null, InputOption::VALUE_NONE, 'Install the React Starter Kit') ->addOption('vue', null, InputOption::VALUE_NONE, 'Install the Vue Starter Kit') ->addOption('livewire', null, InputOption::VALUE_NONE, 'Install the Livewire Starter Kit') @@ -109,58 +110,10 @@ protected function interact(InputInterface $input, OutputInterface $output) ); } - if (! $this->usingStarterKit($input)) { - match (select( - label: 'Which starter kit would you like to install?', - options: [ - 'none' => 'None', - 'react' => 'React', - 'vue' => 'Vue', - 'livewire' => 'Livewire', - ], - default: 'none', - )) { - 'react' => $input->setOption('react', true), - 'vue' => $input->setOption('vue', true), - 'livewire' => $input->setOption('livewire', true), - default => null, - }; - - if ($this->usingLaravelStarterKit($input)) { - match (select( - label: 'Which authentication provider do you prefer?', - options: [ - 'laravel' => "Laravel's built-in authentication", - 'workos' => 'WorkOS (Requires WorkOS account)', - ], - default: 'laravel', - )) { - 'laravel' => $input->setOption('workos', false), - 'workos' => $input->setOption('workos', true), - default => null, - }; - } - - if ($input->getOption('livewire') && ! $input->getOption('workos')) { - $input->setOption('livewire-class-components', ! confirm( - label: 'Would you like to use Laravel Volt?', - default: true, - )); - } - } - - if ($this->usingLaravelStarterKit($input)) { - if (! $input->getOption('phpunit') && - ! $input->getOption('pest')) { - $input->setOption('pest', select( - label: 'Which testing framework do you prefer?', - options: ['Pest', 'PHPUnit'], - default: 'Pest', - ) === 'Pest'); - } - } else { - $input->setOption('phpunit', true); - } + $this->promptForStarterKitOptions($input); + $this->promptForTestingFrameworkOptions($input); + $this->promptForDatabaseOptions($input); + $this->promptForNpmOptions($input); } /** @@ -267,7 +220,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int $directory.'/.env' ); - [$database, $migrate] = $this->promptForDatabaseOptions($directory, $input); + $database = $input->getOption('database'); + $migrate = $input->getOption('migrate'); $this->configureDefaultDatabaseConnection($directory, $database, $name); @@ -308,12 +262,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int $runNpm = $input->getOption('npm'); - if (! $input->getOption('npm') && $input->isInteractive()) { - $runNpm = confirm( - label: 'Would you like to run npm install and npm run build?' - ); - } - if ($runNpm) { $this->runCommands(['npm install', 'npm run build'], $input, $output, workingPath: $directory); } @@ -502,11 +450,10 @@ protected function uncommentDatabaseConfiguration(string $directory) /** * Determine the default database connection. * - * @param string $directory * @param \Symfony\Component\Console\Input\InputInterface $input - * @return array + * @return void */ - protected function promptForDatabaseOptions(string $directory, InputInterface $input) + protected function promptForDatabaseOptions(InputInterface $input) { $defaultDatabase = collect( $databaseOptions = $this->databaseOptions() @@ -514,8 +461,7 @@ protected function promptForDatabaseOptions(string $directory, InputInterface $i if ($this->usingStarterKit($input)) { // Starter kits will already be migrated in post composer create-project command... - $migrate = false; - + $input->setOption('migrate', false); $input->setOption('database', 'sqlite'); } @@ -527,15 +473,103 @@ protected function promptForDatabaseOptions(string $directory, InputInterface $i )); if ($input->getOption('database') !== 'sqlite') { - $migrate = confirm( - label: 'Default database updated. Would you like to run the default database migrations?' - ); + $input->setOption('migrate', confirm( + label: 'Would you like to run the default database migrations?' + )); } else { - $migrate = true; + $input->setOption('migrate', false); } } + } + + /** + * Determine if npm should be run. + * + * @param \Symfony\Component\Console\Input\InputInterface $input + * @return void + */ + protected function promptForNpmOptions(InputInterface $input) + { + if ($input->getOption('npm') && !$input->isInteractive()) { + return; + } - return [$input->getOption('database') ?? $defaultDatabase, $migrate ?? $input->hasOption('database')]; + $input->setOption('npm', confirm( + label: 'Would you like to run npm install and npm run build?' + )); + } + + /** + * Determine if a starter kit should be installed. + * + * @param \Symfony\Component\Console\Input\InputInterface $input + * @return void + */ + protected function promptForStarterKitOptions(InputInterface $input) + { + if ($this->usingStarterKit($input)) { + return; + } + + match (select( + label: 'Which starter kit would you like to install?', + options: [ + 'none' => 'None', + 'react' => 'React', + 'vue' => 'Vue', + 'livewire' => 'Livewire', + ], + default: 'none', + )) { + 'react' => $input->setOption('react', true), + 'vue' => $input->setOption('vue', true), + 'livewire' => $input->setOption('livewire', true), + default => null, + }; + + if ($this->usingLaravelStarterKit($input)) { + match (select( + label: 'Which authentication provider do you prefer?', + options: [ + 'laravel' => "Laravel's built-in authentication", + 'workos' => 'WorkOS (Requires WorkOS account)', + ], + default: 'laravel', + )) { + 'laravel' => $input->setOption('workos', false), + 'workos' => $input->setOption('workos', true), + default => null, + }; + } + + if ($input->getOption('livewire') && ! $input->getOption('workos')) { + $input->setOption('livewire-class-components', ! confirm( + label: 'Would you like to use Laravel Volt?', + default: true, + )); + } + } + + /** + * Determine if Pest or PHPUnit should be installed. + * + * @param \Symfony\Component\Console\Input\InputInterface $input + * @return void + */ + protected function promptForTestingFrameworkOptions(InputInterface $input) + { + if ($this->usingLaravelStarterKit($input)) { + if (! $input->getOption('phpunit') && + ! $input->getOption('pest')) { + $input->setOption('pest', select( + label: 'Which testing framework do you prefer?', + options: ['Pest', 'PHPUnit'], + default: 'Pest', + ) === 'Pest'); + } + } else { + $input->setOption('phpunit', true); + } } /** From b133e4791f911cd2753673fdfa8593bacfef4593 Mon Sep 17 00:00:00 2001 From: luanfreitasdev Date: Wed, 12 Mar 2025 11:32:58 -0300 Subject: [PATCH 2/4] Refactor database option handling and add default database method --- src/NewCommand.php | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/NewCommand.php b/src/NewCommand.php index 63b2234..e4452c1 100644 --- a/src/NewCommand.php +++ b/src/NewCommand.php @@ -48,7 +48,7 @@ protected function configure() ->addOption('github', null, InputOption::VALUE_OPTIONAL, 'Create a new repository on GitHub', false) ->addOption('organization', null, InputOption::VALUE_REQUIRED, 'The GitHub organization to create the new repository for') ->addOption('database', null, InputOption::VALUE_REQUIRED, 'The database driver your application will use') - ->addOption('migrate', null, InputOption::VALUE_NONE, 'Run the default database migrations') + ->addOption('migrate', null, InputOption::VALUE_OPTIONAL, 'Run the default database migrations', false) ->addOption('react', null, InputOption::VALUE_NONE, 'Install the React Starter Kit') ->addOption('vue', null, InputOption::VALUE_NONE, 'Install the Vue Starter Kit') ->addOption('livewire', null, InputOption::VALUE_NONE, 'Install the Livewire Starter Kit') @@ -220,7 +220,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $directory.'/.env' ); - $database = $input->getOption('database'); + $database = $input->getOption('database') ?? $this->defaultDataBase(); $migrate = $input->getOption('migrate'); $this->configureDefaultDatabaseConnection($directory, $database, $name); @@ -304,6 +304,18 @@ protected function defaultBranch() return $process->isSuccessful() && $output ? $output : 'main'; } + /** + * Return default database + * + * @return string + */ + protected function defaultDatabase() + { + return collect( + $this->databaseOptions() + )->keys()->first(); + } + /** * Configure the default database connection. * @@ -455,9 +467,7 @@ protected function uncommentDatabaseConfiguration(string $directory) */ protected function promptForDatabaseOptions(InputInterface $input) { - $defaultDatabase = collect( - $databaseOptions = $this->databaseOptions() - )->keys()->first(); + $defaultDatabase = $this->defaultDataBase(); if ($this->usingStarterKit($input)) { // Starter kits will already be migrated in post composer create-project command... @@ -468,7 +478,7 @@ protected function promptForDatabaseOptions(InputInterface $input) if (! $input->getOption('database') && $input->isInteractive()) { $input->setOption('database', select( label: 'Which database will your application use?', - options: $databaseOptions, + options: $this->databaseOptions(), default: $defaultDatabase, )); @@ -734,7 +744,6 @@ protected function pushToGitHub(string $name, string $directory, InputInterface /** * Configure the Composer "dev" script. * - * @param string $directory * @return void */ protected function configureComposerDevScript(string $directory): void From 9e7404a8db7fc52691dd9936c0672e56f553d685 Mon Sep 17 00:00:00 2001 From: luanfreitasdev Date: Wed, 12 Mar 2025 11:41:44 -0300 Subject: [PATCH 3/4] Reset changes for the configureComposerDevScript method --- src/NewCommand.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/NewCommand.php b/src/NewCommand.php index e4452c1..5167d60 100644 --- a/src/NewCommand.php +++ b/src/NewCommand.php @@ -744,6 +744,7 @@ protected function pushToGitHub(string $name, string $directory, InputInterface /** * Configure the Composer "dev" script. * + * @param string $directory * @return void */ protected function configureComposerDevScript(string $directory): void From b7fea5308008ac6a414a67a28086ba9e47fb8c66 Mon Sep 17 00:00:00 2001 From: luanfreitasdev Date: Wed, 12 Mar 2025 11:45:21 -0300 Subject: [PATCH 4/4] code style --- src/NewCommand.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/NewCommand.php b/src/NewCommand.php index 5167d60..e51479d 100644 --- a/src/NewCommand.php +++ b/src/NewCommand.php @@ -305,7 +305,7 @@ protected function defaultBranch() } /** - * Return default database + * Return default database. * * @return string */ @@ -500,7 +500,7 @@ protected function promptForDatabaseOptions(InputInterface $input) */ protected function promptForNpmOptions(InputInterface $input) { - if ($input->getOption('npm') && !$input->isInteractive()) { + if ($input->getOption('npm') && ! $input->isInteractive()) { return; } @@ -572,10 +572,10 @@ protected function promptForTestingFrameworkOptions(InputInterface $input) if (! $input->getOption('phpunit') && ! $input->getOption('pest')) { $input->setOption('pest', select( - label: 'Which testing framework do you prefer?', - options: ['Pest', 'PHPUnit'], - default: 'Pest', - ) === 'Pest'); + label: 'Which testing framework do you prefer?', + options: ['Pest', 'PHPUnit'], + default: 'Pest', + ) === 'Pest'); } } else { $input->setOption('phpunit', true);