Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: better serve command feedback #1879

Merged
merged 6 commits into from
Jan 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions src/Command/Serve.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,16 @@
['BOX_REQUIREMENT_CHECKER' => '0'] // prevents double check (build then serve)
);

if ($this->getBuilder()->isDebug()) {
$output->writeln(sprintf('<comment>Process: %s</comment>', implode(' ', $buildProcessArguments)));
}

$buildProcess->setTty(Process::isTtySupported());
$buildProcess->setPty(Process::isPtySupported());
$buildProcess->setTimeout(3600 * 2); // timeout = 2 minutes

$processOutputCallback = function ($type, $data) use ($output) {
$output->write($data, false, OutputInterface::OUTPUT_RAW);
$processOutputCallback = function ($type, $buffer) use ($output) {

Check notice on line 140 in src/Command/Serve.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Command/Serve.php#L140

Avoid excessively long variable names like $processOutputCallback. Keep variable name length under 20.
$output->write($buffer, false, OutputInterface::OUTPUT_RAW);
};

// (re)builds before serve
$output->writeln(sprintf('<comment>Build process: %s</comment>', implode(' ', $buildProcessArguments)), OutputInterface::VERBOSITY_DEBUG);
$buildProcess->run($processOutputCallback);
if ($buildProcess->isSuccessful()) {
Util\File::getFS()->dumpFile(Util::joinFile($this->getPath(), self::TMP_DIR, 'changes.flag'), time());
Expand Down Expand Up @@ -176,15 +173,24 @@
pcntl_signal(SIGINT, [$this, 'tearDownServer']);
pcntl_signal(SIGTERM, [$this, 'tearDownServer']);
}
$output->writeln(
sprintf('Starting server (<href=http://%s:%d>%s:%d</>)...', $host, $port, $host, $port)
);
$process->start();
$output->writeln(sprintf('<comment>Server process: %s</comment>', $command), OutputInterface::VERBOSITY_DEBUG);
$output->writeln(sprintf('Starting server (<href=http://%s:%d>%s:%d</>)...', $host, $port, $host, $port));
$process->start(function ($type, $buffer) {
if ($type === Process::ERR) {
error_log($buffer, 3, Util::joinFile($this->getPath(), self::TMP_DIR, 'errors.log'));
}
});
if ($open) {
$output->writeln('Opening web browser...');
Util\Plateform::openBrowser(sprintf('http://%s:%s', $host, $port));
}
while ($process->isRunning()) {
sleep(1); // wait for server is ready
if (!fsockopen($host, (int) $port)) {
$output->writeln('<info>Server is not ready.</info>');

return 1;
}
$watcher = $resourceWatcher->findChanges();
if ($watcher->hasChanges()) {
// prints deleted/new/updated files in debug mode
Expand Down Expand Up @@ -217,6 +223,9 @@
$output->writeln('<info>Server is runnning...</info>');
}
}
if ($process->getExitCode() > 0) {
$output->writeln(sprintf('<comment>%s</comment>', trim($process->getErrorOutput())));
}
} catch (ProcessFailedException $e) {
$this->tearDownServer();

Expand Down Expand Up @@ -276,7 +285,7 @@
public function tearDownServer(): void
{
$this->output->writeln('');
$this->output->writeln('<comment>Server stopped.</comment>');
$this->output->writeln('<info>Server stopped.</info>');

try {
Util\File::getFS()->remove(Util::joinFile($this->getPath(), self::TMP_DIR));
Expand Down