Skip to content

Commit

Permalink
Merge pull request #1446 from hydephp/fancy-serve-command
Browse files Browse the repository at this point in the history
Add dashboard hook to interact with fancy serve command output
  • Loading branch information
caendesilva authored Nov 11, 2023
2 parents 0efe927 + ad7ae2e commit 619655d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
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

0 comments on commit 619655d

Please sign in to comment.