Skip to content

Commit

Permalink
Validate installer can be installed on selected directory
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone committed Jun 5, 2024
1 parent 5d45e78 commit b9c3c5b
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,19 @@ 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->getWorkingDirectory($value));
} catch (RuntimeException $e) {
return 'The directory already exists';
}
}
},
));
}

Expand Down Expand Up @@ -144,7 +154,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$name = $input->getArgument('name');

$directory = $name !== '.' ? getcwd().'/'.$name : '.';
$directory = $this->getWorkingDirectory($name);

$this->composer = new Composer(new Filesystem(), $directory);

Expand Down Expand Up @@ -803,6 +813,17 @@ protected function getVersion(InputInterface $input)
return '';
}

/**
* Get the working installation directory.
*
* @param string $name
* @return string
*/
protected function getWorkingDirectory(string $name)
{
return $name !== '.' ? getcwd().'/'.$name : '.';
}

/**
* Get the composer command for the environment.
*
Expand Down

0 comments on commit b9c3c5b

Please sign in to comment.