|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace CloudinaryLabs\CloudinaryLaravel\Console; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use Illuminate\Support\Facades\File; |
| 7 | + |
| 8 | +class InstallCommand extends Command |
| 9 | +{ |
| 10 | + protected $signature = 'cloudinary:install'; |
| 11 | + |
| 12 | + protected $description = 'Install the Cloudinary Laravel SDK'; |
| 13 | + |
| 14 | + protected function hasLivewire(): bool |
| 15 | + { |
| 16 | + return class_exists(\Livewire\Livewire::class); |
| 17 | + } |
| 18 | + |
| 19 | + protected function hasInertia(): bool |
| 20 | + { |
| 21 | + return class_exists(\Inertia\Inertia::class); |
| 22 | + } |
| 23 | + |
| 24 | + protected function detectInertiaFramework(): ?string |
| 25 | + { |
| 26 | + if (! File::exists(base_path('package.json'))) { |
| 27 | + return null; |
| 28 | + } |
| 29 | + |
| 30 | + $package = json_decode(File::get(base_path('package.json')), true); |
| 31 | + $dependencies = array_merge( |
| 32 | + $package['dependencies'] ?? [], |
| 33 | + $package['devDependencies'] ?? [] |
| 34 | + ); |
| 35 | + |
| 36 | + if (isset($dependencies['@vitejs/plugin-react'])) { |
| 37 | + return 'react'; |
| 38 | + } |
| 39 | + |
| 40 | + if (isset($dependencies['@vitejs/plugin-vue'])) { |
| 41 | + return 'vue'; |
| 42 | + } |
| 43 | + |
| 44 | + if (isset($dependencies['@sveltejs/vite-plugin-svelte'])) { |
| 45 | + return 'svelte'; |
| 46 | + } |
| 47 | + |
| 48 | + return null; |
| 49 | + } |
| 50 | + |
| 51 | + protected function installLivewireComponents(): void |
| 52 | + { |
| 53 | + // @TODO: implement livewire components publishing |
| 54 | + } |
| 55 | + |
| 56 | + protected function installInertiaComponents(string $framework): void |
| 57 | + { |
| 58 | + switch ($framework) { |
| 59 | + case 'react': |
| 60 | + $this->requireNpmPackages(['@cloudinary/react', '@cloudinary/url-gen']); |
| 61 | + break; |
| 62 | + case 'vue': |
| 63 | + $this->requireNpmPackages(['@cloudinary/vue', '@cloudinary/url-gen']); |
| 64 | + break; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + protected function requireNpmPackages(array $packages): void |
| 69 | + { |
| 70 | + $command = 'npm install '.implode(' ', $packages); |
| 71 | + |
| 72 | + if (File::exists(base_path('pnpm-lock.yaml'))) { |
| 73 | + $command = 'pnpm add '.implode(' ', $packages); |
| 74 | + } elseif (File::exists(base_path('yarn.lock'))) { |
| 75 | + $command = 'yarn add '.implode(' ', $packages); |
| 76 | + } |
| 77 | + |
| 78 | + // @TODO: run the command |
| 79 | + } |
| 80 | + |
| 81 | + protected function installBladeComponents(): void |
| 82 | + { |
| 83 | + // @TODO: implement blade components publishing |
| 84 | + } |
| 85 | + |
| 86 | + public function handle() |
| 87 | + { |
| 88 | + $this->info('Installing Cloudinary Laravel SDK...'); |
| 89 | + |
| 90 | + $installedComponents = false; |
| 91 | + |
| 92 | + if ($this->hasLivewire() && $this->confirm('We detected Livewire in your application. Would you like to install the Livewire components?')) { |
| 93 | + $this->installLivewireComponents(); |
| 94 | + $installedComponents = true; |
| 95 | + } |
| 96 | + |
| 97 | + if (! $installedComponents && $this->hasInertia()) { |
| 98 | + $framework = $this->detectInertiaFramework(); |
| 99 | + if ($framework && $this->confirm("We detected Inertia.js with {$framework}. Would you like to install the {$framework} components?")) { |
| 100 | + $this->installInertiaComponents($framework); |
| 101 | + $installedComponents = true; |
| 102 | + $this->info("Cloudinary {$framework} components installed successfully."); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + if (! $installedComponents) { |
| 107 | + $this->installBladeComponents(); |
| 108 | + $this->info('Blade components installed successfully.'); |
| 109 | + } |
| 110 | + |
| 111 | + $this->info('Cloudinary Laravel SDK installation completed.'); |
| 112 | + } |
| 113 | +} |
0 commit comments