From b048fae428615c8329eba5dccd2dd3b557fd7865 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Dec 2024 17:59:14 +0100 Subject: [PATCH] Use only hotfiles instead of internal environment variable See https://github.com/hydephp/develop/pull/2060 --- .../src/Console/Commands/ServeCommand.php | 4 +++- packages/framework/src/Facades/Vite.php | 8 +------ .../Feature/Commands/ServeCommandTest.php | 12 ++++++++-- .../tests/Unit/Facades/ViteFacadeTest.php | 22 ------------------- .../Unit/ServeCommandOptionsUnitTest.php | 12 ---------- .../realtime-compiler/src/ConsoleOutput.php | 3 ++- 6 files changed, 16 insertions(+), 45 deletions(-) diff --git a/packages/framework/src/Console/Commands/ServeCommand.php b/packages/framework/src/Console/Commands/ServeCommand.php index c998aba44ce..b2b85548d05 100644 --- a/packages/framework/src/Console/Commands/ServeCommand.php +++ b/packages/framework/src/Console/Commands/ServeCommand.php @@ -5,6 +5,7 @@ namespace Hyde\Console\Commands; use Closure; +use Hyde\Facades\Filesystem; use Hyde\Hyde; use Hyde\Facades\Config; use Illuminate\Contracts\Process\InvokedProcess; @@ -112,7 +113,6 @@ protected function getEnvironmentVariables(): array 'HYDE_SERVER_DASHBOARD' => $this->parseEnvironmentOption('dashboard'), 'HYDE_PRETTY_URLS' => $this->parseEnvironmentOption('pretty-urls'), 'HYDE_PLAY_CDN' => $this->parseEnvironmentOption('play-cdn'), - 'HYDE_SERVER_VITE' => $this->option('vite') ? 'enabled' : null, ]); } @@ -204,6 +204,8 @@ protected function runViteProcess(): void ); } + Filesystem::touch('app/storage/framework/cache/vite.hot'); + $this->vite = Process::forever()->start('npm run dev'); } diff --git a/packages/framework/src/Facades/Vite.php b/packages/framework/src/Facades/Vite.php index a3db7dd3608..f69c8f0b9de 100644 --- a/packages/framework/src/Facades/Vite.php +++ b/packages/framework/src/Facades/Vite.php @@ -14,7 +14,7 @@ class Vite { public static function running(): bool { - return static::checkIfViteWasEnabledViaTheServeCommand() || Filesystem::exists('app/storage/framework/cache/vite.hot'); + return Filesystem::exists('app/storage/framework/cache/vite.hot'); } public static function asset(string $path): HtmlString @@ -49,12 +49,6 @@ protected static function formatAssetPath(string $path): string throw new InvalidArgumentException("Unsupported asset type for path: '$path'"); } - protected static function checkIfViteWasEnabledViaTheServeCommand(): bool - { - // TODO: Do we actually need this? Hotfile should be enough. - return env('HYDE_SERVER_VITE') === 'enabled'; - } - protected static function isCssPath(string $path): bool { return preg_match('/\.(css|less|sass|scss|styl|stylus|pcss|postcss)$/', $path) === 1; diff --git a/packages/framework/tests/Feature/Commands/ServeCommandTest.php b/packages/framework/tests/Feature/Commands/ServeCommandTest.php index 1f26744c908..3b34e3f4ce9 100644 --- a/packages/framework/tests/Feature/Commands/ServeCommandTest.php +++ b/packages/framework/tests/Feature/Commands/ServeCommandTest.php @@ -182,6 +182,8 @@ public function testWithFancyOutput() public function testHydeServeCommandWithViteOption() { + $this->cleanUpWhenDone('app/storage/framework/cache/vite.hot'); + $mockViteProcess = mock(InvokedProcess::class); $mockViteProcess->shouldReceive('running') ->once() @@ -202,7 +204,7 @@ public function testHydeServeCommandWithViteOption() Process::shouldReceive('env') ->once() - ->with(['HYDE_SERVER_REQUEST_OUTPUT' => false, 'HYDE_SERVER_VITE' => 'enabled']) + ->with(['HYDE_SERVER_REQUEST_OUTPUT' => false]) ->andReturnSelf(); Process::shouldReceive('start') @@ -224,10 +226,14 @@ public function testHydeServeCommandWithViteOption() ->expectsOutput('server output') ->expectsOutput('vite latest output') ->assertExitCode(0); + + $this->assertFileExists('app/storage/framework/cache/vite.hot'); } public function testHydeServeCommandWithViteOptionButViteNotRunning() { + $this->cleanUpWhenDone('app/storage/framework/cache/vite.hot'); + $mockViteProcess = mock(InvokedProcess::class); $mockViteProcess->shouldReceive('running') ->once() @@ -245,7 +251,7 @@ public function testHydeServeCommandWithViteOptionButViteNotRunning() Process::shouldReceive('env') ->once() - ->with(['HYDE_SERVER_REQUEST_OUTPUT' => false, 'HYDE_SERVER_VITE' => 'enabled']) + ->with(['HYDE_SERVER_REQUEST_OUTPUT' => false]) ->andReturnSelf(); Process::shouldReceive('start') @@ -263,6 +269,8 @@ public function testHydeServeCommandWithViteOptionButViteNotRunning() $this->artisan('serve --no-ansi --vite') ->expectsOutput('Starting the HydeRC server... Use Ctrl+C to stop') ->assertExitCode(0); + + $this->assertFileExists('app/storage/framework/cache/vite.hot'); } public function testHydeServeCommandWithViteOptionThrowsWhenPortIsInUse() diff --git a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php index faa90a4676a..4c804e9bf85 100644 --- a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php +++ b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php @@ -24,28 +24,6 @@ protected function tearDown(): void $this->cleanUpFilesystem(); } - public function testRunningReturnsTrueWhenEnvironmentVariableIsSet() - { - putenv('HYDE_SERVER_VITE=enabled'); - - $this->assertTrue(Vite::running()); - - putenv('HYDE_SERVER_VITE'); - } - - public function testRunningReturnsFalseWhenEnvironmentVariableIsNotSetOrDisabled() - { - $this->assertFalse(Vite::running()); - - putenv('HYDE_SERVER_VITE=disabled'); - - $this->assertFalse(Vite::running()); - - putenv('HYDE_SERVER_VITE'); - - $this->assertFalse(Vite::running()); - } - public function testRunningReturnsTrueWhenViteHotFileExists() { $this->file('app/storage/framework/cache/vite.hot'); diff --git a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php index 9df671ec114..eeaa62b5a47 100644 --- a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php +++ b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php @@ -335,18 +335,6 @@ public function testGetOpenCommandForUnknownOS() $this->assertNull($this->getMock()->getOpenCommand('UnknownOS')); } - public function testViteOptionPropagatesToEnvironmentVariables() - { - $command = $this->getMock(['vite' => true]); - $this->assertSame('enabled', $command->getEnvironmentVariables()['HYDE_SERVER_VITE']); - - $command = $this->getMock(['vite' => false]); - $this->assertFalse(isset($command->getEnvironmentVariables()['HYDE_SERVER_VITE'])); - - $command = $this->getMock(); - $this->assertFalse(isset($command->getEnvironmentVariables()['HYDE_SERVER_VITE'])); - } - public function testWithViteArgument() { HydeKernel::setInstance(new HydeKernel()); diff --git a/packages/realtime-compiler/src/ConsoleOutput.php b/packages/realtime-compiler/src/ConsoleOutput.php index fdb1cf0a801..0d1d6a611bf 100644 --- a/packages/realtime-compiler/src/ConsoleOutput.php +++ b/packages/realtime-compiler/src/ConsoleOutput.php @@ -5,6 +5,7 @@ namespace Hyde\RealtimeCompiler; use Closure; +use Hyde\Facades\Vite; use Hyde\Hyde; use Illuminate\Support\Str; use Illuminate\Support\Arr; @@ -35,7 +36,7 @@ public function printStartMessage(string $host, int $port, array $environment = sprintf('Listening on: %s', $url, $url), (config('hyde.server.dashboard.enabled') || Arr::has($environment, 'HYDE_SERVER_DASHBOARD')) && Arr::get($environment, 'HYDE_SERVER_DASHBOARD') === 'enabled' ? sprintf('Live dashboard: %s/dashboard', $url, $url) : null, - Arr::get($environment, 'HYDE_SERVER_VITE') === 'enabled' ? + Vite::running() ? sprintf('Vite HMR server: http://%s:5173', $host, $host) : null, '', ]);