Skip to content

Commit e841fdd

Browse files
committed
first round with install command
1 parent b2c612e commit e841fdd

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

src/Console/InstallCommand.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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

Comments
 (0)