Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable custom location for the config file (#17) #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const yaml = require('js-yaml')
const moment = require('moment')
const args = require('yargs').argv
const timestamp = moment().format('YYYYMMDDHHmmss')
const defaultFile = 'config.yml'
let multiFile = false
let envId
let ENVID
Expand All @@ -17,8 +18,8 @@ let environmentTypes
let environments
let config

function load (env) {
config = loadConfig()
function load (env, file = defaultFile) {
config = loadConfig(file)
environments = config.environments || {}
envId = getEnvId(config, env)
ENVID = envId ? envId.toUpperCase() : undefined
Expand All @@ -28,6 +29,10 @@ function load (env) {
return config
}

function loadFromFile (file, env = undefined) {
return load(env, file)
}

function loadConfigFile (file) {
try {
return yaml.load(fs.readFileSync(file, 'utf8'))
Expand All @@ -39,9 +44,9 @@ function loadConfigFile (file) {
}
}

function loadConfig () {
if (fs.existsSync('config.yml')) {
return loadConfigFile('config.yml')
function loadConfig (file) {
if (fs.existsSync(file)) {
return loadConfigFile(file)
} else {
let templ = {}
multiFile = true
Expand Down Expand Up @@ -174,4 +179,5 @@ function swapVariables (configFile) {
module.exports = load()
module.exports.load = load
module.exports.log = log
module.exports.loadFromFile = loadFromFile
module.exports.require = requireSettings
12 changes: 12 additions & 0 deletions test/configs/custom/custom-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

var1: val1
var2: val2

obj1:
var1: objval1
var2: objval2


list1:
- listval1
- listval2
14 changes: 14 additions & 0 deletions test/configs/custom/env.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

lower: ${envId}
upper: ${ENVID}

obj1:
obj2:
lower: ${envId}
upper: ${ENVID}

setting1: settingVal

list1:
- ${envId}
- ${ENVID}
28 changes: 28 additions & 0 deletions test/envFromFileLoad.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict'

require('should')
const path = require('path')

describe('Config env from load', function () {
let config, env

before(function () {
env = 'loadenv'
config = require('../').loadFromFile(path.join(__dirname, 'configs/custom', 'env.yml'), env)
})

it('should have env variables at top', function () {
config.lower.should.equal(env)
config.upper.should.equal(env.toUpperCase())
})

it('should have env variables in obj', function () {
config.obj1.obj2.lower.should.equal(env)
config.obj1.obj2.upper.should.equal(env.toUpperCase())
})

it('should have env variables in obj', function () {
config.list1[0].should.equal(env)
config.list1[1].should.equal(env.toUpperCase())
})
})
27 changes: 27 additions & 0 deletions test/loadFromFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict'

require('should')
const path = require('path')

describe('Config file provided', function () {
var config

before(function () {
config = require('../').loadFromFile(path.join(__dirname, 'configs/custom', 'custom-config.yml'))
})

it('should have top variables', function () {
config.var1.should.equal('val1')
config.var2.should.equal('val2')
})

it('should have obj variables', function () {
config.obj1.var1.should.equal('objval1')
config.obj1.var2.should.equal('objval2')
})

it('should have obj variables', function () {
config.list1[0].should.equal('listval1')
config.list1[1].should.equal('listval2')
})
})