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