|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Flux\Console; |
| 4 | + |
| 5 | +use function Laravel\Prompts\{ info, text, note, spin, warning, error, alert, intro, outro, suggest }; |
| 6 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 7 | +use Symfony\Component\Process\Process; |
| 8 | +use Illuminate\Support\Facades\Http; |
| 9 | +use Illuminate\Console\Command; |
| 10 | +use Illuminate\Support\Carbon; |
| 11 | + |
| 12 | +#[AsCommand(name: 'flux:activate')] |
| 13 | +class ActivateCommand extends Command |
| 14 | +{ |
| 15 | + protected $signature = 'flux:activate {key?}'; |
| 16 | + |
| 17 | + protected $description = 'Activate Flux, the official UI component library for Livewire'; |
| 18 | + |
| 19 | + public function handle() |
| 20 | + { |
| 21 | + $key = $this->argument('key'); |
| 22 | + |
| 23 | + if ($key) { |
| 24 | + $this->installFluxPro($key); |
| 25 | + } else { |
| 26 | + $key = text( |
| 27 | + label: 'Enter your license key', |
| 28 | + hint: 'Purchase a license key: https://fluxui.dev/pricing', |
| 29 | + required: true, |
| 30 | + ); |
| 31 | + |
| 32 | + $this->installFluxPro($key); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public function installFluxPro($key) |
| 37 | + { |
| 38 | + $hashKey = app('encrypter')->getKey(); |
| 39 | + |
| 40 | + $fingerprint = hash_hmac('sha256', json_encode($key), $hashKey); |
| 41 | + |
| 42 | + $response = spin( |
| 43 | + message: 'Activating your license...', |
| 44 | + callback: fn () => Http::post('https://fluxui.dev/api/activate', [ 'key' => $key, 'fingerprint' => $fingerprint ]), |
| 45 | + ); |
| 46 | + |
| 47 | + if ($response->failed() && $response->json('error') === 'not-found') { |
| 48 | + warning('Invalid license key'); |
| 49 | + note( 'Contact [email protected] for help'); |
| 50 | + return; |
| 51 | + } elseif ($response->failed()) { |
| 52 | + $response->throw(); |
| 53 | + alert('Failed to activate license'); |
| 54 | + note( 'Contact [email protected] for help'); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + $key = (string) $response->json('key'); |
| 59 | + $email = (string) $response->json('email'); |
| 60 | + $expiresAt = Carbon::parse($response->json('expires_at')); |
| 61 | + |
| 62 | + if ($key === '' || $email === '') { |
| 63 | + error('Whoops, something went wrong. Either your license key or email is invalid.'); |
| 64 | + note( 'Contact [email protected] for help'); |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + // Add creds to auth.json... |
| 69 | + $process = new Process([ |
| 70 | + 'composer', 'config', '-a', |
| 71 | + 'http-basic.flux.composer.sh', $email, $key |
| 72 | + ]); |
| 73 | + $process->run(); |
| 74 | + |
| 75 | + if (! $process->isSuccessful()) { |
| 76 | + echo "Failed to add license to auth.json. Console output: " . $process->getErrorOutput(); |
| 77 | + note( 'Contact [email protected] for help'); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + info('[√] License key added to auth.json'); |
| 82 | + |
| 83 | + // Add repository to composer.json... |
| 84 | + $process = new Process(['composer', 'config', 'repositories.flux', 'composer', 'https://flux.composer.sh']); |
| 85 | + $process->run(); |
| 86 | + |
| 87 | + if (! $process->isSuccessful()) { |
| 88 | + echo "Failed to add repository to composer.json. Console output: " . $process->getErrorOutput(); |
| 89 | + note( 'Contact [email protected] for help'); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + info('[√] Repository added to composer.json'); |
| 94 | + |
| 95 | + // Run composer require... |
| 96 | + note('Running: composer require livewire/flux...'); |
| 97 | + |
| 98 | + $process = new Process(['composer', 'require', 'livewire/flux']); |
| 99 | + $process->setTty(true); |
| 100 | + $process->setTimeout(null); |
| 101 | + |
| 102 | + $process->run(function ($type, $buffer) { |
| 103 | + echo $buffer; |
| 104 | + }); |
| 105 | + |
| 106 | + if (! $process->isSuccessful()) { |
| 107 | + error("We are unable to install Flux automatically. Try running `composer require livewire/flux` manually."); |
| 108 | + note( 'Contact [email protected] for help'); |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + if ($expiresAt->isPast()) { |
| 113 | + note(''); |
| 114 | + warning('This license has expired. You will need to purchase a new license to receive updates.'); |
| 115 | + note('Extend your license here: https://fluxui.dev/licenses'); |
| 116 | + } |
| 117 | + |
| 118 | + note(''); |
| 119 | + outro('Thanks for using Flux!'); |
| 120 | + note('Your support is an investment in the future of Livewire ❤️'); |
| 121 | + } |
| 122 | +} |
0 commit comments