61 lines
3.4 KiB
TypeScript
61 lines
3.4 KiB
TypeScript
// Credentials never live in storebound.config.ts or in git. Sources, first hit wins:
|
|
// 1. environment: PLAY_SERVICE_ACCOUNT (path to the JSON key, or the JSON itself), ASC_KEY_ID, ASC_ISSUER_ID,
|
|
// ASC_P8 (path to AuthKey_<id>.p8, or the PEM itself)
|
|
// 2. <project>/.storebound/credentials.json (gitignored), same names:
|
|
// { "PLAY_SERVICE_ACCOUNT": "...", "ASC_KEY_ID": "...", "ASC_ISSUER_ID": "...", "ASC_P8": "...",
|
|
// "reviewContact": { "firstName": "", "lastName": "", "phone": "", "email": "" } }
|
|
// 3. ASC_P8 only: ~/.appstoreconnect/private_keys/AuthKey_<ASC_KEY_ID>.p8 (where Xcode / altool keep it)
|
|
import { existsSync, readFileSync } from 'node:fs';
|
|
import { homedir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
|
|
export interface ServiceAccount { client_email: string; private_key: string; token_uri?: string }
|
|
export interface AscKey { keyId: string; issuerId: string; pem: string }
|
|
export interface ReviewContact { firstName: string; lastName: string; phone: string; email: string }
|
|
|
|
type Store = Record<string, unknown>;
|
|
|
|
function fileStore(root: string): Store {
|
|
const p = join(root, '.storebound', 'credentials.json');
|
|
if (!existsSync(p)) return {};
|
|
try { return JSON.parse(readFileSync(p, 'utf8')) as Store; } catch (e) { throw new Error(`${p}: ${(e as Error).message}`); }
|
|
}
|
|
|
|
function value(root: string, name: string): string | undefined {
|
|
const env = process.env[name];
|
|
if (env) return env;
|
|
const v = fileStore(root)[name];
|
|
return typeof v === 'string' && v ? v : undefined;
|
|
}
|
|
|
|
/** A path or the inline content itself. */
|
|
const inline = (v: string, marker: RegExp): string => (marker.test(v) ? v : readFileSync(v.replace(/^~(?=\/)/, homedir()), 'utf8'));
|
|
|
|
export function parseServiceAccount(text: string): ServiceAccount {
|
|
const j = JSON.parse(text) as Partial<ServiceAccount>;
|
|
if (typeof j.client_email !== 'string' || typeof j.private_key !== 'string' || !j.private_key.includes('PRIVATE KEY'))
|
|
throw new Error('not a Google service account JSON key (client_email / private_key missing)');
|
|
return { client_email: j.client_email, private_key: j.private_key, token_uri: j.token_uri };
|
|
}
|
|
|
|
export function playServiceAccount(root: string): ServiceAccount {
|
|
const v = value(root, 'PLAY_SERVICE_ACCOUNT');
|
|
if (!v) throw new Error('PLAY_SERVICE_ACCOUNT is not set (env, or .storebound/credentials.json): path to the service account JSON key');
|
|
return parseServiceAccount(inline(v, /^\s*\{/));
|
|
}
|
|
|
|
export function ascKey(root: string): AscKey {
|
|
const keyId = value(root, 'ASC_KEY_ID');
|
|
const issuerId = value(root, 'ASC_ISSUER_ID');
|
|
if (!keyId || !issuerId) throw new Error('ASC_KEY_ID and ASC_ISSUER_ID are not set (env, or .storebound/credentials.json)');
|
|
const p8 = value(root, 'ASC_P8') ?? join(homedir(), '.appstoreconnect/private_keys', `AuthKey_${keyId}.p8`);
|
|
if (!/BEGIN PRIVATE KEY/.test(p8) && !existsSync(p8.replace(/^~(?=\/)/, homedir()))) throw new Error(`App Store Connect key not found: ${p8} (set ASC_P8)`);
|
|
return { keyId, issuerId, pem: inline(p8, /BEGIN PRIVATE KEY/) };
|
|
}
|
|
|
|
/** App Review contact from .storebound/credentials.json (personal data, so never in the config). */
|
|
export function reviewContact(root: string): ReviewContact | null {
|
|
const c = fileStore(root).reviewContact as Partial<ReviewContact> | undefined;
|
|
if (!c?.email) return null;
|
|
return { firstName: c.firstName ?? '', lastName: c.lastName ?? '', phone: c.phone ?? '', email: c.email };
|
|
}
|