diff --git a/src/NewCommand.php b/src/NewCommand.php index 34e3ae2..f9bef70 100644 --- a/src/NewCommand.php +++ b/src/NewCommand.php @@ -89,12 +89,28 @@ protected function interact(InputInterface $input, OutputInterface $output) label: 'What is the name of your project?', placeholder: 'E.g. example-app', required: 'The project name is required.', - validate: fn ($value) => preg_match('/[^\pL\pN\-_.]/', $value) !== 0 - ? 'The name may only contain letters, numbers, dashes, underscores, and periods.' - : null, + validate: function ($value) use ($input) { + if (preg_match('/[^\pL\pN\-_.]/', $value) !== 0) { + return 'The name may only contain letters, numbers, dashes, underscores, and periods.'; + } + + if ($input->getOption('force') !== true) { + try { + $this->verifyApplicationDoesntExist($this->getInstallationDirectory($value)); + } catch (RuntimeException $e) { + return 'Application already exists.'; + } + } + }, )); } + if ($input->getOption('force') !== true) { + $this->verifyApplicationDoesntExist( + $this->getInstallationDirectory($input->getArgument('name')) + ); + } + if (! $input->getOption('breeze') && ! $input->getOption('jet')) { match (select( label: 'Would you like to install a starter kit?', @@ -144,7 +160,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $name = $input->getArgument('name'); - $directory = $name !== '.' ? getcwd().'/'.$name : '.'; + $directory = $this->getInstallationDirectory($name); $this->composer = new Composer(new Filesystem(), $directory); @@ -788,6 +804,17 @@ protected function isParked(string $directory) return $output !== false ? in_array(dirname($directory), json_decode($output)) : false; } + /** + * Get the installation directory. + * + * @param string $name + * @return string + */ + protected function getInstallationDirectory(string $name) + { + return $name !== '.' ? getcwd().'/'.$name : '.'; + } + /** * Get the version that should be downloaded. *