storebound/cli/lib/ctx.ts

31 lines
1.2 KiB
TypeScript

import type { Args } from './args';
import type { Language, Loaded } from './config';
import { playCodes } from './config';
export interface Ctx { loaded: Loaded; args: Args }
export function need<T>(v: T | undefined, what: string): T {
if (v === undefined || v === null) throw new Error(`storebound.config.ts: ${what} is not set`);
return v;
}
/** Play language code -> language entry. */
export function byPlayCode(langs: Language[]): Map<string, Language> {
const m = new Map<string, Language>();
for (const l of langs) for (const c of playCodes(l)) m.set(c, l);
return m;
}
/** App Store locale -> language entry. */
export function byAppleLocale(langs: Language[]): Map<string, Language> {
return new Map(langs.filter(l => l.apple).map(l => [l.apple!, l] as const));
}
/** Asks on the terminal unless --yes. Store actions that are public or irreversible go through this. */
export async function confirm(ctx: Ctx, question: string): Promise<boolean> {
if (ctx.args.has('yes')) return true;
if (!process.stdin.isTTY) { console.log(`${question}\n not a terminal: re-run with --yes to confirm`); return false; }
process.stdout.write(`${question} [y/N] `);
for await (const line of console) return /^y(es)?$/i.test(line.trim());
return false;
}