Skip to content

Commit

Permalink
add new commands
Browse files Browse the repository at this point in the history
  • Loading branch information
joshmanders committed Mar 3, 2025
1 parent abb647b commit d3f0aa0
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 76 deletions.
3 changes: 3 additions & 0 deletions src/CloudinaryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public function boot(): void
if ($this->app->runningInConsole()) {
$this->commands([
Console\InstallCommand::class,
Console\LivewireCommand::class,
Console\ReactCommand::class,
Console\VueCommand::class,
]);
}

Expand Down
90 changes: 14 additions & 76 deletions src/Console/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,24 @@ class InstallCommand extends Command

protected $description = 'Install the Cloudinary Laravel SDK';

protected function hasLivewire(): bool
public function handle()
{
return class_exists(\Livewire\Livewire::class);
}
if ($this->callSilently('vendor:publish', ['--tag' => 'cloudinary-config']) === 0) {
$this->info('Configuration published successfully.');
}

protected function hasInertia(): bool
{
return class_exists(\Inertia\Inertia::class);
match ($this->getDependencies()) {
'react' => $this->call('cloudinary:react'),
'vue' => $this->call('cloudinary:vue'),
'livewire' => $this->call('cloudinary:livewire'),
default => $this->info('No JavaScript framework detected.'),
};
}

protected function detectInertiaFramework(): ?string
private function getDependencies(): ?string
{
if (! File::exists(base_path('package.json'))) {
return null;
}

$package = json_decode(File::get(base_path('package.json')), true);

$dependencies = array_merge(
$package['dependencies'] ?? [],
$package['devDependencies'] ?? []
Expand All @@ -41,73 +42,10 @@ protected function detectInertiaFramework(): ?string
return 'vue';
}

if (isset($dependencies['@sveltejs/vite-plugin-svelte'])) {
return 'svelte';
if (class_exists('Livewire\Livewire')) {
return 'livewire';
}

return null;
}

protected function installLivewireComponents(): void
{
// @TODO: implement livewire components publishing
}

protected function installInertiaComponents(string $framework): void
{
switch ($framework) {
case 'react':
$this->requireNpmPackages(['@cloudinary/react', '@cloudinary/url-gen']);
break;
case 'vue':
$this->requireNpmPackages(['@cloudinary/vue', '@cloudinary/url-gen']);
break;
}
}

protected function requireNpmPackages(array $packages): void
{
$command = 'npm install '.implode(' ', $packages);

if (File::exists(base_path('pnpm-lock.yaml'))) {
$command = 'pnpm add '.implode(' ', $packages);
} elseif (File::exists(base_path('yarn.lock'))) {
$command = 'yarn add '.implode(' ', $packages);
}

// @TODO: run the command
}

protected function installBladeComponents(): void
{
// @TODO: implement blade components publishing
}

public function handle()
{
$this->info('Installing Cloudinary Laravel SDK...');

$installedComponents = false;

if ($this->hasLivewire() && $this->confirm('We detected Livewire in your application. Would you like to install the Livewire components?')) {
$this->installLivewireComponents();
$installedComponents = true;
}

if (! $installedComponents && $this->hasInertia()) {
$framework = $this->detectInertiaFramework();
if ($framework && $this->confirm("We detected Inertia.js with {$framework}. Would you like to install the {$framework} components?")) {
$this->installInertiaComponents($framework);
$installedComponents = true;
$this->info("Cloudinary {$framework} components installed successfully.");
}
}

if (! $installedComponents) {
$this->installBladeComponents();
$this->info('Blade components installed successfully.');
}

$this->info('Cloudinary Laravel SDK installation completed.');
}
}
19 changes: 19 additions & 0 deletions src/Console/LivewireCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace CloudinaryLabs\CloudinaryLaravel\Console;

use Illuminate\Console\Command;

class LivewireCommand extends Command
{
protected $signature = 'cloudinary:livewire';

protected $description = 'Install the Livewire components';

protected $hidden = true;

public function handle()
{
$this->info('Installing Cloudinary Livewire components...');
}
}
19 changes: 19 additions & 0 deletions src/Console/ReactCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace CloudinaryLabs\CloudinaryLaravel\Console;

use Illuminate\Console\Command;

class ReactCommand extends Command
{
protected $signature = 'cloudinary:react';

protected $description = 'Install the React components';

protected $hidden = true;

public function handle()
{
$this->info('Installing Cloudinary React components...');
}
}
19 changes: 19 additions & 0 deletions src/Console/VueCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace CloudinaryLabs\CloudinaryLaravel\Console;

use Illuminate\Console\Command;

class VueCommand extends Command
{
protected $signature = 'cloudinary:vue';

protected $description = 'Install the Vue components';

protected $hidden = true;

public function handle()
{
$this->info('Installing Cloudinary Vue components...');
}
}

0 comments on commit d3f0aa0

Please sign in to comment.