// Name: Clone Active GitHub Repo // Description: Clones the GitHub repo from the active Chrome tab into ~/projects/reference-repo (or ~/Documents/projects/reference-repo) and optionally opens it in Finder. // Author: tayiorbeii // GitHub: tayiorbeii import "@johnlindquist/kit" const url = await getActiveTab("Google Chrome") if (!url || !/^https?:\/\/(www\.)?github\.com\//i.test(url)) { exit() } const match = url.match(/^https?:\/\/(?:www\.)?github\.com\/([^\/]+)\/([^\/#?]+)(?:[\/#?]|$)/i) if (!match) { exit() } const owner = decodeURIComponent(match[1]) const repo = decodeURIComponent(match[2].replace(/\.git$/i, "")) const candidates = [ home("projects"), home("Projects"), home("Documents", "projects"), home("Documents", "Projects"), ] let baseDir = "" for (const d of candidates) { if (await pathExists(d)) { baseDir = d break } } if (!baseDir) { baseDir = home("Documents", "projects") await ensureDir(baseDir) } const referenceRoot = path.join(baseDir, "reference-repo") await ensureDir(referenceRoot) const destRepoDir = path.join(referenceRoot, repo) if (await pathExists(destRepoDir)) { exit() } try { await $`gh repo clone ${owner}/${repo} ${destRepoDir}` } catch { exit() } const openChoice = await arg( { placeholder: "Open cloned repo in Finder?", hint: "[y]es/[n]o", }, [ { name: "[y]es", value: true }, { name: "[n]o", value: false }, ] ) if (openChoice) { await revealInFinder(destRepoDir) }