Feature/nodejs (#397)

* Add node support using nvm

* Add icon

* Rename to NodeJS

* Rename to NodeJS

* update php and node logo

* only services which have units can be started,restarted,stopped,disabled and enabled

* add tests

---------

Co-authored-by: Saeed Vaziry <mr.saeedvaziry@gmail.com>
Co-authored-by: Saeed Vaziry <61919774+saeedvaziry@users.noreply.github.com>
This commit is contained in:
Mark Topper
2024-12-24 17:49:27 +01:00
committed by GitHub
parent da1043185a
commit 924920e6e8
21 changed files with 706 additions and 2 deletions

View File

@ -0,0 +1,66 @@
<?php
namespace App\Web\Pages\Servers\NodeJS;
use App\Actions\NodeJS\InstallNewNodeJsVersion;
use App\Enums\NodeJS;
use App\Models\Service;
use App\Web\Pages\Servers\NodeJS\Widgets\NodeJSList;
use App\Web\Pages\Servers\Page;
use Filament\Actions\Action;
use Filament\Forms\Components\Select;
use Filament\Notifications\Notification;
use Filament\Support\Enums\MaxWidth;
class Index extends Page
{
protected static ?string $slug = 'servers/{server}/nodejs';
protected static ?string $title = 'NodeJS';
public function mount(): void
{
$this->authorize('viewAny', [Service::class, $this->server]);
}
public function getWidgets(): array
{
return [
[NodeJSList::class, ['server' => $this->server]],
];
}
protected function getHeaderActions(): array
{
$installedNodeVersions = $this->server->installedNodejsVersions();
return [
Action::make('install')
->authorize(fn () => auth()->user()?->can('create', [Service::class, $this->server]))
->label('Install Node')
->icon('heroicon-o-archive-box-arrow-down')
->modalWidth(MaxWidth::Large)
->form([
Select::make('version')
->options(
collect(config('core.nodejs_versions'))
->filter(fn ($version) => ! in_array($version, array_merge($installedNodeVersions, [NodeJS::NONE])))
->mapWithKeys(fn ($version) => [$version => $version])
->toArray()
)
->rules(InstallNewNodeJsVersion::rules($this->server)['version']),
])
->modalSubmitActionLabel('Install')
->action(function (array $data) {
app(InstallNewNodeJsVersion::class)->install($this->server, $data);
Notification::make()
->success()
->title('Installing Node...')
->send();
$this->dispatch('$refresh');
}),
];
}
}

View File

@ -0,0 +1,117 @@
<?php
namespace App\Web\Pages\Servers\NodeJS\Widgets;
use App\Actions\NodeJS\ChangeDefaultCli;
use App\Actions\Service\Uninstall;
use App\Models\Server;
use App\Models\Service;
use Exception;
use Filament\Notifications\Notification;
use Filament\Tables\Actions\Action;
use Filament\Tables\Actions\ActionGroup;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget as Widget;
use Illuminate\Database\Eloquent\Builder;
class NodeJSList extends Widget
{
public Server $server;
protected $listeners = ['$refresh'];
protected function getTableQuery(): Builder
{
return Service::query()->where('type', 'nodejs')->where('server_id', $this->server->id);
}
protected function getTableColumns(): array
{
return [
TextColumn::make('version')
->sortable(),
TextColumn::make('status')
->label('Status')
->badge()
->color(fn (Service $service) => Service::$statusColors[$service->status])
->sortable(),
TextColumn::make('is_default')
->label('Default Cli')
->badge()
->color(fn (Service $service) => $service->is_default ? 'primary' : 'gray')
->state(fn (Service $service) => $service->is_default ? 'Yes' : 'No')
->sortable(),
TextColumn::make('created_at')
->label('Installed At')
->formatStateUsing(fn ($record) => $record->created_at_by_timezone),
];
}
/**
* @throws Exception
*/
public function table(Table $table): Table
{
return $table
->heading(null)
->query($this->getTableQuery())
->columns($this->getTableColumns())
->actions([
ActionGroup::make([
$this->defaultNodeJsCliAction(),
$this->uninstallAction(),
]),
]);
}
private function defaultNodeJsCliAction(): Action
{
return Action::make('default-nodejs-cli')
->authorize(fn (Service $nodejs) => auth()->user()?->can('update', $nodejs))
->label('Make Default CLI')
->hidden(fn (Service $service) => $service->is_default)
->action(function (Service $service) {
try {
app(ChangeDefaultCli::class)->change($this->server, ['version' => $service->version]);
Notification::make()
->success()
->title('Default NodeJS CLI changed!')
->send();
} catch (Exception $e) {
Notification::make()
->danger()
->title($e->getMessage())
->send();
throw $e;
}
$this->dispatch('$refresh');
});
}
private function uninstallAction(): Action
{
return Action::make('uninstall')
->authorize(fn (Service $nodejs) => auth()->user()?->can('update', $nodejs))
->label('Uninstall')
->color('danger')
->requiresConfirmation()
->action(function (Service $service) {
try {
app(Uninstall::class)->uninstall($service);
} catch (Exception $e) {
Notification::make()
->danger()
->title($e->getMessage())
->send();
throw $e;
}
$this->dispatch('$refresh');
});
}
}

View File

@ -18,6 +18,7 @@
use App\Web\Pages\Servers\Firewall\Index as FirewallIndex;
use App\Web\Pages\Servers\Logs\Index as LogsIndex;
use App\Web\Pages\Servers\Metrics\Index as MetricsIndex;
use App\Web\Pages\Servers\NodeJS\Index as NodeJsIndex;
use App\Web\Pages\Servers\PHP\Index as PHPIndex;
use App\Web\Pages\Servers\Services\Index as ServicesIndex;
use App\Web\Pages\Servers\Settings as ServerSettings;
@ -60,11 +61,18 @@ public function getSubNavigation(): array
if (auth()->user()->can('viewAny', [Service::class, $this->server])) {
$items[] = NavigationItem::make(PHPIndex::getNavigationLabel())
->icon('heroicon-o-code-bracket')
->icon('icon-php-alt')
->isActiveWhen(fn () => request()->routeIs(PHPIndex::getRouteName().'*'))
->url(PHPIndex::getUrl(parameters: ['server' => $this->server]));
}
if (auth()->user()->can('viewAny', [Service::class, $this->server])) {
$items[] = NavigationItem::make(NodeJsIndex::getNavigationLabel())
->icon('icon-nodejs-alt')
->isActiveWhen(fn () => request()->routeIs(NodeJsIndex::getRouteName().'*'))
->url(NodeJsIndex::getUrl(parameters: ['server' => $this->server]));
}
if (auth()->user()->can('viewAny', [FirewallRule::class, $this->server])) {
$items[] = NavigationItem::make(FirewallIndex::getNavigationLabel())
->icon('heroicon-o-fire')

View File

@ -75,7 +75,7 @@ public function table(Table $table): Table
private function serviceAction(string $type, string $icon): Action
{
return Action::make($type)
->authorize(fn (Service $service) => auth()->user()?->can('update', $service))
->authorize(fn (Service $service) => auth()->user()?->can($type, $service))
->label(ucfirst($type).' Service')
->icon($icon)
->action(function (Service $service) use ($type) {