import * as fs from 'fs'; import * as jsYaml from 'js-yaml'; import * as os from 'os'; import * as path from 'path'; import * as dotenv from 'dotenv'; dotenv.config({path: path.join(os.homedir(), '.ocp/.env')}); const CREDENTIAL_FILE = 'credentials.json'; const environmentConfig: EnvironmentConfig = { rivendell: process.env.RIVENDELL || 'https://rivendell.zaius.com', awsRegion: process.env.AWS_REGION || 'us-east-1', uploadBucket: process.env.UPLOAD_BUCKET || 'zaius-apps' }; export interface EnvironmentConfig { rivendell: string; awsRegion: string; uploadBucket: string; } export interface Credential { apiKey?: string; } export interface CredentialConfig { [key: string]: Partial; } export function getEnv() { return process.env.OCP_ENV || 'production'; } export function getDependencyFileUrl(runtime: string) { return `http://s3.amazonaws.com/zaius-public/zip/.env/production/${runtime}/dependencies.json`; } export function runtimeConfig(): EnvironmentConfig { return environmentConfig; } let cachedCredential: Credential; export function getActiveCredential(): Credential { if (cachedCredential) { return cachedCredential; } const file = path.join(os.homedir(), `.ocp/${CREDENTIAL_FILE}`); if (fs.existsSync(file)) { try { const creds: CredentialConfig = jsYaml.safeLoad(fs.readFileSync(file, 'utf8')) || {}; cachedCredential = creds || {}; } catch (e) { console.error(`Unable to parse credential file.`); process.exit(1); } } return cachedCredential || {}; }