Skip to content

Commit 31671f7

Browse files
committed
fix: bundles getting bigger + tests
1 parent c552035 commit 31671f7

File tree

4 files changed

+195
-85
lines changed

4 files changed

+195
-85
lines changed

config/nativephp.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@
5858
/**
5959
* A list of files and folders that should be removed from the
6060
* final app before it is bundled for production.
61-
* You may use glob / wildcard patterns here.
61+
* You may use glob wildcard patterns here.
6262
*/
6363
'cleanup_exclude_files' => [
64+
'build',
65+
'temp',
6466
'content',
6567
'storage/app/framework/{sessions,testing,cache}',
6668
'storage/logs/laravel.log',

src/Commands/BundleCommand.php

Lines changed: 72 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Console\Command;
66
use Illuminate\Support\Facades\Http;
7+
use Illuminate\Support\Number;
78
use Native\Laravel\Commands\Traits\CleansEnvFile;
89
use Symfony\Component\Finder\Finder;
910
use 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
}
Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
<?php
22

3-
it('will remove laravel.log by default before building', function () {
3+
beforeAll(function () {
4+
// Set up the environment variables
5+
putenv('ZEPHPYR_KEY=dummy');
6+
putenv('ZEPHPYR_TOKEN=dummy');
7+
});
8+
9+
afterAll(function () {
10+
// Clean up the temp folder
11+
foreach (glob(base_path('temp/app_*.zip')) as $zip) {
12+
unlink($zip);
13+
}
14+
});
15+
16+
afterEach(function () {
17+
// Clean up the app folder recursively
18+
rmdir_recursive(base_path('resources/app'));
19+
});
20+
21+
it('will remove laravel.log by default before bundling', function () {
422
$logPath = 'resources/app/storage/logs';
523
$laravelLog = $logPath.'/laravel.log';
624

@@ -11,23 +29,15 @@
1129
file_put_contents($laravelLog, 'TEST');
1230

1331
// Run the test
14-
$this->artisan('native:minify resources/app');
15-
$this->assertFalse(file_exists($laravelLog));
32+
$this->artisan('native:bundle --without-cleanup')
33+
->expectsOutput('Creating zip archive');
1634

17-
// Clean up after ourselves
18-
if (file_exists($laravelLog)) {
19-
unlink($laravelLog);
20-
}
21-
if (file_exists('resources/app/storage/logs')) {
22-
rmdir('resources/app/storage/logs');
23-
}
24-
if (file_exists('resources/app/storage')) {
25-
rmdir('resources/app/storage');
26-
}
27-
removeAppFolder();
35+
// Assert
36+
expect(basename($laravelLog))
37+
->not->toBeInZip(findLatestZipPath());
2838
});
2939

30-
it('will remove the content folder by default before building', function () {
40+
it('will remove the content folder by default before bundling', function () {
3141
$contentPath = 'resources/app/content';
3242

3343
// Create a dummy copy of the folder
@@ -36,14 +46,12 @@
3646
}
3747

3848
// Run the test
39-
$this->artisan('native:minify resources/app');
40-
$this->assertFalse(file_exists($contentPath));
49+
$this->artisan('native:bundle --without-cleanup')
50+
->expectsOutput('Creating zip archive');
4151

42-
// Clean up after ourselves
43-
if (file_exists($contentPath)) {
44-
unlink($contentPath);
45-
}
46-
removeAppFolder();
52+
// Assert
53+
expect($contentPath)
54+
->not->toBeInZip(findLatestZipPath());
4755
});
4856

4957
it('will remove only files that match a globbed path', function () {
@@ -63,26 +71,17 @@
6371
file_put_contents($noDeletePath, 'DO NOT DELETE ME');
6472

6573
// Run the test
66-
$this->artisan('native:minify resources/app');
67-
$this->assertFalse(file_exists($yes1DeletePath));
68-
$this->assertFalse(file_exists($yes2DeletePath));
69-
$this->assertTrue(file_exists($noDeletePath));
70-
71-
// Clean up after ourselves
72-
foreach ([$yes1DeletePath, $yes2DeletePath, $noDeletePath] as $remove) {
73-
if (file_exists($remove)) {
74-
unlink($remove);
75-
}
76-
}
77-
if (file_exists($wildcardPath)) {
78-
rmdir($wildcardPath);
79-
}
80-
removeAppFolder();
81-
});
74+
$this->artisan('native:bundle --without-cleanup')
75+
->expectsOutput('Creating zip archive');
8276

83-
function removeAppFolder()
84-
{
85-
if (file_exists('resources/app')) {
86-
rmdir('resources/app');
87-
}
88-
}
77+
// Assert
78+
$latestZip = findLatestZipPath();
79+
80+
expect($yes1DeletePath)
81+
->not->toBeInZip($latestZip);
82+
expect($yes2DeletePath)
83+
->not->toBeInZip($latestZip);
84+
expect($noDeletePath)
85+
->toBeInZip($latestZip);
86+
87+
});

0 commit comments

Comments
 (0)