All files / app/config config.ts

81.82% Statements 27/33
45.83% Branches 11/24
100% Functions 0/0
81.82% Lines 27/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 1211x 1x 1x                     1x 1x     1x             1x 1x               1x 1x         1x           1x       1x         1x       1x 1x 1x                         1x 1x   1x 1x             1x           1x       1x                   1x         1x                   1x  
import path from 'path';
import debug from 'debug';
import {argv} from 'yargs';
import {
  AuthConfig,
  DBConfig,
  ErrorConfig,
  MorganConfig,
  StaticConfig,
  UserConfig,
  Config,
  MetricsConfig,
} from './config.d';
import _ from 'lodash';
import defaultConfig from './defaultConfig';
import {DeepPartial} from 'tsdef';
 
const log = debug('group-car:config');
 
/**
 * Get node environment.
 *
 * If none provided, assume development.
 */
const environment = process.env.NODE_ENV || 'development';
log('Environment: %s', environment);
 
/**
 * For which type of server this server is used:
 *  - development:  The development server
 *  - beta:         The beta server
 *  - release:      The release server, main server
 */
const serverType = process.env.SERVER_TYPE || 'development';
log('Server: %s', serverType);
 
/**
 * Initialize BcryptConfig with default value.
 */
const auth: AuthConfig = {
  saltRounds: 8,
};
/**
 * Initialize ErrorConfig with default value
 */
const error: ErrorConfig = {
  withStack: true,
};
 
const morgan: MorganConfig = {
  formatString: 'dev',
};
 
// Depending on node environment changes configs
Iif (environment === 'production') {
  auth.saltRounds = 10;
  error.withStack = false;
  morgan.formatString = 'common';
} else Eif (environment === 'test') {
  auth.saltRounds = 4;
  morgan.formatString = null;
}
 
/*
 * Set the path for serving static files depending on
 * which environment variable is provided.
 * Priority:
 * - HTML_STATIC
 * - npm_package_config_public (`config.public` in `package.json`)
 * - `'static'`
 */
 
let pathToStatic: string;
try {
  Iif (process.env.HTML_STATIC) {
    pathToStatic = path.resolve(process.env.HTML_STATIC);
  } else Eif (process.env.npm_package_config_public) {
    pathToStatic = path.resolve(process.env.npm_package_config_public);
  } else {
    pathToStatic = 'static';
  }
} catch (err) {
  pathToStatic = 'static';
}
const staticConfig: StaticConfig = {
  path: pathToStatic,
  disabled: argv.disableStaticServe ? true : false,
};
 
 
const database: Partial<DBConfig> = {
  withFlush: environment === 'test' ? true : Boolean(argv.flush),
};
 
const user: Partial<UserConfig> = {
  signUpThroughRequest: environment === 'test' ?
    false :
    argv.allowSignUp ?
    false :
    process.env.DISABLE_SIGN_UP_THROUGH_REQUEST === undefined ?
    true :
    !Boolean(process.env.DISABLE_SIGN_UP_THROUGH_REQUEST),
};
 
const metrics: Partial<MetricsConfig> = {
  enabled: environment !== 'test',
  dsn: process.env.SENTRY_DSN || 'https://7d4cc992f614416abcb1007107e12c16@o656739.ingest.sentry.io/5763203',
};
 
const config: DeepPartial<Config> = {
  user,
  database,
  static: staticConfig,
  auth,
  morgan,
  error,
  metrics,
};
 
export default _.merge(defaultConfig, config);