-
-
Notifications
You must be signed in to change notification settings - Fork 220
/
StartSsr.php
90 lines (73 loc) · 2.66 KB
/
StartSsr.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
namespace Inertia\Commands;
use Inertia\Ssr\SsrException;
use Illuminate\Console\Command;
use Inertia\Ssr\BundleDetector;
use Symfony\Component\Process\Process;
use Symfony\Component\Console\Attribute\AsCommand;
#[AsCommand(name: 'inertia:start-ssr')]
class StartSsr extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $signature = 'inertia:start-ssr {--runtime=node : The runtime to use (`node` or `bun`)}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Start the Inertia SSR server';
/**
* Start the SSR server via a Node process.
*/
public function handle(): int
{
if (! config('inertia.ssr.enabled', true)) {
$this->error('Inertia SSR is not enabled. Enable it via the `inertia.ssr.enabled` config option.');
return self::FAILURE;
}
$bundle = (new BundleDetector())->detect();
$configuredBundle = config('inertia.ssr.bundle');
if ($bundle === null) {
$this->error(
$configuredBundle
? 'Inertia SSR bundle not found at the configured path: "'.$configuredBundle.'"'
: 'Inertia SSR bundle not found. Set the correct Inertia SSR bundle path in your `inertia.ssr.bundle` config.'
);
return self::FAILURE;
} elseif ($configuredBundle && $bundle !== $configuredBundle) {
$this->warn('Inertia SSR bundle not found at the configured path: "'.$configuredBundle.'"');
$this->warn('Using a default bundle instead: "'.$bundle.'"');
}
$runtime = $this->option('runtime');
if (! in_array($runtime, ['node', 'bun'])) {
$this->error('Unsupported runtime: "'.$runtime.'". Supported runtimes are `node` and `bun`.');
return self::INVALID;
}
$this->callSilently('inertia:stop-ssr');
$process = new Process([$runtime, $bundle]);
$process->setTimeout(null);
$process->start();
if (extension_loaded('pcntl')) {
$stop = function () use ($process) {
$process->stop();
};
pcntl_async_signals(true);
pcntl_signal(SIGINT, $stop);
pcntl_signal(SIGQUIT, $stop);
pcntl_signal(SIGTERM, $stop);
}
foreach ($process as $type => $data) {
if ($process::OUT === $type) {
$this->info(trim($data));
} else {
$this->error(trim($data));
report(new SsrException($data));
}
}
return self::SUCCESS;
}
}