Skip to content

Commit

Permalink
Polished installation experience
Browse files Browse the repository at this point in the history
  • Loading branch information
slavarazum committed Mar 24, 2024
1 parent 3c72988 commit f78c982
Showing 1 changed file with 73 additions and 39 deletions.
112 changes: 73 additions & 39 deletions src/Console/Commands/BroadcastingInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@ class BroadcastingInstallCommand extends \Illuminate\Foundation\Console\Broadcas
/** {@inheritdoc} */
public function handle(): int
{
$this->output->write($this->title());

$enableRedisBroadcastingDriver = confirm(
'Would you like to enable the Redis broadcasting driver in .env?',
default: true,
);

$installNodeDependencies = confirm(
'Install and build the Node dependencies required for broadcasting?',
default: true
);

$publishConfiguration = confirm(
'Publish the Wave configuration file?',
default: false,
);

$this->askToStarRepository();

$this->call('config:publish', ['name' => 'broadcasting']);

// Install channel routes file...
Expand Down Expand Up @@ -58,25 +77,24 @@ public function handle(): int
}
}

$this->updateBroadcastingDriver();

$this->installNodeDependencies();
if ($enableRedisBroadcastingDriver) {
$this->updateBroadcastingDriver();
}

$this->publishConfiguration();
if ($installNodeDependencies) {
$this->installNodeDependencies();
}

// $this->askToStarRepository();
if ($publishConfiguration) {
$this->publishConfiguration();
}

return Command::SUCCESS;
}

/** {@inheritdoc} */
protected function installNodeDependencies()
{
if (! confirm('Would you like to install and build the Node dependencies required for broadcasting?',
default: true)) {
return;
}

$this->components->info('Installing and building Node dependencies.');

if (file_exists(base_path('pnpm-lock.yaml'))) {
Expand All @@ -99,13 +117,15 @@ protected function installNodeDependencies()
$command = Process::command(implode(' && ', $commands))
->path(base_path());

if (! windows_os() || SymfonyProcess::isTtySupported()) {
$command->tty(true);
if (! windows_os() && SymfonyProcess::isTtySupported()) {
$command->tty();
}

if ($command->run()->failed()) {
$this->components->warn("Node dependency installation failed. Please run the following commands manually: \n\n".implode(' && ',
$commands));
$this->components->warn(
"Node dependency installation failed. Please run the following commands manually: \n\n"
.implode(' && ', $commands)
);
} else {
$this->components->info('Node dependencies installed successfully.');
}
Expand All @@ -116,30 +136,21 @@ protected function installNodeDependencies()
*/
protected function updateBroadcastingDriver(): void
{
$enable = confirm(
'Would you like to enable the Redis broadcasting driver for Wave?',
default: true,
);

if (! $enable || File::missing($env = app()->environmentFile())) {
if (File::missing($env = app()->environmentFile())) {
return;
}

File::put(
$env,
Str::of(File::get($env))->replaceMatches('/(BROADCAST_(?:DRIVER|CONNECTION))=\w*/',
fn (array $matches) => $matches[1].'=redis')->value()
Str::of(File::get($env))->replaceMatches(
'/(BROADCAST_(?:DRIVER|CONNECTION))=\w*/',
fn (array $matches) => $matches[1].'=redis'
)->value()
);
}

protected function publishConfiguration(): void
{
if (! confirm('Would you like to publish the Wave configuration file?',
default: false,
hint: 'This will allow you to configure the SSE connection.')) {
return;
}

$this->callSilently('vendor:publish', [
'--provider' => WaveServiceProvider::class,
'--tag' => 'wave-config',
Expand All @@ -148,18 +159,41 @@ protected function publishConfiguration(): void

protected function askToStarRepository()
{
if (confirm('Would you like to star our repo on GitHub?')) {
$repoUrl = 'https://github.com/qruto/laravel-wave';
if (! confirm(
'Star our repo on GitHub during installation?',
default: true,
hint: 'Every star contributes to the package development ⭐'
)) {
return;
}

if (PHP_OS_FAMILY == 'Darwin') {
exec("open {$repoUrl}");
}
if (PHP_OS_FAMILY == 'Windows') {
exec("start {$repoUrl}");
}
if (PHP_OS_FAMILY == 'Linux') {
exec("xdg-open {$repoUrl}");
}
$repoUrl = 'https://github.com/qruto/laravel-wave';

if (PHP_OS_FAMILY == 'Darwin') {
exec("open {$repoUrl}");
}
if (PHP_OS_FAMILY == 'Windows') {
exec("start {$repoUrl}");
}
if (PHP_OS_FAMILY == 'Linux') {
exec("xdg-open {$repoUrl}");
}
}

private function title()
{
return PHP_EOL.<<<'TITLE'
<fg=blue>
____ _ _ _
| __ ) _ __ ___ __ _ __| | ___ __ _ ___| |_(_)_ __ __ _
| _ \| '__/ _ \ / _` |/ _` |/ __/ _` / __| __| | '_ \ / _` |
| |_) | | | (_) | (_| | (_| | (_| (_| \__ \ |_| | | | | (_| |
|____/|_| _\___/_\__,_|\__,_|\___\__,_|___/\__|_|_| |_|\__, |
__ _(_) |_| |__ \ \ / /_ ___ _____ |___/
\ \ /\ / / | __| '_ \ \ \ /\ / / _` \ \ / / _ \
\ V V /| | |_| | | | \ V V / (_| |\ V / __/
\_/\_/ |_|\__|_| |_| \_/\_/ \__,_| \_/ \___|
</>
TITLE.PHP_EOL.PHP_EOL;
}
}

0 comments on commit f78c982

Please sign in to comment.