<?php

namespace App\Commands;

use App\Services\Command;
use App\Services\Docker;
use Illuminate\Support\Facades\Process;

class Yarn extends Command
{
    protected $signature = 'yarn 
        {commands* : The yarn command to run}';

    protected $description = 'Run yarn within the container';

    public function handle(): void
    {
        $themeName = config('project.env')->get('THEME_NAME');
        $cwd = sprintf('%s/%s', config('project.themeCwd'), $themeName);

        $cmdString = implode(' ', $this->argument('commands'));

        $shellCommand = Docker::exec(
            command: sprintf('yarn %s', $cmdString),
            cwd: $cwd
        );

        Process::forever()->run($shellCommand, function (string $type, string $output) {
            echo $output;
        });
    }

}
