Add package-install
command
Add a command to install a new package into a project using Composer, npm, yarn or pnpm.
This commit is contained in:
parent
5d32389606
commit
82900c719c
3 changed files with 71 additions and 0 deletions
68
src/Console/Command/PackageInstallCommand.php
Normal file
68
src/Console/Command/PackageInstallCommand.php
Normal file
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
|
||||
namespace App\Console\Command;
|
||||
|
||||
use App\Action\DetermineProjectLanguage;
|
||||
use App\Enum\ProjectLanguage;
|
||||
use App\Process\Process;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
final class PackageInstallCommand extends AbstractCommand
|
||||
{
|
||||
public function configure(): void
|
||||
{
|
||||
parent::configure();
|
||||
|
||||
$this->addArgument(
|
||||
name: 'package-name',
|
||||
mode: InputArgument::REQUIRED,
|
||||
);
|
||||
}
|
||||
|
||||
public function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$args = $input->getOption('args');
|
||||
$workingDir = $input->getOption('working-dir');
|
||||
|
||||
$language = $input->getOption('language') ?? (new DetermineProjectLanguage(
|
||||
filesystem: $this->filesystem,
|
||||
workingDir: $workingDir,
|
||||
))->getLanguage();
|
||||
|
||||
switch ($language) {
|
||||
case ProjectLanguage::PHP->value:
|
||||
$process = Process::create(
|
||||
args: explode(separator: ' ', string: $args ?? ''),
|
||||
command: ['composer', 'require', $input->getArgument('package-name')],
|
||||
workingDir: '.',
|
||||
);
|
||||
|
||||
$process->setTimeout(null);
|
||||
$process->run();
|
||||
break;
|
||||
|
||||
case ProjectLanguage::JavaScript->value:
|
||||
if ($this->filesystem->exists($workingDir.'/yarn.lock')) {
|
||||
$command = ['yarn', 'add'];
|
||||
} elseif ($this->filesystem->exists($workingDir.'/pnpm-lock.yaml')) {
|
||||
$command = ['pnpm', 'install'];
|
||||
} else {
|
||||
$command = ['npm', 'install'];
|
||||
}
|
||||
|
||||
$process = Process::create(
|
||||
args: explode(separator: ' ', string: $args ?? ''),
|
||||
command: $command,
|
||||
workingDir: $workingDir,
|
||||
);
|
||||
$process->setTimeout(null);
|
||||
$process->run();
|
||||
break;
|
||||
}
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
Reference in a new issue