Something went wrong. Try again.
A basic CLI to set up Tangled repos
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566import { mutateConfig } from './config.js';import { ask } from './term.js';
// Mapping from GitHub logins to AT identities, stored in the config file// under `githubUsers`. Keys are lowercased logins; values are// { handle, did }, or null when the user chose to ignore that account.// The first time an unknown account is seen, the user is asked interactively// and the answer is remembered for future runs.
export function createMapper({ agent, config, interactive }) { const map = { ...(config.githubUsers ?? {}) };
async function persist(key, value) { map[key] = value; await mutateConfig((c) => { c.githubUsers = { ...c.githubUsers, [key]: value }; }); }
return { isKnown(login) { return Object.hasOwn(map, login.toLowerCase()); },
// The GitHub login mapped to `did`, if any. loginFor(did) { for (const [login, m] of Object.entries(map)) { if (m?.did === did) return login; } return null; },
// Record a mapping without prompting (used for the acting user's own login). recordLogin(login, { handle, did }) { return persist(login.toLowerCase(), { handle, did }); },
// → { handle, did } when mapped, null when ignored or unknown. async resolve(ghUser) { if (!ghUser?.login) return null; // deleted ("ghost") accounts const key = ghUser.login.toLowerCase(); if (Object.hasOwn(map, key)) return map[key]; if (key.endsWith('[bot]')) return (map[key] = null); // bots have no AT handle; skip quietly if (!interactive) return (map[key] = null); // in-memory only — a later interactive run still asks
const profile = ghUser.html_url ?? `https://github.com/${ghUser.login}`; console.log(`\nNew GitHub account: ${ghUser.login} (${profile})`); for (;;) { const answer = await ask(' Map to AT handle (Enter to ignore): '); if (!answer || answer.toLowerCase() === 'ignore') { await persist(key, null); return null; } const handle = answer.replace(/^@/, '').toLowerCase(); try { const { data } = await agent.com.atproto.identity.resolveHandle({ handle }); await persist(key, { handle, did: data.did }); return map[key]; } catch { console.log(` Could not resolve "${handle}" — try again, or press Enter to ignore.`); } } }, };}