<?php

namespace App\Actions\Init;

use Illuminate\Support\Facades\Process;

class CloneStarter
{
    public function handle(string $starterRepo, string $projectDir): void
    {
        $this->runOrFail(sprintf('git clone --depth=1 %s .', escapeshellarg($starterRepo)), $projectDir);
        $this->runOrFail('rm -rf .git', $projectDir);
    }

    private function runOrFail(string $command, string $path): void
    {
        $result = Process::path($path)->run($command);

        if (!$result->successful()) {
            $message = trim($result->errorOutput()) ?: trim($result->output()) ?: 'Unknown process failure.';

            throw new \RuntimeException($message);
        }
    }
}
