|
| 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 Remove extends BaseCommand |
| 11 | +{ |
| 12 | + protected $group = 'Modules'; |
| 13 | + protected $name = 'vite:remove'; |
| 14 | + protected $description = 'Remove codeigniter vite generated files and settings'; |
| 15 | + |
| 16 | + public function run(array $params) |
| 17 | + { |
| 18 | + # cleaning start. |
| 19 | + CLI::write('Removing Codeigniter Vite 🔥⚡', 'white', 'red'); |
| 20 | + CLI::newLine(); |
| 21 | + |
| 22 | + # Now let's remove vite files (vite.config.js, package.json & resources direction). |
| 23 | + $this->removeFrameworkFiles(); |
| 24 | + |
| 25 | + # Reset .env file. |
| 26 | + $this->resetEnvFile(); |
| 27 | + |
| 28 | + # Everything is ready now. |
| 29 | + CLI::write('Codeigniter vite has removed successfuly ✅', 'green'); |
| 30 | + CLI::newLine(); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Remove vite files (vite.config.js, package.json & resources) |
| 35 | + * |
| 36 | + * @return void |
| 37 | + */ |
| 38 | + private function removeFrameworkFiles() |
| 39 | + { |
| 40 | + CLI::write('⚡ Removing vite files...', 'yellow'); |
| 41 | + CLI::newLine(); |
| 42 | + |
| 43 | + # First vite.config.js |
| 44 | + is_file(ROOTPATH . 'vite.config.js') ? unlink(ROOTPATH . 'vite.config.js') : CLI::error('vite.config.js does not exist'); |
| 45 | + |
| 46 | + # package.json |
| 47 | + is_file(ROOTPATH . 'package.json') ? unlink(ROOTPATH . 'package.json') : CLI::error('package.json does not exist'); |
| 48 | + |
| 49 | + # Empty resources dir. |
| 50 | + if (is_dir(ROOTPATH . 'resources')) |
| 51 | + { |
| 52 | + $publisher = new Publisher(null, ROOTPATH . 'resources'); |
| 53 | + $publisher->wipe(); |
| 54 | + } |
| 55 | + |
| 56 | + CLI::newLine(); |
| 57 | + CLI::write('Deleted ✅', 'green'); |
| 58 | + CLI::newLine(); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Remove vite configs in .env file |
| 63 | + * |
| 64 | + * @return void |
| 65 | + */ |
| 66 | + private function resetEnvFile() |
| 67 | + { |
| 68 | + CLI::write('Reseting .env file...', 'yellow'); |
| 69 | + CLI::newLine(); |
| 70 | + |
| 71 | + # Get the env file. |
| 72 | + $envFile = ROOTPATH . '.env'; |
| 73 | + # Get last backup. |
| 74 | + $backupFile = ROOTPATH . env('VITE_BACKUP_FILE'); |
| 75 | + |
| 76 | + # Does exist? if not, generate it =) |
| 77 | + if (is_file($envFile)) |
| 78 | + { |
| 79 | + # Remove current .env |
| 80 | + unlink($envFile); |
| 81 | + # Restore backup if exists |
| 82 | + if (is_file($backupFile)) |
| 83 | + { |
| 84 | + copy($backupFile, ROOTPATH . '.env'); |
| 85 | + unlink($backupFile); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + # env updated. |
| 90 | + CLI::write('.env file updated ✅', 'green'); |
| 91 | + CLI::newLine(); |
| 92 | + } |
| 93 | +} |
0 commit comments