2021-07-03 13:56:53 +00:00
|
|
|
import fs from 'fs'
|
|
|
|
import path from 'path'
|
|
|
|
import { fileURLToPath } from 'url'
|
|
|
|
import Nconf from 'nconf-lite'
|
|
|
|
|
|
|
|
const nconf = new Nconf()
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
|
|
|
let pckg = JSON.parse(fs.readFileSync(path.resolve(path.join(__dirname, '../package.json'))))
|
2017-12-09 21:51:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Helper method for global usage.
|
|
|
|
nconf.inTest = () => nconf.get('NODE_ENV') === 'test'
|
|
|
|
|
|
|
|
// Config follow the following priority check order:
|
|
|
|
// 1. package.json
|
|
|
|
// 2. Enviroment variables
|
|
|
|
// 3. config/config.json
|
|
|
|
// 4. config/config.default.json
|
|
|
|
|
|
|
|
|
2017-12-09 22:55:25 +00:00
|
|
|
pckg = {
|
|
|
|
name: pckg.name,
|
|
|
|
version: pckg.version,
|
|
|
|
description: pckg.description,
|
|
|
|
author: pckg.author,
|
|
|
|
license: pckg.license,
|
|
|
|
homepage: pckg.homepage,
|
|
|
|
}
|
2017-12-09 21:51:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Load overrides as first priority
|
|
|
|
nconf.overrides(pckg)
|
|
|
|
|
|
|
|
|
|
|
|
// Load enviroment variables as second priority
|
|
|
|
nconf.env()
|
|
|
|
|
|
|
|
|
|
|
|
// Load any overrides from the appropriate config file
|
2021-07-03 13:56:53 +00:00
|
|
|
let configFile = '../config/config.json'
|
2017-12-09 21:51:18 +00:00
|
|
|
|
|
|
|
/* istanbul ignore else */
|
|
|
|
if (nconf.get('NODE_ENV') === 'test') {
|
2021-07-03 13:56:53 +00:00
|
|
|
configFile = '../config/config.test.json'
|
2017-12-09 21:51:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* istanbul ignore if */
|
|
|
|
if (nconf.get('NODE_ENV') === 'production') {
|
2021-07-03 13:56:53 +00:00
|
|
|
configFile = '../config/config.production.json'
|
2017-12-09 21:51:18 +00:00
|
|
|
}
|
|
|
|
|
2021-07-03 13:56:53 +00:00
|
|
|
nconf.file('main', path.resolve(path.join(__dirname, configFile)))
|
2017-12-09 21:51:18 +00:00
|
|
|
|
|
|
|
// Load defaults
|
2021-07-03 13:56:53 +00:00
|
|
|
nconf.file('default', path.resolve(path.join(__dirname, '../config/config.default.json')))
|
2017-12-09 21:51:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Final sanity checks
|
|
|
|
/* istanbul ignore if */
|
|
|
|
if (typeof global.it === 'function' & !nconf.inTest()) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log('Critical: potentially running test on production enviroment. Shutting down.')
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-03 13:56:53 +00:00
|
|
|
export default nconf
|