Skip to content

Commit

Permalink
added assets build command to use by site
Browse files Browse the repository at this point in the history
  • Loading branch information
A1Gard committed Oct 29, 2024
1 parent a117e04 commit d1ee8fe
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 3 deletions.
1 change: 1 addition & 0 deletions README-fa.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ php artisan make:part PartName segmentName

```bash
php artisan client
php artisan build
```
</div>

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Optimize client assets, `scss`,`js`,`css`

```bash
php artisan client
php artisan build
```

### theme parts file
Expand Down
130 changes: 130 additions & 0 deletions app/Console/Commands/AssetsBuild.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
class AssetsBuild extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'build';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Safely run npm build process ';

/**
* Store command output
*/
private $commandOutput = '';

/**
* Get Laravel root path
*/
private function getLaravelRoot()
{
return base_path();
}

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
try {
// Set working directory to Laravel root
chdir($this->getLaravelRoot());

// Check if npm is installed
$npmVersion = new Process(['npm', '-v']);
$npmVersion->setWorkingDirectory($this->getLaravelRoot());
$npmVersion->run();

if (!$npmVersion->isSuccessful()) {
$this->addOutput('npm is not installed. Please install npm first.');
return Command::FAILURE;
}

// Check if package.json exists
if (!file_exists($this->getLaravelRoot() . '/package.json')) {
$this->addOutput('package.json not found in ' . $this->getLaravelRoot());
return Command::FAILURE;
}


// Check if node_modules exists
if (!file_exists($this->getLaravelRoot() . '/node_modules')) {
$this->addOutput('node_modules not found. Installing dependencies...');

$installProcess = new Process(['npm', 'install']);
$installProcess->setWorkingDirectory($this->getLaravelRoot());
$installProcess->setTimeout(3600); // 1 hour timeout

$installProcess->run(function ($type, $buffer) {
$this->addOutput($buffer);
});

if (!$installProcess->isSuccessful()) {
throw new ProcessFailedException($installProcess);
}
}

// Run npm build
$this->addOutput('Starting build process in: ' . $this->getLaravelRoot());

$buildProcess = new Process(['./node_modules/.bin/vite', 'build']);
$buildProcess->setWorkingDirectory($this->getLaravelRoot());
// $this->addOutput($this->getLaravelRoot());

$buildProcess->setTimeout(3600); // 1 hour timeout

$buildProcess->run(function ($type, $buffer) {
$this->addOutput($buffer);
});

if (!$buildProcess->isSuccessful()) {
throw new ProcessFailedException($buildProcess);
}

$this->addOutput('Build completed successfully!');

// Store output in cache for retrieval
cache()->put('build_command_output', $this->commandOutput, now()->addMinutes(5));

return Command::SUCCESS;

} catch (\Exception $e) {
$errorMessage = 'Build process failed: ' . $e->getMessage();
$this->addOutput($errorMessage);
cache()->put('build_command_output', $this->commandOutput, now()->addMinutes(5));
return Command::FAILURE;
}
}

/**
* Add output to both console and stored output
*/
private function addOutput($output)
{
$this->commandOutput .= $output . PHP_EOL;
$this->info($output); // This will only show in CLI
}

/**
* Get the command output
*/
public function getOutput()
{
return $this->commandOutput;
}
}
22 changes: 22 additions & 0 deletions app/Http/Controllers/Admin/AreaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,26 @@ public function sortSave(Request $request)
logAdmin(__METHOD__, __CLASS__, $p->area_id);
return ['OK' => true, 'message' => __("As you wished sort saved")];
}

public function build(){

$exitCode = \Artisan::call('build');

// Get the command output from cache
$output = cache()->get('build_command_output', 'No output available');

// return response()->json([
// 'success' => $exitCode === 0,
// 'exit_code' => $exitCode,
// 'output' => $output
// ]);
logAdmin(__METHOD__, __CLASS__, null);
if ($exitCode == 0){
\Log::info($output);
return redirect()->back()->with(['message' => __('Assets build successfully')]);
}else{
\Log::error($output);
return redirect()->back()->with(['message' => __('Assets build failed')]);
}
}
}
3 changes: 2 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace App\Providers;
use App\Console\Commands\AssetsBuild;
use App\Helpers\TDate;
use App\Http\Middleware\Acl;
use App\Models\Area;
use App\Models\Part;
use App\Observers\AreaObsever;
use App\Observers\PartObsever;
use Carbon\Carbon;
use Illuminate\Pagination\Paginator;
Expand All @@ -23,6 +23,7 @@ public function register(): void
//
$this->commands([
TranslatorCommand::class,
AssetsBuild::class,
]);
foreach (config('xshop.payment.gateways') as $gateway){
/** @var \App\Contracts\Payment $gateway */
Expand Down
2 changes: 1 addition & 1 deletion resources/js/panel/responsive-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const responsiveControl = function () {

document.querySelectorAll('.table-list')?.forEach(function (table) {

console.log(table);
// console.log(table);
// Get all the header cells
const headers = Array.from(table.querySelectorAll('th')).map(th => th.textContent.trim());

Expand Down
9 changes: 8 additions & 1 deletion resources/lang/fa.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@
"As you wished restored successfully": "همانطور که شما مایل بودید بازیافت شد",
"As you wished sort saved": "همانطور که شما مایل بودید مرتب شدند",
"As you wished updated successfully": "همانطور که شما مایل بودید به روز شد",
"Assets build failed": "دارایی‌ها ساخته نشده‌اند",
"Assets build successfully": "دارایی‌ها با موفقیت ساخته شدن",
"Attaching": "پیوست کردن",
"Attachment": "پیوست",
"Attachments": "پیوست‌ها",
Expand All @@ -105,6 +107,7 @@
"Batch delete": "حذف چندگانه",
"Batch restore": "بازیافت چندگانه",
"Before proceeding, please check your email for a verification link.": "قبل از ادامه فرآیند لطفا رایانه خودتون رو بررسی کنید که لینک ارسال شده باشد",
"Build assets": "ساخت دارایی‌های طراحی",
"Bulk actions:": "کار دسته‌جمعی",
"Cache cleared": "حافظه نهان پاک شد",
"Call us!": "با ما تماس بگیرید!",
Expand Down Expand Up @@ -423,6 +426,7 @@
"SEO Score": "امتیاز سئو",
"SKU": "",
"SMS": "پیامک",
"SVG image": "تصویر SVG",
"Save": "ذخیره",
"Save all settings": "ذخیره همه تنظیمات",
"Search": "جستجو",
Expand Down Expand Up @@ -463,7 +467,8 @@
"Sliders": "اسلایدرها",
"Sliders list": "فهرست اسلایدر",
"Slug": "نامک",
"Sort": "مرتب",
"Sort": "ترتیب",
"Sort & filter": "ترتیب و صافی",
"State": "استان",
"States": "استان‌ها",
"States list": "فهرست استان‌ها",
Expand All @@ -476,6 +481,7 @@
"Subtitle": "زیرعنوان",
"Successfully Invoices": "صورت‌حساب های موفق",
"Summary": "خلاصه",
"Svg image": "تصویر SVG",
"System notification": "پیام سیستم",
"Table of contents": "فهرست عناوین",
"Tag": "برچسب",
Expand Down Expand Up @@ -619,6 +625,7 @@
"news": "خبر",
"not searchable": "غیرقابل جستجو",
"one second ago": "یک ثانیه پیش",
"or build it using the button below": "یا توسط دکمه ساختن دارایی پایین اقدام کنید",
"paragraphs": "بند",
"parent_id": "والد",
"password repeat": "تکرار گذرواژه",
Expand Down
8 changes: 8 additions & 0 deletions resources/views/admin/areas/area-list.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
@if(config('app.demo'))
<div class="alert alert-warning">
{{__("Theme loaded every :M minutes in demo version", ['M' => 5])}}
{{__("or build it using the button below")}}
</div>
@endif
@include('components.err')
<div class="row">
@foreach($areas as $area)
<div class="col-md-4">
Expand All @@ -18,5 +20,11 @@
</a>
</div>
@endforeach
<div class="col-md-4">
<a class="area-list-item " style="background: blueviolet" href="{{route('admin.area.build')}}">
<i class="ri-hammer-line"></i>
{{__("Build assets")}}
</a>
</div>
</div>
@endsection
1 change: 1 addition & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ function () {
Route::post('update/{area}', [\App\Http\Controllers\Admin\AreaController::class, "update"])->name('update');
Route::post('update/model/{model}/{id}', [\App\Http\Controllers\Admin\AreaController::class, "updateModel"])->name('update.model');
Route::get('sort/{area}', [\App\Http\Controllers\Admin\AreaController::class, "sort"])->name('sort');
Route::get('build', [\App\Http\Controllers\Admin\AreaController::class, "build"])->name('build');
Route::post('sort-save/{area}', [\App\Http\Controllers\Admin\AreaController::class, "sortSave"])->name('sort-save');
}
);
Expand Down

0 comments on commit d1ee8fe

Please sign in to comment.