Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Commit

Permalink
feat(env): added env to options object param to allow customization…
Browse files Browse the repository at this point in the history
… of env() loading
  • Loading branch information
Blake Edwards committed Dec 13, 2016
1 parent eb36048 commit 2a9dcd0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
18 changes: 12 additions & 6 deletions README.md
Expand Up @@ -36,8 +36,7 @@ in a single config file (obey 12factor and unify your config location), do somet
// in ./app/lib/config.js
var config = require('12factor-dotenv');
var cfg = config({
var schema = {
DEBUG: {
env: 'DEBUG', // the environment export var to read
type: 'boolean', // config var type (string, integer, boolean - maybe more see 12factor-config)
Expand All @@ -57,14 +56,21 @@ var cfg = config({
type: 'string',
default: 'development'
}
}, { debug: true });
};
var cfg = config(schema, { debug: true, env: { overwrite: true } });
## See https://www.npmjs.com/package/node-env-file#api for more details on `env` options
console.log('debug: -- Debug is', process.env.DEBUG);
console.log('debug: -- Port is', process.env.PORT);
console.log('info: -- PORT is', cfg.PORT);
console.log('info: -- NODE_ENV is', cfg.NODE_ENV);
console.log('debug: -- ENV Debug is', process.env.DEBUG);
console.log('debug: -- ENV Port is', process.env.PORT);
console.log('info: < Configured.');
module.exports = cfg;
module.exports = exports = cfg;
```

Expand Down
6 changes: 3 additions & 3 deletions lib/config-schema.js
Expand Up @@ -15,15 +15,15 @@ var parent_dirname = path.dirname(module.parent.parent.filename);
var ConfigSchema = function(schema, opts) {
this.schema = schema;
opts = opts ? opts : {};
this.opts = _.merge({ logger: console, debug: false }, opts);
this.opts = _.merge({ logger: console, debug: false, env: { overwrite: true } }, opts);

this.debugEnabled = function() {
return this.opts.debug ? this.opts.debug : false;
};

if (this.opts.debug) {
this.notify = this.opts.logger;
this.notify.log(' 12factor-dotenv:\t', 'DEBUG '+this.opts.debug);
this.notify.log('12factor-dotenv:\t', 'DEBUG ' + this.opts.debug);
} else {
this.notify = {
log: function() {},
Expand Down Expand Up @@ -51,7 +51,7 @@ var ConfigSchema = function(schema, opts) {
var envFile = path.resolve(path.join(parent_dirname, relative_path, '.env'));
if (fs.existsSync(envFile)) {
this.notify.log(' 12factor-dotenv:\t', 'loading.. '+envFile);
env(envFile, { overwrite: true});
env(envFile, this.opts.env);
} else {
this.notify.log(' 12factor-dotenv:\t', 'skipped.. '+envFile);
}
Expand Down
12 changes: 8 additions & 4 deletions test/test.js
Expand Up @@ -23,11 +23,15 @@ var testSchema = {
}
};

var testENV = ["DEBUG=true", "PORT=5000", "URL=http://github.com/electblake/node-12factor-dotenv"].join("\n");
fs.writeFileSync('.env', testENV);
thisConfig = new Config(testSchema, { debug: true, logger: console });
const thisConfig = new Config(testSchema, { debug: false, logger: console, env: { overwrite: true } });

describe('12factor-dotenv', function(){

before(function() {
var testENV = ["DEBUG=true", "PORT=5000", "URL=http://github.com/electblake/node-12factor-dotenv"].join("\n");
fs.writeFileSync('.env', testENV);
})

describe('cfg', function(){
describe('should have property DEBUG boolean', function() {

it('should be boolean', function(){
Expand Down

0 comments on commit 2a9dcd0

Please sign in to comment.