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
113 changes: 57 additions & 56 deletions src/LionBundle/Commands/Lion/Npm/NpmInitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,71 +13,72 @@
use Symfony\Component\Console\Output\OutputInterface;

/**
* Initialize a project with Vite.JS/Astro
*
* @package Lion\Bundle\Commands\Lion\Npm
* Initialize a project with Vite.JS/Astro.
*/
class NpmInitCommand extends MenuCommand
{
/**
* List of available project templates
*
* @const PROJECTS_TEMPLATE
* Defines a Vite.js project.
*/
public const string PROJECT_VITE = 'Vite.JS';

/**
* Define an Astro-type project.
*/
public const string PROJECT_ASTRO = 'Astro';

/**
* List of available project templates.
*/
private const array PROJECTS_TEMPLATE = [
'Vite.JS',
'Astro',
self::PROJECT_VITE,
self::PROJECT_ASTRO,
];

/**
* List of templates available to create electron-vite projects
*
* @const VITE_ELECTRON_TEMPLATES
* List of templates available to create vite projects.
*/
private const array VITE_ELECTRON_TEMPLATES = [
public const array TEMPLATES_VITE = [
'Vanilla',
'Vue',
'React',
'Preact',
'Lit',
'Svelte',
'Solid',
'Qwik',
'Electron',
];

/**
* List of templates available to create vite projects
*
* @const VITE_TEMPLATES
* List of templates available to create electron-vite projects.
*/
private const array VITE_TEMPLATES = [
public const array TEMPLATES_VITE_ELECTRON = [
'Vanilla',
'Vue',
'React',
'Preact',
'Lit',
'Svelte',
'Solid',
'Qwik',
'Electron',
];

/**
* List of languages available to create a project
*
* @const TYPES
* List of languages available to create a project.
*/
private const array TYPES = [
public const array TYPES = [
'js',
'ts',
];

/**
* Adds functions to execute commands, allows you to create an Application
* object to run applications with your custom commands
* object to run applications with your custom commands.
*
* @property Kernel $kernel
*/
private Kernel $kernel;

/**
* Project name
* Project name.
*
* @var string $project
*/
Expand All @@ -92,29 +93,29 @@ public function setKernel(Kernel $kernel): NpmInitCommand
}

/**
* Configures the current command
* Configures the current command.
*
* @return void
*/
protected function configure(): void
{
$this
->setName('npm:init')
->setDescription('Command to create Javascript projects with Vite.JS/Astro')
->addArgument('project', InputArgument::OPTIONAL, "Project's name", 'app');
->setDescription('Command to create Javascript projects with Vite.JS/Astro.')
->addArgument('project', InputArgument::OPTIONAL, "Project's name.", 'app');
}

/**
* Initializes the command after the input has been bound and before the input
* is validated
* Initializes the command after the input has been bound and before the input is
* validated.
*
* This is mainly useful when a lot of commands extends one main command where
* some things need to be initialized based on the input arguments and options
* This is mainly useful when a lot of commands extends one main command where some
* things need to be initialized based on the input arguments and options.
*
* @param InputInterface $input InputInterface is the interface implemented by
* all input classes
* all input classes.
* @param OutputInterface $output OutputInterface is the interface implemented
* by all Output classes
* by all Output classes.
*
* @return void
*/
Expand All @@ -124,20 +125,20 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
}

/**
* Executes the current command
* Executes the current command.
*
* This method is not abstract because you can use this class as a concrete
* class. In this case, instead of defining the execute() method, you set the
* code to execute by passing a Closure to the setCode() method
* code to execute by passing a Closure to the setCode() method.
*
* @param InputInterface $input InputInterface is the interface implemented by
* all input classes
* all input classes.
* @param OutputInterface $output OutputInterface is the interface implemented
* by all Output classes
* by all Output classes.
*
* @return int
*
* @throws LogicException When this abstract method is not implemented
* @throws LogicException When this abstract method is not implemented.
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
Expand All @@ -156,7 +157,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->project = $project;

if (isSuccess($this->store->exist("resources/{$this->project}/"))) {
$output->writeln($this->warningOutput("\t>> RESOURCES: a resource with this name already exists"));
$output->writeln($this->warningOutput("\t>> RESOURCES: A resource with this name already exists."));

return parent::FAILURE;
}
Expand All @@ -168,34 +169,34 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$output->writeln($this->warningOutput("\t>> RESOURCES: {$this->project}"));

$output->writeln($this->successOutput(
"\t>> RESOURCES: 'resources/{$this->project}/' project has been generated successfully"
"\t>> RESOURCES: 'resources/{$this->project}/' project has been generated successfully."
));

return parent::SUCCESS;
}

/**
* Initializes the projects to be created
* Initializes the projects to be created.
*
* @return void
*/
private function initProjects(): void
{
$projectType = $this->selectedTemplate($this->input, $this->output, self::PROJECTS_TEMPLATE, 'Vite.JS', 0);
$projectType = $this->selectedTemplate(self::PROJECTS_TEMPLATE, self::PROJECT_VITE, 0);

if ('Astro' === $projectType) {
$this->createAstroProject();
} else {
/** @var string $template */
$template = $this->str
->of($this->selectedTemplate($this->input, $this->output, self::VITE_TEMPLATES))
->of($this->selectedTemplate(self::TEMPLATES_VITE))
->lower()
->get();

if ('electron' === $template) {
/** @var string $electronTemplate */
$electronTemplate = $this->str
->of($this->selectedTemplate($this->input, $this->output, self::VITE_ELECTRON_TEMPLATES))
->of($this->selectedTemplate(self::TEMPLATES_VITE_ELECTRON))
->lower()
->get();

Expand All @@ -207,49 +208,49 @@ private function initProjects(): void
}

/**
* Starting an Astro Project
* Starting an Astro Project.
*
* @return void
*/
private function createAstroProject(): void
{
$command = "cd resources/ && echo | npm create astro@latest {$this->project} ";
$command = "cd resources/ && echo | npm create --silent astro@latest {$this->project} ";

$command .= "-- --no-install --no-git --yes --skip-houston";

$this->kernel->execute($command);
}

/**
* Create a vite project and install its dependencies
* Create a vite project and install its dependencies.
*
* @param string $template Project template
* @param string $template Project template.
*
* @return void
*/
private function createViteProject(string $template): void
{
$type = $this->selectedTypes($this->input, $this->output, self::TYPES);
$type = $this->selectedTypes(self::TYPES);

$command = "cd resources/ && echo | npm create vite@latest {$this->project}";
$command = "cd resources/ && echo | npm create --silent vite@latest {$this->project}";

$command .= " -- --template {$template}" . ('js' === $type ? '' : '-ts');

$this->kernel->execute($command);
}

/**
* Create an electron-vite project and install its dependencies
* Create an electron-vite project and install its dependencies.
*
* @param string $template Project template
* @param string $template Project template.
*
* @return void
*/
private function createElectronViteProject(string $template): void
{
$type = $this->selectedTypes($this->input, $this->output, self::TYPES);
$type = $this->selectedTypes(self::TYPES);

$command = "cd resources/ && echo | npm create @quick-start/electron";
$command = "cd resources/ && echo | npm create --silent @quick-start/electron";

$command .= " {$this->project} -- --template {$template}" . ('js' === $type ? '' : '-ts') . ' --skip';

Expand Down
40 changes: 19 additions & 21 deletions src/LionBundle/Commands/Lion/Npm/NpmInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
use Symfony\Component\Console\Output\OutputInterface;

/**
* Install the Vite.JS project dependencies
*
* @package Lion\Bundle\Commands\Lion\Npm
* Install the project dependencies using NPM.
*/
class NpmInstallCommand extends MenuCommand
{
/**
* [Kernel class object]
* Adds functions to execute commands, allows you to create an Application
* object to run applications with your custom commands.
*
* @property Kernel $kernel
*/
Expand All @@ -36,39 +35,38 @@ public function setKernel(Kernel $kernel): NpmInstallCommand
}

/**
* Configures the current command
* Configures the current command.
*
* @return void
*/
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', []);
->setDescription('Command to install dependencies with npm for a certain vite project.')
->addArgument('packages', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Package name.', []);
}

/**
* Executes the current command
*
* This method is not abstract because you can use this class
* as a concrete class. In this case, instead of defining the
* execute() method, you set the code to execute by passing
* a Closure to the setCode() method
* This method is not abstract because you can use this class as a concrete
* class. In this case, instead of defining the execute() method, you set the
* code to execute by passing a Closure to the setCode() method.
*
* @param InputInterface $input [InputInterface is the interface implemented
* by all input classes]
* @param OutputInterface $output [OutputInterface is the interface
* implemented by all Output classes]
* @param InputInterface $input InputInterface is the interface implemented by
* all input classes
* @param OutputInterface $output OutputInterface is the interface implemented
* by all Output classes.
*
* @return int
*
* @throws Exception
* @throws LogicException [When this abstract method is not implemented]
* @throws Exception If there are no projects available.
* @throws LogicException When this abstract method is not implemented.
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$project = $this->selectedProject($input, $output);
$project = $this->selectedProject();

/** @var array<int, string> $packagesList */
$packagesList = $input->getArgument('packages');
Expand All @@ -85,7 +83,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

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

Expand All @@ -96,9 +94,9 @@ 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}"));
$output->writeln($this->successOutput("\t>> RESOURCES: Dependencies have been installed: {$join}"));
} else {
$output->writeln($this->successOutput("\t>> RESOURCES: dependencies have been installed"));
$output->writeln($this->successOutput("\t>> RESOURCES: Dependencies have been installed"));
}

return parent::SUCCESS;
Expand Down
Loading
Loading