From d1edebf80e4542f29ce4a2ff65729335de40c810 Mon Sep 17 00:00:00 2001 From: Alvaro Juste Date: Tue, 30 May 2017 22:50:59 -0400 Subject: [PATCH] feat: read from file when available --- lib/environment.js | 34 ++++++++++++++++++++++------- package.json | 2 +- src/environment.coffee | 23 ++++++++++++++++++++ test/environment/ctr.coffee | 43 +++++++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 9 deletions(-) create mode 100644 test/environment/ctr.coffee diff --git a/lib/environment.js b/lib/environment.js index 93af54e..0305611 100644 --- a/lib/environment.js +++ b/lib/environment.js @@ -4,11 +4,16 @@ * @author Alvaro Juste */ "use strict"; +var ENV_TYPE, ENV_TYPE_DEV, ENV_TYPE_PROD, EnvFiles, Environment, MatchNonWord, existsSync, join, readFileSync, ref; + +join = require('path').join; + +ref = require('fs'), existsSync = ref.existsSync, readFileSync = ref.readFileSync; + /** * @constant Process property name for environment type */ -var ENV_TYPE, ENV_TYPE_DEV, ENV_TYPE_PROD, Environment, MatchNonWord; ENV_TYPE = "env"; @@ -28,9 +33,22 @@ ENV_TYPE_PROD = "prod"; MatchNonWord = /\W/g; +EnvFiles = ['.env', 'env', 'env.json', '.config', 'config', 'config.json']; + Environment = (function() { function Environment(config) { + var f, i, len; this.config = config; + if (this.config) { + return; + } + for (i = 0, len = EnvFiles.length; i < len; i++) { + f = EnvFiles[i]; + if (existsSync(f)) { + this.config = JSON.parse(readFileSync(join(process.cwd(), f))); + break; + } + } } Environment.prototype.transformKey = function(key) { @@ -56,7 +74,7 @@ Environment = (function() { */ Environment.prototype.getEnvProperty = function() { - var current, data, index, length, proc, property, ref, ref1, section, sections; + var current, data, index, length, proc, property, ref1, ref2, section, sections; section = arguments[0], property = arguments[1]; data = null; if (!section) { @@ -75,10 +93,10 @@ Environment = (function() { while (index < length && (data != null) || index === 0) { current = data != null ? data : this.config; section = sections[index++]; - data = (ref = current["" + section + this.envPost]) != null ? ref : current[section]; + data = (ref1 = current["" + section + this.envPost]) != null ? ref1 : current[section]; } if (arguments.length >= 2 && (data != null)) { - data = (ref1 = data["" + property + this.envPost]) != null ? ref1 : data[property]; + data = (ref2 = data["" + property + this.envPost]) != null ? ref2 : data[property]; } return this.parseValue(data); }; @@ -92,14 +110,14 @@ Environment = (function() { */ Environment.prototype.setProcessProperty = function(key, value) { - var ref; + var ref1; if ('string' !== typeof key) { throw new Error('key must be a string'); } if ('string' !== typeof value) { throw new Error('value must be a string'); } - ((ref = process.jaune) != null ? ref : (process.jaune = {}))[key] = value; + ((ref1 = process.jaune) != null ? ref1 : (process.jaune = {}))[key] = value; return this; }; @@ -111,11 +129,11 @@ Environment = (function() { */ Environment.prototype.getProcessProperty = function(key, def) { - var ref, ref1, ref2, ref3; + var ref1, ref2, ref3, ref4; if ('string' !== typeof key) { throw new Error('key must be a string'); } - return this.parseValue((ref = (ref1 = (ref2 = process.env[this.transformKey(key)]) != null ? ref2 : process.env[key]) != null ? ref1 : ((ref3 = process.jaune) != null ? ref3 : (process.jaune = {}))[key]) != null ? ref : def); + return this.parseValue((ref1 = (ref2 = (ref3 = process.env[this.transformKey(key)]) != null ? ref3 : process.env[key]) != null ? ref2 : ((ref4 = process.jaune) != null ? ref4 : (process.jaune = {}))[key]) != null ? ref1 : def); }; diff --git a/package.json b/package.json index d6dd64d..483271c 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "author": "Alvaro Juste ", "name": "jaune-env", - "version": "0.0.7", + "version": "0.0.8", "description": "Singleton environment information / configuration", "main": "index.js", "scripts": { diff --git a/src/environment.coffee b/src/environment.coffee index aba54d0..1514571 100644 --- a/src/environment.coffee +++ b/src/environment.coffee @@ -3,6 +3,14 @@ * @author Alvaro Juste ### "use strict" +{ + join +} = require 'path' + +{ + existsSync + readFileSync +} = require 'fs' ###* * @constant Process property name for environment type @@ -21,9 +29,24 @@ ENV_TYPE_PROD = "prod" MatchNonWord = /\W/g +EnvFiles = [ + '.env' + 'env' + 'env.json' + '.config' + 'config' + 'config.json' +] + class Environment constructor: (@config) -> + return if @config + + for f in EnvFiles + if existsSync f + @config = JSON.parse readFileSync join process.cwd(), f + break transformKey: (key) -> key.replace MatchNonWord, '_' diff --git a/test/environment/ctr.coffee b/test/environment/ctr.coffee new file mode 100644 index 0000000..4b848b3 --- /dev/null +++ b/test/environment/ctr.coffee @@ -0,0 +1,43 @@ +lib = require '../../' +{ + equal + deepEqual +} = require 'assert' + +{ + unlinkSync + writeFileSync +} = require 'fs' + +{ + join +} = require 'path' + +EnvFiles = [ + '.env' + 'env' + 'env.json' + '.config' + 'config' + 'config.json' +] + +describe 'environment', -> + + describe 'constructor', -> + + EnvFiles.forEach (f) -> + + context "using #{f} as config file", -> + + before -> + writeFileSync join(process.cwd(), f), + JSON.stringify val: "this is #{f} file config" + @env = new lib() + + after -> + unlinkSync f + @env = null + + it "should use #{f} config file", -> + deepEqual @env.config, val: "this is #{f} file config"