diff --git a/index.js b/index.js index bf577c2..7d65cb8 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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 @@ -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')) @@ -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 @@ -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 diff --git a/test/configs/custom/custom-config.yml b/test/configs/custom/custom-config.yml new file mode 100644 index 0000000..e79a79b --- /dev/null +++ b/test/configs/custom/custom-config.yml @@ -0,0 +1,12 @@ + +var1: val1 +var2: val2 + +obj1: + var1: objval1 + var2: objval2 + + +list1: + - listval1 + - listval2 diff --git a/test/configs/custom/env.yml b/test/configs/custom/env.yml new file mode 100644 index 0000000..540901f --- /dev/null +++ b/test/configs/custom/env.yml @@ -0,0 +1,14 @@ + +lower: ${envId} +upper: ${ENVID} + +obj1: + obj2: + lower: ${envId} + upper: ${ENVID} + +setting1: settingVal + +list1: + - ${envId} + - ${ENVID} diff --git a/test/envFromFileLoad.js b/test/envFromFileLoad.js new file mode 100644 index 0000000..b2c4499 --- /dev/null +++ b/test/envFromFileLoad.js @@ -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()) + }) +}) diff --git a/test/loadFromFile.js b/test/loadFromFile.js new file mode 100644 index 0000000..7f7123e --- /dev/null +++ b/test/loadFromFile.js @@ -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') + }) +})