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

Array supports, fixes #6 #20

Merged
merged 13 commits into from
Aug 23, 2016
53 changes: 49 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
common-env
==========

[![Build Status](https://img.shields.io/circleci/project/FGRibreau/common-env.svg)](https://circleci.com/gh/FGRibreau/common-env/)
[![Deps](https://img.shields.io/david/FGRibreau/common-env.svg)](https://david-dm.org/FGRibreau/common-env)
[![NPM version](https://img.shields.io/npm/v/common-env.svg)](http://badge.fury.io/js/common-env) [![Downloads](http://img.shields.io/npm/dm/common-env.svg)](https://www.npmjs.com/package/common-env) ![extra](https://img.shields.io/badge/actively%20maintained-yes-ff69b4.svg) [![Twitter Follow](https://img.shields.io/twitter/follow/fgribreau.svg?style=flat)](https://twitter.com/FGRibreau)
[![Build Status](https://img.shields.io/circleci/project/FGRibreau/common-env.svg)](https://circleci.com/gh/FGRibreau/common-env/) [![Coverage Status](https://img.shields.io/coveralls/FGRibreau/common-env/master.svg)](https://coveralls.io/github/FGRibreau/common-env?branch=master) [![Deps]( https://img.shields.io/david/FGRibreau/common-env.svg)](https://david-dm.org/FGRibreau/common-env) [![NPM version](https://img.shields.io/npm/v/common-env.svg)](http://badge.fury.io/js/common-env) [![Downloads](http://img.shields.io/npm/dm/common-env.svg)](https://www.npmjs.com/package/common-env) ![extra](https://img.shields.io/badge/actively%20maintained-yes-ff69b4.svg)

A little helper I use everywhere for configuration. [Environment variables](http://blog.honeybadger.io/ruby-guide-environment-variables/) are a really great way to quickly change a program behavior.
A library I use everywhere for configuration. [Environment variables](http://blog.honeybadger.io/ruby-guide-environment-variables/) are a really great way to quickly change a program behavior.

# Philosophy

Expand Down Expand Up @@ -137,6 +135,48 @@ It's sometimes useful to be able to specify aliases, for instance [Clever-cloud]

Common-env adds a [layer of indirection](http://en.wikipedia.org/wiki/Fundamental_theorem_of_software_engineering) enabling you to specify environment aliases that won't impact your codebase.

#### How to handle environment variable arrays

Since **v6**, common-env is able to read arrays from environment variables. Before going further, please don't forget that **environment variables do not support arrays**, thus `MY_ENV_VAR[0]_A` is not a valid environment variable name, as well as `MY_ENV_VAR$0$_A` and so on. In fact, the only supported characters are `[0-9_]`. But since we wanted **a lot** array support [we had to find a work-around](https://github.com/FGRibreau/common-env/issues/6).

And here is what we did:

| Configuration key path | Generated environment key |
|---|---|
| amqp.exchanges[0].name | AMQP_EXCHANGES__0_NAME |
| amqp.exchanges[10].name | AMQP_EXCHANGES__10_NAME |

As you can see, we a replacing `[0]`, with `__0` and thus common-env is compliant with the limited character support while providing an awesome abstraction for configuration through environment variables.

Note that **only the first element** of the array will be used as a **description** for every other element of the array. So in the following code:

```js
const config = env.getOrElseAll({
mysql: {
hosts: [{
host: '127.0.0.1',
port: 3306
}, {
auth: {
$type: env.types.String
$secure: true
}
}]
}
});
```

only the first object

`{
host: '127.0.0.1',
port: 3306
}`

will be used as a *type* template for every defined elements.

One last thing, common-env is smart enough to build plain arrays (not sparse), so if you defined `MYSQL_HOSTS__10_PORT=3310`, `config.mysql.hosts` will contains **10 objects** as you thought it would.

#### How to specify environment variable arrays

Common-env is able to use arrays as key values for instance:
Expand Down Expand Up @@ -166,6 +206,8 @@ $ AMQP_HOSTS='88.23.21.21,88.23.21.22,88.23.21.23' node test.js

#### How to specify environment variable arrays using $aliases

**Deprecated** aliases breaks common-env philosophy by allowing a developer to specify environment variables that matches outside constraints (like a company convention). Since a software internal configuration should not depends on external factors, this feature is now deprecated.

```javascript
// test.js
var env = require('common-env')();
Expand All @@ -192,6 +234,9 @@ $ LOCAL_RABBITMQ_HOSTS='88.23.21.21,88.23.21.22,88.23.21.23' node test.js
['88.23.21.21', '88.23.21.22', '88.23.21.23']
```

Aliases don't supports arrays in their names and never will. **$aliases is deprecated**, please use common-env classical form.


##### fail-fast behaviour

If `$default` is not defined and no environment variables (aliases included) resolve to a value then common-env will throw an error. This error should not be caught in order to make the app crash, following the [fail-fast](https://en.wikipedia.org/wiki/Fail-fast) principle.
Expand Down
10 changes: 10 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
machine:
node:
version: 6.3
environment:
COVERALLS_SERVICE_NAME: circleci

test:
override:
- npm run test
- npm run send-coverage
13 changes: 0 additions & 13 deletions lib/CommonEnvGetOrDieAliasesException.js

This file was deleted.

11 changes: 0 additions & 11 deletions lib/CommonEnvGetOrDieException.js

This file was deleted.

186 changes: 0 additions & 186 deletions lib/env.js

This file was deleted.

33 changes: 26 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,28 @@
"version": "5.4.0",
"keywords": [
"environment",
"configuration",
"process.env",
"env",
"var",
"getenv",
"variables"
],
"description": "A nice little helper for retrieving configuration from environment variable",
"main": "./lib",
"main": "./src/lib",
"directories": {
"doc": "docs",
"test": "test"
},
"engines": {
"node": "6"
},
"scripts": {
"test": "mocha test",
"test-watch": "mocha -R min -w test",
"test": "npm run test-unit && npm run test-coverage",
"test-unit": "mocha $(find src -name '*.test.js')",
"test-watch": "mocha -R min -w $(find src -name '*.test.js')",
"test-coverage": "nyc --all --statements=100 --lines=100 --functions=100 --branches=100 --check-coverage --reporter=lcov --reporter=cobertura --report-dir=coverage -- mocha -R spec -t 100000 $(find src -name '*.test.js')",
"send-coverage": "cat ./coverage/lcov.info | coveralls",
"check-build": "check-build",
"update": "updtr",
"changelog": "github-changes --o $(node -p 'process.env.npm_package_repository_url.split(\"/\")[3];') --r $(node -p 'a=process.env.npm_package_repository_url.split(\"/\");a[a.length-1].split(\".\")[0]') --token $CHANGELOG_GITHUB_TOKEN_FG -f CHANGELOG.md"
Expand All @@ -30,15 +38,26 @@
"bugs": {
"url": "https://github.com/FGRibreau/common-env/issues"
},
"nyc": {
"exclude": [
"node_modules",
"dist",
"coverage",
"webpack.config.js",
"**/**.test.js"
]
},
"homepage": "https://github.com/FGRibreau/common-env",
"devDependencies": {
"chai": "^3.5.0",
"check-build": "^2.8.2",
"github-changes": "^1.0.2",
"mocha": "^3.0.2"
"coveralls": "^2.11.12",
"github-changes": "^1.0.4",
"mocha": "^3.0.2",
"nyc": "^8.1.0",
"updtr": "^0.2.1"
},
"dependencies": {
"lodash": "^4.15.0",
"updtr": "^0.2.1"
"lodash": "^4.15.0"
}
}
6 changes: 3 additions & 3 deletions test/getOrDie.test.js → src/lib/env.getOrDie.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';

var t = require('chai').assert;
const t = require('chai').assert;

describe('.getOrDie', function () {
var env;
let env;

beforeEach(function () {
env = require('..')();
env = require('../..')();
});

it('should crash the app if the env. variable did not exist', function () {
Expand Down
Loading