<commit_before>'use strict';

const fs = require('fs');

const variables = [
  { name: 'NTB_API_KEY' },
  { name: 'NEW_RELIC_LICENSE_KEY', env: ['production'] },
  { name: 'SENTRY_DSN', env: ['production'] },
  { name: 'OAUTH_CLIENT_ID', env: ['development', 'test'] },
  { name: 'OAUTH_CLIENT_SECRET', env: ['development', 'test'] },
  { name: 'OAUTH_ACCESS_TOKEN', env: ['development', 'test'] },
  { name: 'OAUTH_REFRESH_TOKEN', env: ['development', 'test'] },
  { name: 'OAUTH_USER_ID', env: ['development', 'test'] },
  { name: 'AWS_ACCESS_KEY_ID' },
  { name: 'AWS_SECRET_ACCESS_KEY' },
];

function getFromJson() {
  try {
    return JSON.parse(fs.readFileSync('/secrets/prod.json', { encoding: 'utf-8' }));
  } catch (err) {
    throw new Error('Could not read secrets file "prod.json"');
  }
}

function getFromProcess() {
  return variables
    .filter( // eslint-disable-next-line
      variable => variable.env ? variable.env.find(env => /^development|test$/.test(env)) : true
    )
    .reduce((object, variable) => {
      const value = process.env[variable.name];
      object[variable.name] = value;

      return object;
    }, {});
}

const env = process.env.NODE_ENV;
let secrets;

switch (env) {
  case 'development':
  case 'test':
    secrets = getFromProcess();
    break;
  case 'production':
    secrets = getFromJson();
    break;
  default:
    throw new Error('Environment variable "NODE_ENV" is undefined or invalid');
}

module.exports = secrets;<commit_msg>chore(secrets): throw error if secret is missing<commit_after>'use strict';

const fs = require('fs');

const variables = [
  { name: 'NTB_API_KEY' },
  { name: 'NEW_RELIC_LICENSE_KEY', env: ['production'] },
  { name: 'SENTRY_DSN', env: ['production'] },
  { name: 'OAUTH_CLIENT_ID', env: ['development', 'test'] },
  { name: 'OAUTH_CLIENT_SECRET', env: ['development', 'test'] },
  { name: 'OAUTH_ACCESS_TOKEN', env: ['development', 'test'] },
  { name: 'OAUTH_REFRESH_TOKEN', env: ['development', 'test'] },
  { name: 'OAUTH_USER_ID', env: ['development', 'test'] },
  { name: 'AWS_ACCESS_KEY_ID' },
  { name: 'AWS_SECRET_ACCESS_KEY' },
];

function getFromJson() {
  try {
    return JSON.parse(fs.readFileSync('/secrets/prod.json', { encoding: 'utf-8' }));
  } catch (err) {
    throw new Error('Could not read secrets file "prod.json"');
  }
}

function getFromProcess() {
  return variables
    .filter( // eslint-disable-next-line
      variable => variable.env ? variable.env.find(env => /^development|test$/.test(env)) : true
    )
    .reduce((object, variable) => {
      const value = process.env[variable.name];
      object[variable.name] = value;

      return object;
    }, {});
}

let secrets;

switch (process.env.NODE_ENV) {
  case 'development':
  case 'test':
    secrets = getFromProcess();
    break;
  case 'production':
    secrets = getFromJson();
    break;
  default:
    throw new Error('Environment variable "NODE_ENV" is undefined or invalid');
}

variables
  .filter(
    variable => variable.env ? variable.env.find(
      env => (new RegExp(process.env.NODE_ENV).test(env))
    ) : true
  )
  .forEach(variable => {
    if (typeof secrets[variable.name] === 'undefined') {
      throw new Error(`Environvent variable ${variable.name} is missing`);
    }
  });

module.exports = secrets;
{'fraction_non_alphanumeric': 0.17673716012084592, 'fraction_numerical': 0.0006042296072507553, 'mean_word_length': 3.805515239477504, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5781317', 'n_tokens_mistral': 1130, 'n_tokens_neox': 1124, 'n_words': 251}