Make BudgetInstall command interactive
MODIFY
app/Console/Commands/BudgetInstall.php
app/Console/Commands/BudgetInstall.php
namespace App\Console\Commands;
+use Exception;
use Illuminate\Console\Command;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
class BudgetInstall extends Command
{
- protected $signature = 'budget:install';
+ protected $signature = 'budget:install {--node-package-manager=}';
protected $description = 'Runs most of the commands needed to make Budget work';
public function __construct()
@@ -19,6 +20,7 @@ class BudgetInstall extends Command
private function executeCommand($command): string
{
$process = new Process($command);
+
$process->run();
if (!$process->isSuccessful()) {
@@ -28,34 +30,42 @@ class BudgetInstall extends Command
return $process->getOutput();
}
- private function nodePackageManagerExists(): bool
+ private function programExists(string $program): bool
{
- $output = $this->executeCommand(['which', 'npm']);
+ try {
+ $this->executeCommand(['which', $program]);
+ } catch (Exception $e) {
+ return false;
+ }
- return strpos($output, 'not found') === false;
+ return true;
}
- private function yarnExists(): bool
+ public function handle(): void
{
- $output = $this->executeCommand(['which', 'yarn']);
+ $nodePackageManager = $this->option('node-package-manager');
- return strpos($output, 'not found') === false;
+ if (!$nodePackageManager) {
+ $nodePackageManager = $this->choice('Which package manager would you like to use for Node.js?', [
+ 'npm',
+ 'yarn',
+ ]);
}
- public function handle(): void
- {
- if ($this->yarnExists()) {
- $this->executeCommand(['yarn', 'install']);
- $this->executeCommand(['yarn', 'run', 'production']);
- } elseif ($this->nodePackageManagerExists()) {
- $this->executeCommand(['npm', 'install']);
- $this->executeCommand(['npm', 'run', 'production']);
+ if (!$this->programExists($nodePackageManager)) {
+ $this->error('Could not find "' . $nodePackageManager . '", will not be able to compile front-end assets');
} else {
- $this->warn('Neither Yarn or NPM were found, please install either and retry this command');
+ $this->info('Installing Node.js packages');
+ $this->executeCommand([$nodePackageManager, 'install']);
+
+ $this->info('Compiling front-end assets');
+ $this->executeCommand([$nodePackageManager, 'run', 'production']);
}
$this->executeCommand(['cp', '.env.example', '.env']);
$this->executeCommand(['php', 'artisan', 'key:generate']);
$this->executeCommand(['php', 'artisan', 'storage:link']);
+
+ $this->info('Done!');
}
}

Make BudgetInstall command interactive