|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Leaf\Console; |
| 6 | + |
| 7 | +use Symfony\Component\Console\Command\Command; |
| 8 | +use Symfony\Component\Console\Input\InputArgument; |
| 9 | +use Symfony\Component\Console\Input\InputOption; |
| 10 | +use Symfony\Component\Console\Input\InputInterface; |
| 11 | +use Symfony\Component\Console\Output\OutputInterface; |
| 12 | +use Symfony\Component\Console\Question\ConfirmationQuestion; |
| 13 | +use Symfony\Component\Process\Process; |
| 14 | + |
| 15 | +class UICommand extends Command |
| 16 | +{ |
| 17 | + protected static $defaultName = 'ui'; |
| 18 | + |
| 19 | + protected function configure() |
| 20 | + { |
| 21 | + $this |
| 22 | + ->setHelp('Open up the Leaf CLI GUI') |
| 23 | + ->setDescription('Start the Leaf CLI GUI process') |
| 24 | + ->addOption('port', 'p', InputOption::VALUE_OPTIONAL, 'Port to run app on'); |
| 25 | + } |
| 26 | + |
| 27 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 28 | + { |
| 29 | + $port = $input->getOption('port') ? (int) $input->getOption('port') : 5500; |
| 30 | + $uiDirectory = __DIR__ . '/ui/dist'; |
| 31 | + |
| 32 | + $serveCommand = "cd $uiDirectory && php -S localhost:$port"; |
| 33 | + |
| 34 | + $process = Process::fromShellCommandline( |
| 35 | + $serveCommand, |
| 36 | + null, |
| 37 | + null, |
| 38 | + null, |
| 39 | + null |
| 40 | + ); |
| 41 | + |
| 42 | + $output->writeln("<info>CLI GUI started at <href=http://localhost:$port>http://localhost:$port</></info>"); |
| 43 | + |
| 44 | + return $process->run(function ($type, $line) use ($output, $process) { |
| 45 | + if (is_string($line) && !strpos($line, 'Failed')) { |
| 46 | + $output->write($line); |
| 47 | + } else { |
| 48 | + $output->write("<error>$line</error>"); |
| 49 | + } |
| 50 | + }); |
| 51 | + } |
| 52 | +} |
0 commit comments