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.
This commit is contained in:
Oliver Davies 2024-02-21 23:32:59 +00:00
parent 72dea5070b
commit c5248bb061
3 changed files with 53 additions and 1 deletions

View file

@ -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',

View file

@ -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<int, non-empty-string>
*/
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'];
}
}

View file

@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace App\Enum;
enum ProjectLanguage: string
{
case JavaScript = 'javascript';
case PHP = 'php';
}