From c5248bb061b732b43da93768789f1fa06e57c237 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Wed, 21 Feb 2024 23:32:59 +0000 Subject: [PATCH] Add initial JavaScript/TypeScript support Add JS/TS support to `versa install`. Run `npm install`, `yarn` or `pnpm install` based on which files are present in the project. --- src/Console/Command/AbstractCommand.php | 8 ++++++ src/Console/Command/InstallCommand.php | 35 ++++++++++++++++++++++++- src/Enum/ProjectLanguage.php | 11 ++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 src/Enum/ProjectLanguage.php diff --git a/src/Console/Command/AbstractCommand.php b/src/Console/Command/AbstractCommand.php index 44d56da..3fe0eef 100644 --- a/src/Console/Command/AbstractCommand.php +++ b/src/Console/Command/AbstractCommand.php @@ -16,6 +16,14 @@ abstract class AbstractCommand extends Command description: 'Any additonal arguments to pass to the command.', ); + $this->addOption( + name: 'language', + shortcut: 'l', + mode: InputArgument::OPTIONAL, + description: 'The project language', + suggestedValues: ['php', 'javascript'], + ); + $this->addOption( name: 'type', shortcut: 't', diff --git a/src/Console/Command/InstallCommand.php b/src/Console/Command/InstallCommand.php index 52ca37b..862c03d 100644 --- a/src/Console/Command/InstallCommand.php +++ b/src/Console/Command/InstallCommand.php @@ -2,10 +2,12 @@ namespace App\Console\Command; +use App\Enum\ProjectLanguage; use App\Process\Process; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Filesystem\Filesystem; final class InstallCommand extends AbstractCommand { @@ -14,9 +16,17 @@ final class InstallCommand extends AbstractCommand $extraArgs = $input->getOption('extra-args'); $workingDir = $input->getOption('working-dir'); + // TODO: What to do if a project contains multiple languages? + // e.g. a composer.lock file (PHP) and pnpm-lock.yaml file (JS)? + + // TODO: validate the language is an allowed value. + // TODO: Composer in Docker Compose? $process = Process::create( - command: ['composer', 'install'], + command: $this->getCommand( + language: $input->getOption('language'), + workingDir: $workingDir, + ), extraArgs: $extraArgs, workingDir: $workingDir, ); @@ -26,4 +36,27 @@ final class InstallCommand extends AbstractCommand return Command::SUCCESS; } + + /** + * @param non-empty-string $language + * @param non-empty-string $workingDir + * @return non-empty-array + */ + private function getCommand(string $language, string $workingDir): array + { + $filesystem = new Filesystem(); + + if ($language === ProjectLanguage::JavaScript->value) { + if ($filesystem->exists($workingDir.'/yarn.lock')) { + return ['yarn']; + } elseif ($filesystem->exists($workingDir.'/pnpm-lock.yaml')) { + return ['pnpm', 'install']; + } else { + return ['npm', 'install']; + } + } + + return ['composer', 'install']; + } + } diff --git a/src/Enum/ProjectLanguage.php b/src/Enum/ProjectLanguage.php new file mode 100644 index 0000000..f0df04c --- /dev/null +++ b/src/Enum/ProjectLanguage.php @@ -0,0 +1,11 @@ +