Last active
April 29, 2019 23:04
-
-
Save fuelingtheweb/b7943b945e733239b5714531cc34ba84 to your computer and use it in GitHub Desktop.
Artisan commands for compiling and releasing assets. I version control my production-ready compiled assets, but not the everyday compiled assets for local development. I made these two Laravel artisan commands for compiling for production and then releasing those assets in a deployment hook.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
use Illuminate\Support\Facades\File; | |
class AssetsCompile extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'assets:compile'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Compile assets for next release'; | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function handle() | |
{ | |
$this->ensureCompiledFoldersExist(); | |
$this->info('Compiling assets for production via `yarn run production`'); | |
passthru('yarn run production'); | |
$this->info('Copying compiled assets to ./resources/compiled'); | |
collect($this->moveManifestAndGetContents())->keys()->each(function ($path) { | |
File::copy(public_path($path), resource_path('compiled' . $path)); | |
}); | |
} | |
public function ensureCompiledFoldersExist() | |
{ | |
$resourcePaths = ['compiled', 'compiled/js', 'compiled/css']; | |
foreach ($resourcePaths as $path) { | |
if (!File::exists(resource_path($path))) { | |
File::makeDirectory(resource_path($path)); | |
} | |
} | |
} | |
public function moveManifestAndGetContents() | |
{ | |
$manifest = File::get(public_path('mix-manifest.json')); | |
File::put(resource_path('compiled/mix-manifest.json'), $manifest); | |
return json_decode($manifest, true); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
use Illuminate\Support\Facades\File; | |
class AssetsRelease extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'assets:release'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Move compiled assets to public folder'; | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function handle() | |
{ | |
$this->ensurePublicFoldersExist(); | |
$this->info('Moving compiled assets:'); | |
collect($this->moveManifestAndGetContents())->keys()->each(function ($path) { | |
$this->line($path); | |
File::copy(resource_path('compiled' . $path), public_path($path)); | |
}); | |
$this->info('Moved compiled assets to public folder'); | |
} | |
public function ensurePublicFoldersExist() | |
{ | |
foreach (['js', 'css'] as $path) { | |
if (!File::exists(public_path($path))) { | |
File::makeDirectory(public_path($path)); | |
} | |
} | |
} | |
public function moveManifestAndGetContents() | |
{ | |
$manifest = File::get(resource_path('compiled/mix-manifest.json')); | |
File::put(public_path('mix-manifest.json'), $manifest); | |
return json_decode($manifest, true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment