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:
Oliver Davies 2024-02-25 16:30:23 +00:00
parent 5d32389606
commit 82900c719c
3 changed files with 71 additions and 0 deletions

View file

@ -4,6 +4,7 @@
### Added
- Add `package-install` command to add a new package.
- Add initial JavaScript/TypeScript/Fractal support to `versa install` and `versa run`.
- Add a Symfony project type.
- Automatically use PHPUnit or ParaTest based on `require-dev` dependencies.

View 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;
}
}

2
versa
View file

@ -5,6 +5,7 @@ require __DIR__.'/vendor/autoload.php';
use App\Console\Command\BuildCommand;
use App\Console\Command\InstallCommand;
use App\Console\Command\PackageInstallCommand;
use App\Console\Command\RunCommand;
use App\Console\Command\TestCommand;
use Symfony\Component\Console\Application;
@ -16,6 +17,7 @@ $filesystem = new Filesystem();
$application->addCommands([
new BuildCommand(filesystem: $filesystem, name: 'build'),
new InstallCommand(filesystem: $filesystem, name: 'install'),
new PackageInstallCommand(filesystem: $filesystem, name: 'package-install'),
new RunCommand(filesystem: $filesystem, name: 'run'),
new TestCommand(filesystem: $filesystem, name: 'test'),
]);