| 59e6667 | | | 1 | import { log } from "@clack/prompts"; |
| e93a978 | | | 2 | import { readFile, writeFile, mkdir } from "node:fs/promises"; |
| 69d1a72 | | | 3 | import { existsSync, readFileSync } from "node:fs"; |
| e93a978 | | | 4 | import { homedir } from "node:os"; |
| 69d1a72 | | | 5 | import { join, resolve, dirname } from "node:path"; |
| e93a978 | | | 6 | |
| e93a978 | | | 7 | export interface GroveConfig { |
| e93a978 | | | 8 | hub: string; |
| e93a978 | | | 9 | token?: string; |
| e93a978 | | | 10 | } |
| e93a978 | | | 11 | |
| e93a978 | | | 12 | const CONFIG_DIR = join(homedir(), ".grove"); |
| e93a978 | | | 13 | const CONFIG_PATH = join(CONFIG_DIR, "config.json"); |
| e93a978 | | | 14 | |
| e93a978 | | | 15 | const DEFAULT_CONFIG: GroveConfig = { |
| e93a978 | | | 16 | hub: "https://grove.host", |
| e93a978 | | | 17 | }; |
| e93a978 | | | 18 | |
| e93a978 | | | 19 | export async function loadConfig(): Promise<GroveConfig> { |
| e93a978 | | | 20 | try { |
| e93a978 | | | 21 | const raw = await readFile(CONFIG_PATH, "utf-8"); |
| e93a978 | | | 22 | return { ...DEFAULT_CONFIG, ...JSON.parse(raw) }; |
| e93a978 | | | 23 | } catch { |
| e93a978 | | | 24 | return { ...DEFAULT_CONFIG }; |
| e93a978 | | | 25 | } |
| e93a978 | | | 26 | } |
| e93a978 | | | 27 | |
| e93a978 | | | 28 | export async function saveConfig(config: GroveConfig): Promise<void> { |
| e93a978 | | | 29 | await mkdir(CONFIG_DIR, { recursive: true }); |
| e93a978 | | | 30 | await writeFile(CONFIG_PATH, JSON.stringify(config, null, 2) + "\n", { |
| e93a978 | | | 31 | mode: 0o600, |
| e93a978 | | | 32 | }); |
| e93a978 | | | 33 | } |
| e93a978 | | | 34 | |
| e93a978 | | | 35 | export async function getToken(): Promise<string> { |
| e93a978 | | | 36 | const config = await loadConfig(); |
| e93a978 | | | 37 | if (!config.token) { |
| 59e6667 | | | 38 | log.error("Not logged in. Run: grove auth login"); |
| e93a978 | | | 39 | process.exit(1); |
| e93a978 | | | 40 | } |
| e93a978 | | | 41 | return config.token; |
| e93a978 | | | 42 | } |
| e93a978 | | | 43 | |
| e93a978 | | | 44 | export async function getHub(): Promise<string> { |
| e93a978 | | | 45 | const config = await loadConfig(); |
| e93a978 | | | 46 | return config.hub; |
| e93a978 | | | 47 | } |
| 69d1a72 | | | 48 | |
| 69d1a72 | | | 49 | /** |
| 69d1a72 | | | 50 | * Infer the repo name from the current directory's .sl/config. |
| 69d1a72 | | | 51 | * Looks for [remotefilelog] reponame = <name>. |
| 69d1a72 | | | 52 | */ |
| 69d1a72 | | | 53 | function inferRepoName(): string | null { |
| 69d1a72 | | | 54 | let dir = process.cwd(); |
| 69d1a72 | | | 55 | while (dir !== dirname(dir)) { |
| 69d1a72 | | | 56 | const configPath = join(dir, ".sl", "config"); |
| 69d1a72 | | | 57 | if (existsSync(configPath)) { |
| 69d1a72 | | | 58 | const content = readFileSync(configPath, "utf-8"); |
| 69d1a72 | | | 59 | const match = content.match(/reponame\s*=\s*(.+)/); |
| 69d1a72 | | | 60 | if (match) return match[1].trim(); |
| 69d1a72 | | | 61 | } |
| 69d1a72 | | | 62 | dir = dirname(dir); |
| 69d1a72 | | | 63 | } |
| 69d1a72 | | | 64 | return null; |
| 69d1a72 | | | 65 | } |
| 69d1a72 | | | 66 | |
| 69d1a72 | | | 67 | /** |
| 69d1a72 | | | 68 | * Get the repo slug (owner/repo) from --repo arg or infer from .sl/config. |
| 69d1a72 | | | 69 | * If inferred, queries the API to find the owner. |
| 69d1a72 | | | 70 | */ |
| 69d1a72 | | | 71 | export async function getRepoSlug(args: string[]): Promise<string> { |
| 69d1a72 | | | 72 | const repoIdx = args.indexOf("--repo"); |
| 69d1a72 | | | 73 | if (repoIdx !== -1 && args[repoIdx + 1]) { |
| 69d1a72 | | | 74 | return args[repoIdx + 1]; |
| 69d1a72 | | | 75 | } |
| 69d1a72 | | | 76 | |
| 69d1a72 | | | 77 | const repoName = inferRepoName(); |
| 69d1a72 | | | 78 | if (!repoName) { |
| 59e6667 | | | 79 | log.error("Could not infer repo. Use --repo <owner/repo> or run from a Sapling repo."); |
| 69d1a72 | | | 80 | process.exit(1); |
| 69d1a72 | | | 81 | } |
| 69d1a72 | | | 82 | |
| 69d1a72 | | | 83 | // Need to look up the owner via the API |
| 69d1a72 | | | 84 | const hub = await getHub(); |
| 69d1a72 | | | 85 | const token = await getToken(); |
| 69d1a72 | | | 86 | const res = await fetch(`${hub}/api/repos`, { |
| 69d1a72 | | | 87 | headers: { Authorization: `Bearer ${token}` }, |
| 69d1a72 | | | 88 | }); |
| 69d1a72 | | | 89 | if (!res.ok) { |
| 59e6667 | | | 90 | log.error("Could not fetch repos to infer owner."); |
| 69d1a72 | | | 91 | process.exit(1); |
| 69d1a72 | | | 92 | } |
| 69d1a72 | | | 93 | const { repos } = (await res.json()) as { repos: Array<{ owner_name: string; name: string }> }; |
| 69d1a72 | | | 94 | const match = repos.find((r) => r.name === repoName); |
| 69d1a72 | | | 95 | if (!match) { |
| 59e6667 | | | 96 | log.error(`Repo '${repoName}' not found. Use --repo <owner/repo>.`); |
| 69d1a72 | | | 97 | process.exit(1); |
| 69d1a72 | | | 98 | } |
| 69d1a72 | | | 99 | return `${match.owner_name}/${match.name}`; |
| 69d1a72 | | | 100 | } |