← back to Beverlyhillsvideos

social/poster/env.mjs

47 lines

// env.mjs — resolve BHV Instagram config.
// Reads from process.env first; falls back to the secrets-manager master .env
// so the launchd/cron wrapper doesn't have to export every key by hand.
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';

const SECRETS_ENV = path.join(os.homedir(), 'Projects/secrets-manager/.env');

function fromSecretsFile(key) {
  try {
    const txt = fs.readFileSync(SECRETS_ENV, 'utf8');
    // last occurrence wins (matches how the secrets CLI appends)
    let val = null;
    for (const line of txt.split('\n')) {
      const m = line.match(/^([A-Z0-9_]+)=(.*)$/);
      if (m && m[1] === key) val = m[2];
    }
    return val;
  } catch {
    return null;
  }
}

function get(key, fallback = null) {
  return process.env[key] ?? fromSecretsFile(key) ?? fallback;
}

export const cfg = {
  // Graph API identity — populated once the account is Business/Creator + token issued.
  igUserId: get('BHV_IG_USER_ID'),
  accessToken: get('BHV_IG_ACCESS_TOKEN'),
  graphVersion: get('BHV_IG_GRAPH_VERSION', 'v21.0'),

  // Where Instagram fetches the video from (must be public HTTPS).
  siteBase: get('BHV_SITE_BASE', 'https://beverlyhillsvideos.com'),

  // Safety: nothing posts for real unless BHV_IG_LIVE=1 AND a token exists.
  live: get('BHV_IG_LIVE', '0') === '1',

  handle: get('BHV_INSTAGRAM_HANDLE', 'beverlyhillsvideos'),
};

export function canGoLive() {
  return Boolean(cfg.live && cfg.igUserId && cfg.accessToken);
}