import { simpleGit } from 'simple-git';
import { existsSync, promises as fs } from 'fs';
import { dirname } from 'path';
import getCachedItemPath from './getCachedItemPath.js';
export default function cloneIntoCache(bucket, itemPath) {
    return async ({ repository, checkout }) => {
        const destination = getCachedItemPath(bucket, itemPath, checkout);
        const parentDir = dirname(destination);
        if (existsSync(destination)) {
            return;
        }
        if (!existsSync(parentDir)) {
            await fs.mkdir(parentDir, { recursive: true });
        }
        const git = simpleGit();
        await git.clone(repository, destination, checkout
            ? {
                '--branch': checkout,
            }
            : {});
    };
}
