Skip to content

Commit

Permalink
Use only hotfiles instead of internal environment variable
Browse files Browse the repository at this point in the history
See #2060
  • Loading branch information
caendesilva committed Dec 8, 2024
1 parent bf88379 commit d4570f7
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 45 deletions.
4 changes: 3 additions & 1 deletion packages/framework/src/Console/Commands/ServeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
]);
}

Expand Down Expand Up @@ -204,6 +204,8 @@ protected function runViteProcess(): void
);
}

Filesystem::touch('app/storage/framework/cache/vite.hot');

$this->vite = Process::forever()->start('npm run dev');
}

Expand Down
8 changes: 1 addition & 7 deletions packages/framework/src/Facades/Vite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
namespace Hyde\Framework\Testing\Feature\Commands;

use Closure;
use Hyde\Facades\Filesystem;
use Hyde\Hyde;
use Hyde\Testing\TestCase;
use Illuminate\Contracts\Process\InvokedProcess;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Process;
use Mockery;
use TypeError;

/**
Expand Down Expand Up @@ -202,7 +205,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')
Expand Down Expand Up @@ -245,7 +248,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')
Expand Down
22 changes: 0 additions & 22 deletions packages/framework/tests/Unit/Facades/ViteFacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
12 changes: 0 additions & 12 deletions packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
3 changes: 2 additions & 1 deletion packages/realtime-compiler/src/ConsoleOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Hyde\RealtimeCompiler;

use Closure;
use Hyde\Facades\Vite;
use Hyde\Hyde;
use Illuminate\Support\Str;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -35,7 +36,7 @@ public function printStartMessage(string $host, int $port, array $environment =
sprintf('<span class="text-white">Listening on:</span> <a href="%s" class="text-yellow-500">%s</a>', $url, $url),
(config('hyde.server.dashboard.enabled') || Arr::has($environment, 'HYDE_SERVER_DASHBOARD')) && Arr::get($environment, 'HYDE_SERVER_DASHBOARD') === 'enabled' ?
sprintf('<span class="text-white">Live dashboard:</span> <a href="%s/dashboard" class="text-yellow-500">%s/dashboard</a>', $url, $url) : null,
Arr::get($environment, 'HYDE_SERVER_VITE') === 'enabled' ?
Vite::running() ?
sprintf('<span class="text-white">Vite HMR server:</span> <a href="http://%s:5173" class="text-yellow-500">http://%s:5173</a>', $host, $host) : null,
'',
]);
Expand Down

0 comments on commit d4570f7

Please sign in to comment.