Skip to content
Open
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
24 changes: 19 additions & 5 deletions src/Console/ServeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use YiiPress\Environment;
use YiiPress\RuntimePaths;
use YiiPress\Web\DevServer\DevHtmlInjector;
use YiiPress\Web\LiveReload\SiteBuildResult;
use YiiPress\Web\LiveReload\SiteBuildRunner;
use FilesystemIterator;
use HttpSoft\Message\ServerRequest;
Expand Down Expand Up @@ -412,8 +413,21 @@ function (): void {
return;
}

$this->buildLiveReloadSite();
$this->broadcastLiveReloadEvent('reload', 'changed', true);
$result = $this->buildLiveReloadSite();
if ($result === null) {
return;
}

if ($result->succeeded()) {
$this->broadcastLiveReloadEvent('reload', 'changed', true);
return;
}

$this->broadcastLiveReloadEvent(
'build-error',
json_encode(['output' => $result->output], JSON_THROW_ON_ERROR),
true,
);
});
}

Expand Down Expand Up @@ -461,15 +475,15 @@ private function closeLiveReloadWatcher(): void
$this->liveReloadClients = [];
}

private function buildLiveReloadSite(): void
private function buildLiveReloadSite(): ?SiteBuildResult
{
$now = microtime(true);
if ($now - $this->lastLiveReloadBuildTime < 1.0) {
return;
return null;
}

$this->lastLiveReloadBuildTime = $now;
$this->createLiveReloadBuildRunner()->build();
return $this->createLiveReloadBuildRunner()->build();
}

private function finishLiveReloadResponse(ConnectionInterface $connection, string $event, string $data): void
Expand Down
11 changes: 11 additions & 0 deletions src/Web/DevServer/assets/live-reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@
}
es = new EventSource("/_live-reload");
es.addEventListener("reload", function() { es.close(); location.reload(); });
es.addEventListener("build-error", function(event) {
es.close();
Comment on lines +22 to +23
try {
var payload = JSON.parse(event.data);
if (payload.output) {
console.error(payload.output);
}
} catch (error) {
console.error(event.data);
}
});
es.addEventListener("ping", function() {});
es.onerror = function() {
es.close();
Expand Down
18 changes: 18 additions & 0 deletions src/Web/LiveReload/SiteBuildResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace YiiPress\Web\LiveReload;

final readonly class SiteBuildResult
{
public function __construct(
public int $exitCode,
public string $output,
) {}

public function succeeded(): bool
{
return $this->exitCode === 0;
}
}
4 changes: 2 additions & 2 deletions src/Web/LiveReload/SiteBuildRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function __construct(
private string $outputDir,
) {}

public function build(): bool
public function build(): SiteBuildResult
{
$command = escapeshellarg($this->yiiBinary)
. ' build'
Expand All @@ -22,6 +22,6 @@ public function build(): bool

exec($command, $output, $exitCode);

return $exitCode === 0;
return new SiteBuildResult($exitCode, implode("\n", $output));
}
}
11 changes: 7 additions & 4 deletions tests/Unit/Web/LiveReload/SiteBuildRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function testBuildUsesIncrementalCommandLine(): void

$runner = new SiteBuildRunner($script, $this->tempDir . '/content', $this->tempDir . '/output');

self::assertTrue($runner->build());
self::assertTrue($runner->build()->succeeded());

$commandLine = file_get_contents($recordFile);
assertSame(
Expand All @@ -64,15 +64,18 @@ public function testBuildUsesIncrementalCommandLine(): void
assertStringNotContainsString('--no-cache', $commandLine);
}

public function testBuildReturnsFalseWhenCommandFails(): void
public function testBuildReturnsFailureResultWhenCommandFails(): void
{
$script = $this->tempDir . '/fail-yii';
file_put_contents($script, "#!/bin/sh\nexit 1\n");
file_put_contents($script, "#!/bin/sh\nprintf 'failed build\n'\nexit 1\n");
chmod($script, 0o755);

$runner = new SiteBuildRunner($script, $this->tempDir . '/content', $this->tempDir . '/output');
$result = $runner->build();

assertFalse($runner->build());
assertFalse($result->succeeded());
assertSame(1, $result->exitCode);
assertStringContainsString('failed build', $result->output);
}

private function removeDir(string $path): void
Expand Down