Skip to content
This repository has been archived by the owner on Nov 3, 2022. It is now read-only.

Commit

Permalink
add first build
Browse files Browse the repository at this point in the history
  • Loading branch information
genediazjr committed Mar 6, 2017
1 parent 86c64bc commit 9729329
Show file tree
Hide file tree
Showing 2 changed files with 249 additions and 0 deletions.
46 changes: 46 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';


exports.register = (server, options, next) => {

if (!options.prefix) {

return next(new Error('Missing prefix'));
}

for (const envVars in process.env) {
if (envVars.indexOf(options.prefix) === 0) {

let appVar = {};
try {
appVar = JSON.parse(process.env[envVars]);
}
catch (e) {

return next(new Error(`Value unparsable: ${process.env[envVars]}`));
}

if (appVar.path && appVar.hasOwnProperty('value')) {
const appVarArr = appVar.path.split('.');
let tmp = server.app;

for (let i = 0; i < appVarArr.length; ++i) {
if (i === appVarArr.length - 1) {
tmp[appVarArr[i]] = appVar.value;
}
else {
tmp[appVarArr[i]] = tmp[appVarArr[i]] || {};
}
tmp = tmp[appVarArr[i]];
}
}
}
}

return next();
};


exports.register.attributes = {
pkg: require('../package.json')
};
203 changes: 203 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
'use strict';

const Rewire = require('rewire');
const Hapi = require('hapi');
const Code = require('code');
const Lab = require('lab');
const Plugin = Rewire('../');

const expect = Code.expect;
const lab = exports.lab = Lab.script();
const beforeEach = lab.beforeEach;
const describe = lab.describe;
const it = lab.it;


describe('registration and functionality', () => {

let server;

beforeEach((done) => {

server = new Hapi.Server();

return done();
});

const register = (options, next) => {

server.register({
register: Plugin,
options: options
}, (err) => {

return next(err);
});
};

it('registers', (done) => {

register({
prefix: 'FOO_VAR_'
}, (err) => {

expect(err).to.not.exist();

return done();
});
});

it('returns error if no option', (done) => {

register({}, (err) => {

expect(err).to.exist();
expect(err.message).to.equal('Missing prefix');

return done();
});
});

it('adds new server.app value', (done) => {

Plugin.__set__('process', {
env: {
FOO_VAR_test1: '{"path":"some.config.stuff","value":"Bazinga!"}'
}
});

register({
prefix: 'FOO_VAR_'
}, (err) => {

expect(err).to.not.exist();
expect(server.app).to.equal({ some: { config: { stuff: 'Bazinga!' } } });

return done();
});
});


it('adds only matching prefix', (done) => {

Plugin.__set__('process', {
env: {
FOO_VAR_test1: '{"path":"some.config.stuff","value":"Bazinga!"}',
SOME_VAR_test: 'nothingness'
}
});

register({
prefix: 'FOO_VAR_'
}, (err) => {

expect(err).to.not.exist();
expect(server.app).to.equal({ some: { config: { stuff: 'Bazinga!' } } });

return done();
});
});

it('returns error on unparsable value', (done) => {

Plugin.__set__('process', {
env: {
FOO_VAR_test1: '{"path":"some.config.stuff","value":"Bazinga!"}',
FOO_VAR_test2: 'not a stringified object'
}
});

register({
prefix: 'FOO_VAR_'
}, (err) => {

expect(err).to.exist();
expect(err.message).to.equal('Value unparsable: not a stringified object');

return done();
});
});

it('ignores if no value key', (done) => {

Plugin.__set__('process', {
env: {
FOO_VAR_test1: '{"path":"some.config.stuff","value":"Bazinga!"}',
FOO_VAR_test2: '{"path":"some","key": "test"}'
}
});

register({
prefix: 'FOO_VAR_'
}, (err) => {

expect(err).to.not.exist();
expect(server.app).to.equal({ some: { config: { stuff: 'Bazinga!' } } });

return done();
});
});

it('ignores if no path key', (done) => {

Plugin.__set__('process', {
env: {
FOO_VAR_test1: '{"path":"some.config.stuff","value":"Bazinga!"}',
FOO_VAR_test2: '{"value":"some","key": "test"}'
}
});

register({
prefix: 'FOO_VAR_'
}, (err) => {

expect(err).to.not.exist();
expect(server.app).to.equal({ some: { config: { stuff: 'Bazinga!' } } });

return done();
});
});

it('reuses existing objects in the path', (done) => {

Plugin.__set__('process', {
env: {
FOO_VAR_test1: '{"path":"some.config.stuff","value":"Bazinga!"}'
}
});

server.app.some = {};

register({
prefix: 'FOO_VAR_'
}, (err) => {

expect(err).to.not.exist();
expect(server.app).to.equal({ some: { config: { stuff: 'Bazinga!' } } });

return done();
});
});

it('overwrites existing value', (done) => {

Plugin.__set__('process', {
env: {
FOO_VAR_test1: '{"path":"some.config.stuff","value":"Bazinga!"}'
}
});

server.app.some = { config: { stuff: 'Spock!' } };
expect(server.app).to.equal({ some: { config: { stuff: 'Spock!' } } });

register({
prefix: 'FOO_VAR_'
}, (err) => {

expect(err).to.not.exist();
expect(server.app).to.equal({ some: { config: { stuff: 'Bazinga!' } } });

return done();
});
});
});

0 comments on commit 9729329

Please sign in to comment.