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

Add dashboard hook to interact with fancy serve command output #1446

Merged
merged 2 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
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
25 changes: 22 additions & 3 deletions packages/realtime-compiler/src/ConsoleOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use function sprintf;
use function str_repeat;
use function str_replace;
use function str_contains;
use function Termwind\render;

class ConsoleOutput
Expand Down Expand Up @@ -60,6 +61,13 @@ public static function getFormatter(bool $verbose): Closure
};
}

/** @experimental */
public static function printMessage(string $message, string $context): void
{
$consoleOutput = new \Symfony\Component\Console\Output\ConsoleOutput();
$consoleOutput->writeln(sprintf('%s [%s]', $message, $context));
}

public function __construct(bool $verbose = false)
{
$this->verbose = $verbose;
Expand Down Expand Up @@ -87,6 +95,13 @@ protected function formatLineForOutput(string $line): ?string
if (str_ends_with(trim($line), 'Accepted') || str_ends_with(trim($line), 'Closing')) {
return $this->verbose ? $this->formatRequestStatusLine($line) : null;
}
if (str_contains($line, '[dashboard@')) {
$message = trim(Str::before($line, '[dashboard@'));
$context = trim(trim(Str::after($line, $message)), '[]');
$success = str_contains($message, 'Created') || str_contains($message, 'Updated');

return $this->formatLine($message, Carbon::now(), $success ? 'green-500' : 'blue-500', $context);
}

return $this->formatLine($line, Carbon::now());
}
Expand Down Expand Up @@ -118,18 +133,22 @@ protected function formatRequestStatusLine(string $line): string
return $this->formatLine(sprintf('%s %s', $address, $status), $this->parseDate($line));
}

protected function formatLine(string $message, Carbon $date, string $iconColor = 'blue-500'): string
protected static function formatLine(string $message, Carbon $date, string $iconColor = 'blue-500', string $context = ''): string
{
if ($context) {
$context = "$context ";
}

return sprintf(<<<'HTML'
<div class="flex w-full justify-between">
<span>
<span class="text-%s">i</span>
%s
</span>
<span class="text-gray">%s</span>
<span class="text-gray">%s%s</span>
</div>
HTML,
$iconColor, $message, $date->format('Y-m-d H:i:s')
$iconColor, $message, $context, $date->format('Y-m-d H:i:s')
);
}

Expand Down
3 changes: 3 additions & 0 deletions packages/realtime-compiler/src/Http/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Desilva\Microserve\JsonResponse;
use Hyde\Support\Filesystem\MediaFile;
use Illuminate\Support\Facades\Process;
use Hyde\RealtimeCompiler\ConsoleOutput;
use Hyde\Framework\Actions\StaticPageBuilder;
use Hyde\Framework\Actions\AnonymousViewCompiler;
use Desilva\Microserve\Request;
Expand Down Expand Up @@ -353,6 +354,8 @@ protected function createPage(): void
$this->abort($exception->getCode(), $exception->getMessage());
}

ConsoleOutput::printMessage("Created file '$path'", 'dashboard@createPage');

$this->flash('justCreatedPage', RouteKey::fromPage($pageClass, $pageClass::pathToIdentifier($path))->get());
$this->setJsonResponse(201, "Created file '$path'!");
}
Expand Down
Loading