Skip to content

Commit

Permalink
Update Vite server to check if the port is free
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Nov 12, 2024
1 parent 2208e17 commit c584036
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/framework/src/Console/Commands/ServeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,23 @@ protected function getOpenCommand(string $osFamily): ?string

protected function runViteProcess(): void
{
if (!$this->isPortAvailable(5173)) {
throw new InvalidArgumentException(
'Unable to start Vite server: Port 5173 is already in use. ' .
'Please stop any other Vite processes and try again.'
);
}

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

protected function isPortAvailable(int $port): bool
{
$socket = @fsockopen('localhost', $port, $errno, $errstr, 1);
if ($socket !== false) {
fclose($socket);
return false;
}
return true;
}
}
12 changes: 12 additions & 0 deletions packages/framework/tests/Feature/Commands/ServeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Contracts\Process\InvokedProcess;
use Illuminate\Support\Facades\Process;
use TypeError;
use InvalidArgumentException;

/**
* @covers \Hyde\Console\Commands\ServeCommand
Expand Down Expand Up @@ -265,6 +266,17 @@ public function testHydeServeCommandWithViteOptionButViteNotRunning()
->assertExitCode(0);
}

public function testHydeServeCommandWithViteOptionThrowsWhenPortIsInUse()
{
$socket = stream_socket_server('tcp://127.0.0.1:5173');

$this->artisan('serve --vite')
->expectsOutputToContain('Unable to start Vite server: Port 5173 is already in use')
->assertExitCode(1);

stream_socket_shutdown($socket, STREAM_SHUT_RDWR);
}

protected function binaryPath(): string
{
return Hyde::path('vendor/hyde/realtime-compiler/bin/server.php');
Expand Down

0 comments on commit c584036

Please sign in to comment.