Skip to content

Commit

Permalink
Merge pull request #5 from ajuste/feature/read-file
Browse files Browse the repository at this point in the history
feat: read from file when available
  • Loading branch information
ajuste committed May 31, 2017
2 parents f6615ed + d1edebf commit 6cef7a1
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 9 deletions.
34 changes: 26 additions & 8 deletions lib/environment.js
Expand Up @@ -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";

Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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);
};
Expand All @@ -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;
};

Expand All @@ -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);
};


Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"author": "Alvaro Juste <juste.alvaro@gmail.com>",
"name": "jaune-env",
"version": "0.0.7",
"version": "0.0.8",
"description": "Singleton environment information / configuration",
"main": "index.js",
"scripts": {
Expand Down
23 changes: 23 additions & 0 deletions src/environment.coffee
Expand Up @@ -3,6 +3,14 @@
* @author Alvaro Juste
###
"use strict"
{
join
} = require 'path'

{
existsSync
readFileSync
} = require 'fs'

###*
* @constant Process property name for environment type
Expand All @@ -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, '_'

Expand Down
43 changes: 43 additions & 0 deletions 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"

0 comments on commit 6cef7a1

Please sign in to comment.