Skip to content

Reorder database and npm selection prompts for better clarity #411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 117 additions & 73 deletions src/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_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')
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -267,7 +220,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$directory.'/.env'
);

[$database, $migrate] = $this->promptForDatabaseOptions($directory, $input);
$database = $input->getOption('database') ?? $this->defaultDataBase();
$migrate = $input->getOption('migrate');

$this->configureDefaultDatabaseConnection($directory, $database, $name);

Expand Down Expand Up @@ -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 <options=bold>npm install</> and <options=bold>npm run build</>?'
);
}

if ($runNpm) {
$this->runCommands(['npm install', 'npm run build'], $input, $output, workingPath: $directory);
}
Expand Down Expand Up @@ -356,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.
*
Expand Down Expand Up @@ -502,40 +462,124 @@ 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()
)->keys()->first();
$defaultDatabase = $this->defaultDataBase();

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');
}

if (! $input->getOption('database') && $input->isInteractive()) {
$input->setOption('database', select(
label: 'Which database will your application use?',
options: $databaseOptions,
options: $this->databaseOptions(),
default: $defaultDatabase,
));

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 <options=bold>npm install</> and <options=bold>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);
}
}

/**
Expand Down