Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/LionBundle/Commands/Lion/Npm/NpmInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use LogicException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
Expand Down Expand Up @@ -44,7 +45,8 @@ protected function configure(): void
$this
->setName('npm:install')
->setDescription('Command to install dependencies with npm for a certain vite project.')
->addArgument('packages', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Package name.', []);
->addArgument('packages', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Package name.', [])
->addOption('dev', 'D', InputOption::VALUE_NONE, 'Install packages as devDependencies (--save-dev).');
}

/**
Expand All @@ -68,6 +70,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$project = $this->selectedProject();

/** @var bool $isDev */
$isDev = $input->getOption('dev');

/** @var array<int, string> $packagesList */
$packagesList = $input->getArgument('packages');

Expand All @@ -81,9 +86,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
->trim()
->get();

$devFlag = $isDev ? '--save-dev' : '';

/** @var string $command */
$command = $this->str
->of("cd resources/{$project}/ && npm install --silent {$packages}")
->of("cd resources/{$project}/ && npm install --silent {$devFlag} {$packages}")
->trim()
->get();

Expand All @@ -94,7 +101,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if ('' != $packages) {
$join = $this->arr->of(explode(' ', $packages))->join(', ');

$output->writeln($this->successOutput("\t>> RESOURCES: Dependencies have been installed: {$join}"));
$devText = $isDev ? ' (devDependencies)' : '';

$output->writeln(
$this->successOutput("\t>> RESOURCES: Dependencies have been installed{$devText}: {$join}")
);
} else {
$output->writeln($this->successOutput("\t>> RESOURCES: Dependencies have been installed"));
}
Expand Down
33 changes: 33 additions & 0 deletions tests/Commands/Lion/Npm/NpmInstallCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class NpmInstallCommandTest extends Test
private const string PROJECT_NAME = 'test-app';
private const string OUTPUT_MESSAGE_INIT_PROJECT = 'project has been generated successfully';
private const string OUTPUT_MESSAGE_INSTALL = 'Dependencies have been installed';
private const string OUTPUT_MESSAGE_DEV_INSTALL = 'Dependencies have been installed (devDependencies)';

private CommandTester $commandTesterNpmIn;
private CommandTester $commandTesterNpmI;
Expand Down Expand Up @@ -76,10 +77,42 @@ public function execute(): void
{
$this->assertSame(Command::SUCCESS, $this->commandTesterNpmIn->execute(['project' => self::PROJECT_NAME]));
$this->assertStringContainsString(self::OUTPUT_MESSAGE_INIT_PROJECT, $this->commandTesterNpmIn->getDisplay());

$this->assertSame(Command::SUCCESS, $this->commandTesterNpmI->execute(['packages' => ['']]));

$this->assertSame(Command::SUCCESS, $this->commandTesterNpmI->execute(['packages' => ['axios']]));
$this->assertStringContainsString(self::OUTPUT_MESSAGE_INSTALL, $this->commandTesterNpmI->getDisplay());

$this->assertSame(Command::SUCCESS, $this->commandTesterNpmI->execute(['packages' => ['dayjs jwt-decode']]));
$this->assertStringContainsString(self::OUTPUT_MESSAGE_INSTALL, $this->commandTesterNpmI->getDisplay());
}

#[Testing]
public function executeWithDevOption(): void
{
$this->assertSame(Command::SUCCESS, $this->commandTesterNpmIn->execute(['project' => self::PROJECT_NAME]));
$this->assertStringContainsString(self::OUTPUT_MESSAGE_INIT_PROJECT, $this->commandTesterNpmIn->getDisplay());

$this->assertSame(
Command::SUCCESS,
$this->commandTesterNpmI->execute([
'packages' => ['typescript'],
'--dev' => true,
])
);

$this->assertStringContainsString(self::OUTPUT_MESSAGE_DEV_INSTALL, $this->commandTesterNpmI->getDisplay());
$this->assertStringContainsString('typescript', $this->commandTesterNpmI->getDisplay());

$this->assertSame(
Command::SUCCESS,
$this->commandTesterNpmI->execute([
'packages' => ['@types/node', 'prettier'],
'-D' => true,
])
);

$this->assertStringContainsString(self::OUTPUT_MESSAGE_DEV_INSTALL, $this->commandTesterNpmI->getDisplay());
$this->assertStringContainsString('@types/node, prettier', $this->commandTesterNpmI->getDisplay());
}
}
Loading