-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathDevserverCommand.php
More file actions
147 lines (130 loc) · 4.49 KB
/
DevserverCommand.php
File metadata and controls
147 lines (130 loc) · 4.49 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
declare(strict_types=1);
namespace App\Command;
use Cake\Command\Command;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Cake\Console\Exception\StopException;
use Symfony\Component\Process\Process;
/**
* Devserver command.
*/
class DevserverCommand extends Command
{
/**
* The name of this command.
*/
protected string $name = 'devserver';
/**
* Get the default command name.
*
* @return string
*/
public static function defaultName(): string
{
return 'devserver';
}
/**
* Get the command description.
*
* @return string
*/
public static function getDescription(): string
{
return 'Run the `bin/cake server` and `npm run dev` together';
}
/**
* Hook method for defining this command's option parser.
*
* @see https://book.cakephp.org/5/en/console-commands/commands.html#defining-arguments-and-options
* @param \Cake\Console\ConsoleOptionParser $parser The parser to be defined
* @return \Cake\Console\ConsoleOptionParser The built parser.
*/
protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
{
return parent::buildOptionParser($parser)
->setDescription(static::getDescription());
}
/**
* Implement this method with your command's logic.
*
* @param \Cake\Console\Arguments $args The command arguments.
* @param \Cake\Console\ConsoleIo $io The console io
* @return int|null The exit code or null for success
*/
public function execute(Arguments $args, ConsoleIo $io): ?int
{
$cwd = getcwd();
if ($cwd === false) {
throw new StopException('Cannot read CWD');
}
// Input is a 'server'.
// Servers have a name, command to run, and environment vars.
$io->verbose('Starting bin/cake/server');
$cakeCommand = ['bin/cake', 'server'];
$npmCommand = ['npm', 'run', 'dev'];
$cakeProcess = new Process($cakeCommand, $cwd, ['CAKE_DEVSERVER' => '1', 'PATH' => getenv('PATH')]);
$npmProcess = new Process($npmCommand, $cwd);
$cakeProcess->start();
$io->verbose('Cake server started');
$npmProcess->start();
$io->verbose('Starting npm run dev');
$servers = [
[
'name' => 'cake',
'process' => $cakeProcess,
],
[
'name' => 'npm',
'process' => $npmProcess,
],
];
$poll = true;
while ($poll) {
foreach ($servers as $server) {
$process = $server['process'];
$name = $server['name'];
if (!$process->isRunning()) {
$poll = false;
$exitCode = $process->getExitCode();
$io->error(sprintf('%s has died with code %s.', $name, $exitCode));
$errorOutput = trim($process->getErrorOutput());
if ($errorOutput !== '') {
$io->error(sprintf('%s | %s', $name, $errorOutput));
}
break;
}
// Read any incremental output
$output = $process->getIncrementalOutput();
if (!empty($output)) {
foreach (explode("\n", trim($output)) as $line) {
if ($line !== '') {
$io->info(sprintf('%s | %s', $name, $line));
}
}
}
$error = $process->getIncrementalErrorOutput();
if (!empty($error)) {
foreach (explode("\n", trim($error)) as $line) {
if ($line !== '') {
$io->comment(sprintf('%s | %s', $name, $line));
}
}
}
}
usleep(100); // Small delay to prevent high CPU usage
}
$io->verbose('Start shutdown');
foreach ($servers as $server) {
$process = $server['process'];
if ($process->isRunning()) {
$process->stop(1); // graceful timeout of 1 second
}
}
$io->out('Shutdown complete');
// We exit error as, normally this process gets killed with ctrl-c, and we
// should only get here if a server died.
return static::CODE_ERROR;
}
}