From 3cf0397da832ace07e701c993bd6535ed88d2120 Mon Sep 17 00:00:00 2001 From: Jon Winton Date: Wed, 12 Jul 2017 18:57:42 -0400 Subject: [PATCH 1/4] Adding env var registering --- lib/setup.js | 4 +++- lib/setup.test.js | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/setup.js b/lib/setup.js index 8da068ba..e7353bc3 100644 --- a/lib/setup.js +++ b/lib/setup.js @@ -22,7 +22,7 @@ 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 @@ -30,6 +30,7 @@ module.exports = function (options) { 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) { @@ -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 diff --git a/lib/setup.test.js b/lib/setup.test.js index aa9aeb64..b9c4aca4 100644 --- a/lib/setup.test.js +++ b/lib/setup.test.js @@ -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 () { @@ -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); + }); + }); }); From 05fa6289358bdf0e4c96d9b55c38ee6ab1e0d57c Mon Sep 17 00:00:00 2001 From: Jon Winton Date: Wed, 12 Jul 2017 18:58:01 -0400 Subject: [PATCH 2/4] Sending env data in render mode --- lib/render.js | 42 +++++++++++++++++++++++++++++++++++++++--- lib/render.test.js | 18 ++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/lib/render.js b/lib/render.js index 921bd03e..1b900bca 100644 --- a/lib/render.js +++ b/lib/render.js @@ -1,6 +1,6 @@ 'use strict'; -var uriRoutes, renderers; +var uriRoutes, renderers, envVars; const _ = require('lodash'), media = require('./media'), schema = require('./schema'), @@ -37,6 +37,18 @@ function registerRenderers(renderObj) { module.exports.renderers = renderers; } +/** + * Register env variables to be referenced + * + * @param {Array} envArray + */ +function registerEnv(envArray) { + envVars = envArray; + // Export the renderers object + module.exports.envVars = envArray; + console.log('!!!!!!', module.exports.envVars); +} + /** * * @param {string} ref @@ -65,6 +77,20 @@ function transfer(from, to, fromProp, toProp) { } } +/** + * [resolveEnvVars description] + * @return {[type]} [description] + */ +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 @@ -73,7 +99,7 @@ function transfer(from, to, fromProp, toProp) { */ function applyOptions(options, locals) { return function (result) { - let state, { site } = locals, componentList; + let state, { site } = locals, componentList, envVars; // Initial state object which is the response payload state = { @@ -81,6 +107,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 = 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. @@ -92,7 +124,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); @@ -397,9 +430,12 @@ 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.resolveEnvVars = resolveEnvVars; module.exports.resetUriRouteHandlers = resetUriRouteHandlers; module.exports.setUriRouteHandlers = setUriRouteHandlers; module.exports.assumePublishedUnlessEditing = assumePublishedUnlessEditing; diff --git a/lib/render.test.js b/lib/render.test.js index af2fb3f8..2cfd5d21 100644 --- a/lib/render.test.js +++ b/lib/render.test.js @@ -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: { @@ -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]; From fac3564b8c81be242de1d2390d4fdd601f036d7f Mon Sep 17 00:00:00 2001 From: Jon Winton Date: Wed, 12 Jul 2017 19:02:49 -0400 Subject: [PATCH 3/4] memoize call; remove console.log --- lib/render.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/render.js b/lib/render.js index 1b900bca..8fc7670f 100644 --- a/lib/render.js +++ b/lib/render.js @@ -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 @@ -46,7 +47,6 @@ function registerEnv(envArray) { envVars = envArray; // Export the renderers object module.exports.envVars = envArray; - console.log('!!!!!!', module.exports.envVars); } /** @@ -78,8 +78,10 @@ function transfer(from, to, fromProp, toProp) { } /** - * [resolveEnvVars description] - * @return {[type]} [description] + * Look through env variables array and return + * the variables from the `process.env` bject + * + * @return {Object} */ function resolveEnvVars() { var obj = {}; @@ -110,7 +112,7 @@ function applyOptions(options, 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 = resolveEnvVars(); + state._envVars = module.exports.resolveEnvVars(); } // Get an array of components that are included in the request. This @@ -421,6 +423,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; @@ -435,7 +438,6 @@ module.exports.registerRenderers = registerRenderers; module.exports.registerEnv = registerEnv; // For Testing -module.exports.resolveEnvVars = resolveEnvVars; module.exports.resetUriRouteHandlers = resetUriRouteHandlers; module.exports.setUriRouteHandlers = setUriRouteHandlers; module.exports.assumePublishedUnlessEditing = assumePublishedUnlessEditing; From 3a86a4254437e3d8209dd65807f640485cb92d07 Mon Sep 17 00:00:00 2001 From: Jon Winton Date: Thu, 13 Jul 2017 09:55:02 -0400 Subject: [PATCH 4/4] Fixing lint issues --- lib/render.js | 5 ++--- lib/render.test.js | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/render.js b/lib/render.js index 8fc7670f..2a4b099a 100644 --- a/lib/render.js +++ b/lib/render.js @@ -1,6 +1,6 @@ 'use strict'; -var uriRoutes, renderers, envVars; +var uriRoutes, renderers; const _ = require('lodash'), media = require('./media'), schema = require('./schema'), @@ -44,7 +44,6 @@ function registerRenderers(renderObj) { * @param {Array} envArray */ function registerEnv(envArray) { - envVars = envArray; // Export the renderers object module.exports.envVars = envArray; } @@ -101,7 +100,7 @@ function resolveEnvVars() { */ function applyOptions(options, locals) { return function (result) { - let state, { site } = locals, componentList, envVars; + let state, { site } = locals, componentList; // Initial state object which is the response payload state = { diff --git a/lib/render.test.js b/lib/render.test.js index 2cfd5d21..93ea3daa 100644 --- a/lib/render.test.js +++ b/lib/render.test.js @@ -384,8 +384,8 @@ describe(_.startCase(filename), function () { callback = fn({}, locals), returnVal = callback({}); - sandbox.stub(lib, 'resolveEnvVars'); - expect(returnVal._envVars).to.eql({ foo: undefined, bar: undefined }) + sandbox.stub(lib, 'resolveEnvVars'); + expect(returnVal._envVars).to.eql({ foo: undefined, bar: undefined }); }); });