|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Mihatori\CodeigniterVite\Commands; |
| 4 | + |
| 5 | +use CodeIgniter\CLI\BaseCommand; |
| 6 | +use CodeIgniter\CLI\CLI; |
| 7 | +use CodeIgniter\Publisher\Publisher; |
| 8 | +use Throwable; |
| 9 | + |
| 10 | +class Init extends BaseCommand |
| 11 | +{ |
| 12 | + protected $group = 'Modules'; |
| 13 | + protected $name = 'vite:init'; |
| 14 | + protected $description = 'Initialize codeigniter vite module'; |
| 15 | + |
| 16 | + /** |
| 17 | + * @var string |
| 18 | + */ |
| 19 | + private string $framework; |
| 20 | + |
| 21 | + |
| 22 | + /** |
| 23 | + * Module path |
| 24 | + * |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + private $path; |
| 28 | + |
| 29 | + public function __construct() |
| 30 | + { |
| 31 | + $this->path = service('autoloader')->getNamespace('Mihatori\\CodeigniterVite')[0]; |
| 32 | + } |
| 33 | + |
| 34 | + public function run(array $params) |
| 35 | + { |
| 36 | + # Module start. |
| 37 | + CLI::write('Installing Codeigniter Vite Plugin 🔥⚡', 'white', 'cyan'); |
| 38 | + CLI::newLine(); |
| 39 | + |
| 40 | + # Set framework. |
| 41 | + $this->framework = $params['framework'] ?? CLI::prompt('Choose a framework: ', ['none', 'vue', 'react', 'svelte']); |
| 42 | + CLI::newLine(); |
| 43 | + |
| 44 | + # First, what if user select a none supported framework ?! |
| 45 | + # if that's true, return an error message with available frameworks. |
| 46 | + if (!in_array($this->framework, ['none', 'vue', 'react', 'svelte'])) |
| 47 | + { |
| 48 | + CLI::error("❌ Sorry, but $this->framework is not supported!"); |
| 49 | + CLI::error('Available frameworks are: ' . CLI::color('vue, react and svelte', 'green')); |
| 50 | + CLI::newLine(); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + # Now let's generate vite necesary files (vite.config.js, package.json & resources direction). |
| 55 | + $this->generateFrameworkFiles(); |
| 56 | + |
| 57 | + # Update .env file. |
| 58 | + $this->updateEnvFile(); |
| 59 | + |
| 60 | + # Everything is ready now. |
| 61 | + CLI::write('Codeigniter vite has succussfuly installed ✅', 'green'); |
| 62 | + CLI::newLine(); |
| 63 | + CLI::write('run: npm install && npm run dev'); |
| 64 | + CLI::newLine(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Generate vite files (vite.config.js, package.json & resources) |
| 69 | + * |
| 70 | + * @return void |
| 71 | + */ |
| 72 | + private function generateFrameworkFiles() |
| 73 | + { |
| 74 | + CLI::write('⚡ Generating vite files...', 'yellow'); |
| 75 | + CLI::newLine(); |
| 76 | + |
| 77 | + # Framework files. |
| 78 | + $paths = ['vite.config.js', 'package.json', 'resources']; |
| 79 | + |
| 80 | + if ($this->framework === 'none') |
| 81 | + { |
| 82 | + $publisher = new Publisher($this->path . 'Config/default', ROOTPATH); |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + $publisher = new Publisher($this->path . "Config/$this->framework", ROOTPATH); |
| 87 | + } |
| 88 | + |
| 89 | + # Publish them. |
| 90 | + try |
| 91 | + { |
| 92 | + $publisher->addPaths($paths)->merge(true); |
| 93 | + } |
| 94 | + catch (Throwable $e) |
| 95 | + { |
| 96 | + $this->showError($e); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + CLI::write('Vite files are ready ✅', 'green'); |
| 101 | + CLI::newLine(); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Set vite configs in .env file |
| 106 | + * |
| 107 | + * @return void |
| 108 | + */ |
| 109 | + private function updateEnvFile() |
| 110 | + { |
| 111 | + CLI::write('Updating .env file...', 'yellow'); |
| 112 | + |
| 113 | + # Get the env file. |
| 114 | + $envFile = ROOTPATH . '.env'; |
| 115 | + |
| 116 | + # Does exist? if not, generate it =) |
| 117 | + if (is_file($envFile)) |
| 118 | + { |
| 119 | + # But first, let's take a backup. |
| 120 | + copy($envFile, $envFile . 'BACKUP-' . time()); |
| 121 | + |
| 122 | + # Get .env.default content |
| 123 | + $content = file_get_contents($this->path . 'Config/env.default'); |
| 124 | + |
| 125 | + # Append it. |
| 126 | + file_put_contents($envFile, "\n\n$content", FILE_APPEND); |
| 127 | + } |
| 128 | + else |
| 129 | + { |
| 130 | + # As we said before, generate it. |
| 131 | + copy($this->path . 'Config/env.default', ROOTPATH . '.env'); |
| 132 | + } |
| 133 | + |
| 134 | + # Define framework. |
| 135 | + if ($this->framework !== 'none') |
| 136 | + { |
| 137 | + $updates = file_get_contents($envFile); |
| 138 | + $updates = str_replace("VITE_FRAMEWORK='none'", "VITE_FRAMEWORK='$this->framework'", $updates); |
| 139 | + file_put_contents($envFile, $updates); |
| 140 | + } |
| 141 | + |
| 142 | + # env updated. |
| 143 | + CLI::newLine(); |
| 144 | + CLI::write('.env file updated ✅', 'green'); |
| 145 | + CLI::newLine(); |
| 146 | + } |
| 147 | +} |
0 commit comments