2026-10-05 18:10:18 -04:00
<? php
declare ( strict_types = 1 );
namespace Pterodactyl\Console\Commands\Extensions ;
use Illuminate\Console\Attributes\Description ;
use Illuminate\Console\Attributes\Signature ;
use Illuminate\Console\Command ;
use Illuminate\Support\Facades\Process ;
use Pterodactyl\Contracts\Extensions\InstallsExtensions ;
use Pterodactyl\Exceptions\Extensions\InvalidExtensionException ;
use Pterodactyl\Services\Extensions\ExtensionAssetPublisher ;
use Pterodactyl\Services\Extensions\ExtensionManifestValidator ;
#[ Description ( 'Build and publish a local extension; --watch republishes each successful build.' )]
#[ Signature ( 'p:extension:dev {path : Path to the source package.} {--watch : Watch frontend sources for changes.} {--enable : Enable after the initial build.}' )]
class DevCommand extends Command
{
public function handle ( ExtensionManifestValidator $validator , InstallsExtensions $installer , ExtensionAssetPublisher $assets ) : int
{
try {
$manifest = $validator -> fromDirectory ( $this -> argument ( 'path' ));
$publish = function () use ( $installer , $manifest ) : void {
2026-10-06 14:01:38 -04:00
// Republishing its own source over the installed copy is what this command is for.
$installer -> install ( $manifest -> directory , ( bool ) $this -> option ( 'enable' ), replace : true );
2026-10-05 18:10:18 -04:00
$this -> components -> info ( 'Published ' . $manifest -> id . '.' );
};
if ( ! $manifest -> hasUi ()) {
$publish ();
return self :: SUCCESS ;
}
$build = Process :: path ( $manifest -> directory ) -> timeout ( 300 ) -> run ([ 'npm' , 'run' , 'build' ]);
if ( $build -> failed ()) {
$this -> error ( $build -> output () . $build -> errorOutput ());
return self :: FAILURE ;
}
$publish ();
if ( $this -> option ( 'watch' )) {
2026-10-06 14:05:50 -04:00
// Disabled extensions publish no frontend build, so there is nothing to reload.
throw_if ( $assets -> currentVersion ( $manifest -> id ) === null , InvalidExtensionException :: class , "Extension \" { $manifest -> id } \" is disabled; pass --enable to watch it." );
2026-10-05 18:10:18 -04:00
$assets -> watchDevelopment ( $manifest -> id );
try {
$buffer = '' ;
$process = Process :: path ( $manifest -> directory ) -> forever () -> start ([ 'npm' , 'run' , 'dev' ], function ( string $type , string $output ) use ( & $buffer , $publish ) : void {
$this -> output -> write ( $output );
$buffer .= $output ;
if ( preg_match ( '/built in [^\r\n]+[\r\n]/' , $buffer )) {
$buffer = '' ;
try {
$publish ();
} catch ( InvalidExtensionException $exception ) {
$this -> components -> error ( $exception -> getMessage ());
}
}
$buffer = mb_substr ( $buffer , - 8192 );
});
$this -> trap ([ SIGINT , SIGTERM ], function () use ( $process ) : void {
$process -> signal ( SIGTERM );
});
return $process -> wait () -> exitCode () ?? self :: FAILURE ;
} finally {
$assets -> stopDevelopment ( $manifest -> id );
}
}
} catch ( InvalidExtensionException $invalidExtensionException ) {
$this -> components -> error ( $invalidExtensionException -> getMessage ());
return self :: FAILURE ;
}
return self :: SUCCESS ;
}
}