44
55use Illuminate \Console \Command ;
66use Illuminate \Support \Facades \Http ;
7+ use Illuminate \Support \Number ;
78use Native \Laravel \Commands \Traits \CleansEnvFile ;
89use Symfony \Component \Finder \Finder ;
910use ZipArchive ;
@@ -12,7 +13,7 @@ class BundleCommand extends Command
1213{
1314 use CleansEnvFile;
1415
15- protected $ signature = 'native:bundle {--fetch} ' ;
16+ protected $ signature = 'native:bundle {--fetch} {--without-cleanup} ' ;
1617
1718 protected $ description = 'Bundle your application for distribution. ' ;
1819
@@ -24,42 +25,18 @@ class BundleCommand extends Command
2425
2526 public function handle ()
2627 {
27- $ this ->key = config ('nativephp-internal.zephpyr.key ' );
28-
29- if (! $ this ->key ) {
30- $ this ->line ('' );
31- $ this ->warn ('No ZEPHPYR_KEY found. Cannot bundle! ' );
32- $ this ->line ('' );
33- $ this ->line ('Add this app \'s ZEPHPYR_KEY to its .env file: ' );
34- $ this ->line (base_path ('.env ' ));
35- $ this ->line ('' );
36- $ this ->info ('Not set up with Zephpyr yet? Secure your NativePHP app builds and more! ' );
37- $ this ->info ('Check out https://zephpyr.com ' );
38- $ this ->line ('' );
39-
28+ if (! $ this ->checkForZephpyrKey ()) {
4029 return static ::FAILURE ;
4130 }
4231
43- if (! config ('nativephp-internal.zephpyr.token ' )) {
44- $ this ->line ('' );
45- $ this ->warn ('No ZEPHPYR_TOKEN found. Cannot bundle! ' );
46- $ this ->line ('' );
47- $ this ->line ('Add your api ZEPHPYR_TOKEN to its .env file: ' );
48- $ this ->line (base_path ('.env ' ));
49- $ this ->line ('' );
50- $ this ->info ('Not set up with Zephpyr yet? Secure your NativePHP app builds and more! ' );
51- $ this ->info ('Check out https://zephpyr.com ' );
52- $ this ->line ('' );
53-
32+ if (! $ this ->checkForZephpyrToken ()) {
5433 return static ::FAILURE ;
5534 }
5635
57- $ result = $ this ->checkAuthenticated ();
58-
59- if ($ result ->failed ()) {
36+ if (! $ this ->checkAuthenticated ()) {
6037 $ this ->error ('Invalid API token: check your ZEPHPYR_TOKEN on https://zephpyr.com/user/api-tokens ' );
6138
62- return ;
39+ return static :: FAILURE ;
6340 }
6441
6542 if ($ this ->option ('fetch ' )) {
@@ -80,32 +57,47 @@ public function handle()
8057
8158 return static ::FAILURE ;
8259 }
83- // $this->zipName = 'app_CcINfsoQ.zip';
84- // $this->zipPath = base_path('temp/'.$this->zipName);
8560
8661 // Send the zip file
8762 $ result = $ this ->sendToZephpyr ();
8863
89- // dd($result->status(), json_decode($result->body()));
64+ if ($ result ->status () === 413 ) {
65+ $ fileSize = Number::fileSize (filesize ($ this ->zipPath ));
66+ $ this ->error ('The zip file is too large to upload to Zephpyr ( ' .$ fileSize .'). Please contact support. ' );
9067
91- if ($ result ->code () === 413 ) {
92- $ this ->error ('The zip file is too large to upload to Zephpyr. Please contact support. ' );
68+ $ this ->cleanUp ();
9369
9470 return static ::FAILURE ;
9571 } elseif ($ result ->failed ()) {
96- $ this ->error ("Failed to upload zip [ {$ this ->zipPath }] to Zephpyr. " );
72+ $ this ->error ("Failed to upload zip to Zephpyr. Error: {$ result ->status ()}" );
73+ $ this ->cleanUp ();
9774
9875 return static ::FAILURE ;
9976 }
10077
101- @unlink ($ this ->zipPath );
102-
10378 $ this ->info ('Successfully uploaded to Zephpyr. ' );
10479 $ this ->line ('Use native:bundle --fetch to retrieve the latest bundle. ' );
10580
81+ $ this ->cleanUp ();
82+
10683 return static ::SUCCESS ;
10784 }
10885
86+ protected function cleanUp (): void
87+ {
88+ if ($ this ->option ('without-cleanup ' )) {
89+ return ;
90+ }
91+
92+ $ this ->line ('Cleaning up… ' );
93+
94+ $ previousBuilds = glob (base_path ('temp/app_*.zip ' ));
95+
96+ foreach ($ previousBuilds as $ previousBuild ) {
97+ @unlink ($ previousBuild );
98+ }
99+ }
100+
109101 private function zipApplication (): bool
110102 {
111103 $ this ->zipName = 'app_ ' .str ()->random (8 ).'.zip ' ;
@@ -138,7 +130,7 @@ private function addFilesToZip(ZipArchive $zip): void
138130 // TODO: Check the composer.json to make sure there are no symlinked
139131 // or private packages as these will be a pain later
140132
141- $ this ->line ('Adding files to zip… ' );
133+ $ this ->line ('Creating zip archive … ' );
142134
143135 $ app = (new Finder )->files ()
144136 ->followLinks ()
@@ -188,7 +180,7 @@ private function baseUrl(): string
188180
189181 private function sendToZephpyr ()
190182 {
191- $ this ->line ('Uploading to Zephpyr… ' );
183+ $ this ->line ('Uploading zip to Zephpyr… ' );
192184
193185 return Http::acceptJson ()
194186 ->withoutRedirecting () // Upload won't work if we follow the redirect
@@ -203,7 +195,7 @@ private function checkAuthenticated()
203195
204196 return Http::acceptJson ()
205197 ->withToken (config ('nativephp-internal.zephpyr.token ' ))
206- ->get ($ this ->baseUrl ().'api/user ' );
198+ ->get ($ this ->baseUrl ().'api/user ' )-> successful () ;
207199 }
208200
209201 private function fetchLatestBundle (): bool
@@ -220,4 +212,44 @@ private function fetchLatestBundle(): bool
220212
221213 return true ;
222214 }
215+
216+ private function checkForZephpyrKey ()
217+ {
218+ $ this ->key = config ('nativephp-internal.zephpyr.key ' );
219+
220+ if (! $ this ->key ) {
221+ $ this ->line ('' );
222+ $ this ->warn ('No ZEPHPYR_KEY found. Cannot bundle! ' );
223+ $ this ->line ('' );
224+ $ this ->line ('Add this app \'s ZEPHPYR_KEY to its .env file: ' );
225+ $ this ->line (base_path ('.env ' ));
226+ $ this ->line ('' );
227+ $ this ->info ('Not set up with Zephpyr yet? Secure your NativePHP app builds and more! ' );
228+ $ this ->info ('Check out https://zephpyr.com ' );
229+ $ this ->line ('' );
230+
231+ return false ;
232+ }
233+
234+ return true ;
235+ }
236+
237+ private function checkForZephpyrToken ()
238+ {
239+ if (! config ('nativephp-internal.zephpyr.token ' )) {
240+ $ this ->line ('' );
241+ $ this ->warn ('No ZEPHPYR_TOKEN found. Cannot bundle! ' );
242+ $ this ->line ('' );
243+ $ this ->line ('Add your api ZEPHPYR_TOKEN to its .env file: ' );
244+ $ this ->line (base_path ('.env ' ));
245+ $ this ->line ('' );
246+ $ this ->info ('Not set up with Zephpyr yet? Secure your NativePHP app builds and more! ' );
247+ $ this ->info ('Check out https://zephpyr.com ' );
248+ $ this ->line ('' );
249+
250+ return false ;
251+ }
252+
253+ return true ;
254+ }
223255}
0 commit comments