Skip to content

Commit

Permalink
Environment Variables (#421)
Browse files Browse the repository at this point in the history
* Adding env var registering

* Sending env data in render mode

* memoize call; remove console.log

* Fixing lint issues
  • Loading branch information
jonwinton authored Jul 13, 2017
1 parent b944002 commit 5d566c8
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
41 changes: 39 additions & 2 deletions lib/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const _ = require('lodash'),
db = require('./services/db'),
log = require('./services/log').withStandardPrefix(__filename),
composer = require('./services/composer'),
mapLayoutToPageData = require('./utils/layout-to-page-data');
mapLayoutToPageData = require('./utils/layout-to-page-data'),
control = require('./control');

/**
* Check the renderers for the request extension. If the
Expand All @@ -37,6 +38,16 @@ function registerRenderers(renderObj) {
module.exports.renderers = renderers;
}

/**
* Register env variables to be referenced
*
* @param {Array} envArray
*/
function registerEnv(envArray) {
// Export the renderers object
module.exports.envVars = envArray;
}

/**
*
* @param {string} ref
Expand Down Expand Up @@ -65,6 +76,22 @@ function transfer(from, to, fromProp, toProp) {
}
}

/**
* Look through env variables array and return
* the variables from the `process.env` bject
*
* @return {Object}
*/
function resolveEnvVars() {
var obj = {};

_.map(module.exports.envVars, function (val) {
obj[val] = process.env[val];
});

return obj;
}

/**
* Add state to result, which adds functionality and information about this request to the templates.
* @param {object} options
Expand All @@ -81,6 +108,12 @@ function applyOptions(options, locals) {
locals
};

// If we're in edit mode then we want to send across the
// environment variables that model.js files might need.
if (locals.edit) {
state._envVars = module.exports.resolveEnvVars();
}

// Get an array of components that are included in the request. This
// includes the root component (layout or other) that is at the base
// of the request and its children.
Expand All @@ -92,7 +125,8 @@ function applyOptions(options, locals) {
transfer(options, state, 'pageRef', '_self');
transfer(options, state, 'pageData', '_pageData');
transfer(options, state, 'version', '_version');
transfer(options, state, 'layoutRef', '_layoutRef'); // TODO: collapse layoutRef and pageRef into the same prop name?
transfer(options, state, 'layoutRef', '_layoutRef');


// Add the list of components to the payload
_.set(state, '_components', componentList);
Expand Down Expand Up @@ -388,6 +422,7 @@ module.exports.renderPage = renderPage;
module.exports.renderUri = renderUri;
module.exports.formDataForRenderer = formDataForRenderer;
module.exports.applyOptions = applyOptions;
module.exports.resolveEnvVars = control.memoize(resolveEnvVars);

// Render Utils
module.exports.rendererExists = rendererExists;
Expand All @@ -397,7 +432,9 @@ module.exports.transfer = transfer;

// Setup
module.exports.renderers = {}; // Overriden when a user registers a render object
module.exports.envVars = [];
module.exports.registerRenderers = registerRenderers;
module.exports.registerEnv = registerEnv;

// For Testing
module.exports.resetUriRouteHandlers = resetUriRouteHandlers;
Expand Down
18 changes: 18 additions & 0 deletions lib/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ describe(_.startCase(filename), function () {
describe('applyOptions', function () {
const fn = lib[this.title];

lib.registerEnv(['foo', 'bar']);

it('consolidates all request information to be sent to the rendererer', function () {
const locals = {
site: {
Expand All @@ -371,8 +373,24 @@ describe(_.startCase(filename), function () {
expect(returnVal._media).to.have.property('styles');
expect(returnVal._media).to.have.property('styles');
});

it('calls `resolveEnvVars` if the request is for edit mode', function () {
const locals = {
site: {
slug: 'site'
},
edit: 'true'
},
callback = fn({}, locals),
returnVal = callback({});

sandbox.stub(lib, 'resolveEnvVars');
expect(returnVal._envVars).to.eql({ foo: undefined, bar: undefined });
});
});



describe('transfer', function () {
const fn = lib[this.title];

Expand Down
4 changes: 3 additions & 1 deletion lib/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ bluebird.config({
* @returns {Promise}
*/
module.exports = function (options) {
let app, providers, router, sessionStore, renderers, appPlugins;
let app, providers, router, sessionStore, renderers, appPlugins, env;

options = options || {};
app = options.app || express(); // use new express app if not passed in
providers = options.providers || [];
sessionStore = options.sessionStore;
renderers = options.renderers; // TODO: DOCUMENT THIS
appPlugins = options.plugins; // TODO: DOCUMENT THIS
env = options.env || []; // TODO: DOCUMENT THIS

// Init plugins
if (appPlugins) {
Expand All @@ -42,6 +43,7 @@ module.exports = function (options) {
// if engines were passed in, send them to the renderer
if (renderers) {
render.registerRenderers(renderers);
render.registerEnv(env);
}

// look for bootstraps in components
Expand Down
7 changes: 7 additions & 0 deletions lib/setup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe(_.startCase(filename), function () {
sandbox = sinon.sandbox.create();
sandbox.stub(plugins, 'registerPlugins');
sandbox.stub(render, 'registerRenderers');
sandbox.stub(render, 'registerEnv');
});

afterEach(function () {
Expand Down Expand Up @@ -49,4 +50,10 @@ describe(_.startCase(filename), function () {
sinon.assert.calledOnce(render.registerRenderers);
});
});

it('registers env vars', function () {
return lib({ renderers: { default: 'html', html: _.noop } }).then(function () {
sinon.assert.calledOnce(render.registerEnv);
});
});
});

0 comments on commit 5d566c8

Please sign in to comment.