Skip to content

Commit 1c389f2

Browse files
committed
new api endpoints + better error handling
1 parent 31671f7 commit 1c389f2

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

src/Commands/BundleCommand.php

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Native\Laravel\Commands;
44

5+
use Carbon\CarbonInterface;
56
use Illuminate\Console\Command;
67
use Illuminate\Support\Facades\Http;
78
use Illuminate\Support\Number;
@@ -34,7 +35,7 @@ public function handle()
3435
}
3536

3637
if (! $this->checkAuthenticated()) {
37-
$this->error('Invalid API token: check your ZEPHPYR_TOKEN on https://zephpyr.com/user/api-tokens');
38+
$this->error('Invalid API token: check your ZEPHPYR_TOKEN on https://'.$this->hostname().'/user/api-tokens');
3839

3940
return static::FAILURE;
4041
}
@@ -67,9 +68,21 @@ public function handle()
6768

6869
$this->cleanUp();
6970

71+
return static::FAILURE;
72+
} elseif ($result->status() === 422) {
73+
$this->error('Zephpyr returned the following error:');
74+
$this->error(''.$result->json('message'));
75+
$this->cleanUp();
76+
77+
return static::FAILURE;
78+
} elseif ($result->status() === 429) {
79+
$this->error('Zephpyr has a rate limit on builds per hour. Please try again in '.now()->addSeconds(intval($result->header('Retry-After')))->diffForHumans(syntax: CarbonInterface::DIFF_ABSOLUTE).'.');
80+
$this->cleanUp();
81+
7082
return static::FAILURE;
7183
} elseif ($result->failed()) {
72-
$this->error("Failed to upload zip to Zephpyr. Error: {$result->status()}");
84+
$this->error("Failed to upload zip to Zephpyr. Error code: {$result->status()}");
85+
ray($result->body());
7386
$this->cleanUp();
7487

7588
return static::FAILURE;
@@ -92,9 +105,11 @@ protected function cleanUp(): void
92105
$this->line('Cleaning up…');
93106

94107
$previousBuilds = glob(base_path('temp/app_*.zip'));
108+
$failedZips = glob(base_path('temp/app_*.part'));
95109

96-
foreach ($previousBuilds as $previousBuild) {
97-
@unlink($previousBuild);
110+
$deleteFiles = array_merge($previousBuilds, $failedZips);
111+
foreach ($deleteFiles as $file) {
112+
@unlink($file);
98113
}
99114
}
100115

@@ -156,10 +171,12 @@ private function addFilesToZip(ZipArchive $zip): void
156171

157172
$this->finderToZip($vendor, $zip, 'vendor');
158173

159-
$nodeModules = (new Finder)->files()
160-
->in(base_path('node_modules'));
174+
if (file_exists(base_path('node_modules'))) {
175+
$nodeModules = (new Finder)->files()
176+
->in(base_path('node_modules'));
161177

162-
$this->finderToZip($nodeModules, $zip, 'node_modules');
178+
$this->finderToZip($nodeModules, $zip, 'node_modules');
179+
}
163180
}
164181

165182
private function finderToZip(Finder $finder, ZipArchive $zip, ?string $path = null): void
@@ -178,6 +195,11 @@ private function baseUrl(): string
178195
return str(config('nativephp-internal.zephpyr.host'))->finish('/');
179196
}
180197

198+
protected function hostname(): string
199+
{
200+
return parse_url(config('nativephp-internal.zephpyr.host'), PHP_URL_HOST);
201+
}
202+
181203
private function sendToZephpyr()
182204
{
183205
$this->line('Uploading zip to Zephpyr…');
@@ -186,7 +208,7 @@ private function sendToZephpyr()
186208
->withoutRedirecting() // Upload won't work if we follow the redirect
187209
->withToken(config('nativephp-internal.zephpyr.token'))
188210
->attach('archive', fopen($this->zipPath, 'r'), $this->zipName)
189-
->post($this->baseUrl().'api/build/'.$this->key);
211+
->post($this->baseUrl().'api/v1/project/'.$this->key.'/build/');
190212
}
191213

192214
private function checkAuthenticated()
@@ -195,14 +217,14 @@ private function checkAuthenticated()
195217

196218
return Http::acceptJson()
197219
->withToken(config('nativephp-internal.zephpyr.token'))
198-
->get($this->baseUrl().'api/user')->successful();
220+
->get($this->baseUrl().'api/v1/user')->successful();
199221
}
200222

201223
private function fetchLatestBundle(): bool
202224
{
203225
$response = Http::acceptJson()
204226
->withToken(config('nativephp-internal.zephpyr.token'))
205-
->get($this->baseUrl().'api/download/'.$this->key);
227+
->get($this->baseUrl().'api/v1/project/'.$this->key.'/build/download');
206228

207229
if ($response->failed()) {
208230
return false;
@@ -225,7 +247,7 @@ private function checkForZephpyrKey()
225247
$this->line(base_path('.env'));
226248
$this->line('');
227249
$this->info('Not set up with Zephpyr yet? Secure your NativePHP app builds and more!');
228-
$this->info('Check out https://zephpyr.com');
250+
$this->info('Check out https://'.$this->hostname().'');
229251
$this->line('');
230252

231253
return false;
@@ -244,7 +266,7 @@ private function checkForZephpyrToken()
244266
$this->line(base_path('.env'));
245267
$this->line('');
246268
$this->info('Not set up with Zephpyr yet? Secure your NativePHP app builds and more!');
247-
$this->info('Check out https://zephpyr.com');
269+
$this->info('Check out https://'.$this->hostname().'');
248270
$this->line('');
249271

250272
return false;

0 commit comments

Comments
 (0)