import * as fs from 'fs'; import * as os from 'os'; import * as path from 'path'; import * as chalk from 'chalk'; import {command, help, namespace, param} from 'oo-cli'; import { getEnv } from '../../lib/Config'; import {die} from '../../lib/die'; import {formatError} from '../../lib/formatError'; const STAGING_ENV = { 'OCP_ENV': 'staging', 'RIVENDELL': 'https://rivendell.staging.zaius.com', 'AWS_REGION': 'us-east-1', 'UPLOAD_BUCKET': 'zaius-apps-staging' }; const ENV_WHITELIST = ['staging', 'production']; @namespace('env') export class SetEnvironmentCommand { @param @help('The environment name') public env!: string; @command @help('Set the current environment for the CLI') public set() { try { if (ENV_WHITELIST.includes(this.env)) { const envPath = path.join(os.homedir(), '.ocp'); const envFile = path.join(envPath, `.env`); const setEnvFile = `${envFile}.${this.env}`; if (fs.existsSync(envFile)) { fs.renameSync(envFile, `${envFile}.${getEnv()}`); } if (fs.existsSync(setEnvFile)) { fs.renameSync(setEnvFile, envFile); } else if (this.env === 'staging') { try { fs.statSync(envPath); } catch { fs.mkdirSync(envPath); } fs.writeFileSync(envFile, Object.entries(STAGING_ENV).map((x) => x.join('=')).join('\n')); } console.log(chalk.yellow(`using ${this.env} environment`)); } else { console.log(chalk.red( `Could not switch to environment ${this.env}. Permitted environments are ${ENV_WHITELIST}`)); } } catch (e: any) { die(formatError(e)); } } }