Skip to content

Commit 32a983b

Browse files
committed
chore: migrate to sprout
1 parent 0256857 commit 32a983b

File tree

3 files changed

+50
-192
lines changed

3 files changed

+50
-192
lines changed

src/ServeCommand.php

Lines changed: 22 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -4,153 +4,45 @@
44

55
namespace Leaf\Console;
66

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;
7+
use Leaf\Sprout\Command;
148

159
class ServeCommand extends Command
1610
{
17-
protected static $defaultName = 'serve';
11+
protected $signature = 'serve
12+
{filename? : The PHP script to run}
13+
{--p|port=5500 : Port to run app on}
14+
{--nc|no-concurrent : Run PHP server without Vite server}';
1815

19-
protected function configure()
20-
{
21-
$this
22-
->setHelp('Start the leaf app server')
23-
->setDescription('Run your Leaf app')
24-
->addArgument('filename', InputArgument::OPTIONAL, 'The PHP script to run')
25-
->addOption('port', 'p', InputOption::VALUE_OPTIONAL, 'Port to run app on')
26-
->addOption('watch', 'w', InputOption::VALUE_NONE, 'Run your leaf app with hot reloading [experimental]')
27-
// ->addOption('path', 't', InputOption::VALUE_OPTIONAL, 'Path to your app', getcwd() . '/public')
28-
// ->addOption('host', 's', InputOption::VALUE_OPTIONAL, 'Your application host', 'localhost')
29-
->addOption('no-concurrent', 'nc', InputOption::VALUE_OPTIONAL, 'Run PHP server without Vite server', false);
30-
}
16+
protected $description = 'Run a server to serve your Leaf app';
3117

32-
protected function execute(InputInterface $input, OutputInterface $output): int
18+
protected function handle(): int
3319
{
3420
if ($this->isMVCApp()) {
35-
return (int) Utils\Core::run("php leaf serve --ansi", $output);
36-
}
37-
38-
$vendorPath = getcwd() . '/vendor';
39-
$composerJsonPath = getcwd() . '/composer.json';
40-
41-
if (!is_dir($vendorPath) && file_exists($composerJsonPath)) {
42-
$output->writeln('<info>Installing dependencies...</info>');
43-
$leaf = Utils\Core::findLeaf();
44-
$installProcess = Process::fromShellCommandline("$leaf install");
45-
46-
$installProcess->run(function ($type, $line) use ($output) {
47-
$output->write($line);
48-
});
49-
50-
if (!$installProcess->isSuccessful()) {
51-
$output->writeln('<error>Failed to install dependencies</error>');
52-
}
21+
return (int) sprout()->run("php leaf serve --ansi");
5322
}
5423

55-
if ($input->getArgument('filename')) {
56-
$input->setOption('watch', true);
24+
if (!sprout()->composer()->json()) {
25+
$this->writeln('<error>No composer.json found in the current directory.</error>');
26+
return 1;
5727
}
5828

59-
if ($input->getOption('watch')) {
60-
$leafWatcherInstalled = Utils\Core::commandExists('leaf-watcher');
61-
$node = Utils\Core::findNodeJS();
62-
$npm = Utils\Core::findNpm();
63-
$watcher = Utils\Core::findWatcher();
64-
$leaf = Utils\Core::findLeaf();
29+
if (!sprout()->composer()->hasDependencies()) {
30+
$this->writeln('<info>Installing dependencies...</info>');
6531

66-
if (!$node || !$npm) {
67-
$output->writeln('<error>Can\'t find NodeJS on the system. Watching will be disabled.</error>');
68-
69-
return $this->startServer($input, $output);
32+
if (!sprout()->composer()->install()->isSuccessful()) {
33+
$this->writeln('<error>❌ Failed to install dependencies.</error>');
34+
return 1;
7035
}
71-
72-
if (!$leafWatcherInstalled) {
73-
$installWatcher = $this->askToInstallWatcher($input, $output);
74-
75-
if (!$installWatcher && !file_exists($watcher)) {
76-
$output->writeln('<error>Watcher install cancelled. Watching will be disabled.</error>');
77-
78-
return $this->startServer($input, $output);
79-
}
80-
81-
$output->writeln('<info>Installing leaf watcher...</info>');
82-
$installProcess = Process::fromShellCommandline("$npm install -g @leafphp/watcher");
83-
84-
$installProcess->run(function ($type, $line) use ($output) {
85-
$output->write($line);
86-
});
87-
88-
if (!$installProcess->isSuccessful()) {
89-
$output->writeln('<error>Failed to install leaf watcher. Watching will be disabled.</error>');
90-
91-
return $this->startServer($input, $output);
92-
}
93-
}
94-
95-
$port = $input->getOption('port') ? (int) $input->getOption('port') : 5500;
96-
$process = Process::fromShellCommandline("$watcher --exec $leaf serve --port $port", null, null, null, null);
97-
98-
if ($input->getArgument('filename')) {
99-
$filename = $input->getArgument('filename');
100-
$process = Process::fromShellCommandline("$watcher --exec " . PHP_BINARY . " $filename", null, null, null, null);
101-
}
102-
103-
return $process->run(function ($type, $line) use ($output) {
104-
$output->write($line);
105-
});
106-
}
107-
108-
return $this->startServer($input, $output);
109-
}
110-
111-
protected function askToInstallWatcher($input, $output)
112-
{
113-
$helper = $this->getHelper('question');
114-
$question = new ConfirmationQuestion('<info>* Leaf Watcher is required to enable monitoring. Install package?</info> ', true);
115-
116-
return $helper->ask($input, $output, $question);
117-
}
118-
119-
protected function startServer(InputInterface $input, OutputInterface $output): int
120-
{
121-
$useConcurrent = true;
122-
123-
$noConcurrent = $input->getOption('no-concurrent');
124-
$port = (int) ($input->getOption('port') ?? 5500);
125-
126-
if ($noConcurrent || !file_exists(getcwd() . '/vite.config.js')) {
127-
$useConcurrent = false;
12836
}
12937

130-
$serveCommand = !$useConcurrent ? "php -S localhost:$port" : "npx concurrently -c \"#3eaf7c,#bd34fe\" \"php -S localhost:$port\" \"npm run dev\" --names=server,vite --colors";
131-
38+
$port = $this->option('port');
13239
$isDockerProject = file_exists(getcwd() . '/docker-compose.yml');
133-
$process = Process::fromShellCommandline(
134-
$isDockerProject ? 'docker compose up' : $serveCommand,
135-
null,
136-
null,
137-
null,
138-
null
139-
);
140-
141-
$output->writeln(
142-
$isDockerProject ?
143-
'<info>Serving Leaf application using Docker Compose...</info>' :
144-
"<info>Starting Leaf development server on <href=http://localhost:$port>http://localhost:$port</></info>"
145-
);
40+
$useConcurrent = !$this->option('no-concurrent') && file_exists(getcwd() . '/vite.config.js');
41+
$serveCommand = !$useConcurrent ? "php -S localhost:$port" : "npx concurrently -c \"#3eaf7c,#bd34fe\" \"php -S localhost:$port\" \"npm run dev\" --names=server,vite --colors";
14642

147-
return $process->run(function ($type, $line) use ($output, $process) {
148-
if (is_string($line) && !strpos($line, 'Failed')) {
149-
$output->write($line);
150-
} else {
151-
$output->write("<error>$line</error>");
152-
}
153-
});
43+
return sprout()->process(
44+
$isDockerProject ? 'docker compose up' : $serveCommand
45+
)->run();
15446
}
15547

15648
protected function isMVCApp()

src/UICommand.php

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,24 @@
44

55
namespace Leaf\Console;
66

7-
use Symfony\Component\Console\Command\Command;
8-
use Symfony\Component\Console\Input\InputOption;
9-
use Symfony\Component\Console\Input\InputInterface;
10-
use Symfony\Component\Console\Output\OutputInterface;
11-
use Symfony\Component\Process\Process;
7+
use Leaf\Sprout\Command;
128

139
class UICommand extends Command
1410
{
15-
protected static $defaultName = 'ui';
11+
protected $signature = 'ui {--port=3001}';
1612

17-
protected function configure()
18-
{
19-
$this
20-
->setAliases(['gui'])
21-
->setHelp('Open up the Leaf CLI GUI')
22-
->setDescription('Start the Leaf CLI GUI process')
23-
->addOption('port', 'p', InputOption::VALUE_OPTIONAL, 'Port to run app on', 3001);
24-
}
13+
protected $description = 'Open up the Leaf CLI GUI';
2514

26-
protected function execute(InputInterface $input, OutputInterface $output): int
15+
protected function execute(): int
2716
{
28-
$port = (int) $input->getOption('port');
29-
17+
$port = $this->option('port');
3018
$uiDirectory = __DIR__ . '/ui/dist';
3119
$serveCommand = "cd $uiDirectory && php -S localhost:$port";
3220

33-
$process = Process::fromShellCommandline(
34-
$serveCommand,
35-
null,
36-
null,
37-
null,
38-
null
39-
);
40-
41-
$output->writeln("<info>CLI GUI started at <href=http://localhost:$port>http://localhost:$port</></info>");
42-
43-
return $process->run(function ($type, $line) use ($output, $process) {
44-
if (is_string($line) && !strpos($line, 'Failed')) {
45-
$output->write($line);
46-
} else {
47-
$output->write("<error>$line</error>");
48-
}
49-
});
21+
$process = sprout()->process($serveCommand);
22+
23+
$this->writeln("<info>CLI GUI started at <href=http://localhost:$port>http://localhost:$port</></info>");
24+
25+
return $process->run();
5026
}
5127
}

src/ViewBuildCommand.php

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,35 @@
44

55
namespace Leaf\Console;
66

7-
use Symfony\Component\Console\Command\Command;
8-
use Symfony\Component\Console\Input\InputOption;
9-
use Symfony\Component\Console\Input\InputInterface;
10-
use Symfony\Component\Console\Output\OutputInterface;
7+
use Leaf\Sprout\Command;
118

129
class ViewBuildCommand extends Command
1310
{
14-
protected static $defaultName = 'view:build';
11+
protected $signature = 'view:build {--pm=npm}';
1512

16-
protected function configure()
17-
{
18-
$this
19-
->setHelp('Run your frontend dev command')
20-
->setDescription('Run your frontend dev server')
21-
->addOption('pm', 'pm', InputOption::VALUE_OPTIONAL, 'Package manager to use', 'npm');
22-
}
13+
protected $description = 'Build your frontend assets';
2314

24-
protected function execute(InputInterface $input, OutputInterface $output): int
15+
protected function execute(): int
2516
{
26-
$directory = getcwd();
27-
$npm = Utils\Core::findNpm($input->getOption('pm'));
28-
29-
if (!is_dir("$directory/node_modules")) {
30-
$output->writeln('<info>Installing dependencies...</info>');
31-
$success = Utils\Core::run("$npm install", $output);
32-
33-
if (!$success) {
34-
$output->writeln('<error>❌ Failed to install dependencies.</error>');
17+
if (!sprout()->npm($this->option('pm'))->json()) {
18+
$this->writeln('<error>No package.json found in the current directory.</error>');
19+
return 1;
20+
}
3521

22+
if (!sprout()->npm($this->option('pm'))->hasDependencies()) {
23+
$this->writeln('<info>Installing dependencies...</info>');
24+
25+
if (!sprout()->npm($this->option('pm'))->install()->isSuccessful()) {
26+
$this->writeln('<error>❌ Failed to install dependencies.</error>');
3627
return 1;
3728
}
3829
}
3930

40-
$success = Utils\Core::run("$npm run build", $output);
41-
42-
if (!$success) {
43-
return 1;
44-
}
31+
$this->writeln('<info>Building assets...</info>');
4532

46-
return 0;
33+
return (int) sprout()
34+
->npm($this->option('pm'))
35+
->runScript('build')
36+
->isSuccessful();
4737
}
4838
}

0 commit comments

Comments
 (0)