diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ce17e3..7c8bdd0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/src/Console/Command/PackageInstallCommand.php b/src/Console/Command/PackageInstallCommand.php new file mode 100644 index 0000000..56548a7 --- /dev/null +++ b/src/Console/Command/PackageInstallCommand.php @@ -0,0 +1,68 @@ +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; + } +} diff --git a/versa b/versa index 6c6d11b..878b34a 100755 --- a/versa +++ b/versa @@ -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'), ]);