Skip to content

Commit

Permalink
Centralize access to environment name (#9162)
Browse files Browse the repository at this point in the history
* Move site/stages/constants to parent directory

* Add env constants into Webpack config

* Consume env constants in platform code

* Add JSDoc

* Cleaning up

* More clean up

* Fix hardcoded values

* Replace production-lookups

* Move buildtype back to globals

* replace buildtype instances with helper functions

* Check BUILDTYPE as bound to environmentx

* Update module name

* Call toString on port

* Attempt to fix failing claims tests

* Fix for incorrect use of object.assign

* Tear down broken process.env from webpack

* Remove port 3001 from test-server

* Allow hostname

* remove preview from buckets

* Add JSDoc to environments

* Add more JSDoc commments

* Switch node env references to use constants

* Fix unit test

* Add more comments, per PR feedback

* Remove buildtype mutations from unit test

* Check for port in localhost config

* Remove preview from hostnames
  • Loading branch information
ncksllvn committed Nov 29, 2018
1 parent 38708ed commit 1038000
Show file tree
Hide file tree
Showing 33 changed files with 174 additions and 168 deletions.
1 change: 0 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"jest": true,
},
"globals": {
"__BUILDTYPE__": true,
"PerformanceObserver": true
},
"rules": {
Expand Down
53 changes: 18 additions & 35 deletions config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const BundleAnalyzerPlugin = require('webpack-bundle-analyzer')
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
const webpack = require('webpack');
const path = require('path');
const ENVIRONMENTS = require('../src/site/constants/environments');
const BUCKETS = require('../src/site/constants/buckets');

require('babel-polyfill');

Expand Down Expand Up @@ -34,19 +36,23 @@ const globalEntryFiles = {
],
};

const configGenerator = (options, apps) => {
const isDev = ['localhost', 'vagovdev'].includes(options.buildtype);
const configGenerator = (buildOptions, apps) => {
const entryFiles = Object.assign({}, apps, globalEntryFiles);
const isOptimizedBuild = [
ENVIRONMENTS.VAGOVSTAGING,
ENVIRONMENTS.VAGOVPROD,
].includes(buildOptions.buildtype);

const baseConfig = {
mode: 'development',
entry: entryFiles,
output: {
path: `${options.destination}/generated`,
path: `${buildOptions.destination}/generated`,
publicPath: '/generated/',
filename: isDev
filename: !isOptimizedBuild
? '[name].entry.js'
: `[name].entry.[chunkhash]-${timestamp}.js`,
chunkFilename: isDev
chunkFilename: !isOptimizedBuild
? '[name].entry.js'
: `[name].entry.[chunkhash]-${timestamp}.js`,
},
Expand Down Expand Up @@ -91,9 +97,7 @@ const configGenerator = (options, apps) => {
{
loader: 'css-loader',
options: {
minimize: ['vagovprod', 'vagovstaging'].includes(
options.buildtype,
),
minimize: isOptimizedBuild,
},
},
{ loader: 'sass-loader' },
Expand Down Expand Up @@ -175,21 +179,11 @@ const configGenerator = (options, apps) => {
},
plugins: [
new webpack.DefinePlugin({
__BUILDTYPE__: JSON.stringify(options.buildtype),
'process.env': {
API_PORT: process.env.API_PORT || 3000,
WEB_PORT: process.env.WEB_PORT || 3333,
API_URL: process.env.API_URL
? JSON.stringify(process.env.API_URL)
: null,
BASE_URL: process.env.BASE_URL
? JSON.stringify(process.env.BASE_URL)
: null,
},
__BUILDTYPE__: JSON.stringify(buildOptions.buildtype),
}),

new ExtractTextPlugin({
filename: isDev
filename: !isOptimizedBuild
? '[name].css'
: `[name].[contenthash]-${timestamp}.css`,
}),
Expand All @@ -200,23 +194,12 @@ const configGenerator = (options, apps) => {
],
};

if (['vagovstaging', 'vagovprod'].includes(options.buildtype)) {
let sourceMap = null;

switch (options.buildtype) {
case 'vagovstaging':
sourceMap = 'https://s3-us-gov-west-1.amazonaws.com/staging.va.gov';
break;

case 'vagovprod':
default:
sourceMap = 'https://s3-us-gov-west-1.amazonaws.com/www.va.gov';
break;
}
if (isOptimizedBuild) {
const bucket = BUCKETS[buildOptions.buildtype];

baseConfig.plugins.push(
new webpack.SourceMapDevToolPlugin({
append: `\n//# sourceMappingURL=${sourceMap}/generated/[url]`,
append: `\n//# sourceMappingURL=${bucket}/generated/[url]`,
filename: '[file].map',
}),
);
Expand All @@ -227,7 +210,7 @@ const configGenerator = (options, apps) => {
baseConfig.devtool = '#eval-source-map';
}

if (options.analyzer) {
if (buildOptions.analyzer) {
baseConfig.plugins.push(
new BundleAnalyzerPlugin({
analyzerMode: 'disabled',
Expand Down
2 changes: 1 addition & 1 deletion script/run-docker-nightwatch.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

node src/platform/testing/e2e/test-server.js --buildtype=vagovprod --host=0.0.0.0 --port=3001 &
node src/platform/testing/e2e/test-server.js --buildtype=vagovprod --host=0.0.0.0 &
node src/platform/testing/e2e/mockapi.js --host=0.0.0.0 --port=3000 &

while ! echo exit | nc localhost ${API_PORT:-3000}; do sleep 3; done
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ const Timeouts = require('../../../../platform/testing/e2e/timeouts');
const PageHelpers = require('./disability-benefits-helpers');
const testData = require('./schema/maximal-test.json');
const FormsTestHelpers = require('../../../../platform/testing/e2e/form-helpers');
const ENVIRONMENTS = require('../../../../site/constants/environments');

const runTest = E2eHelpers.createE2eTest(client => {
PageHelpers.initDocumentUploadMock();
PageHelpers.initApplicationSubmitMock();

if (
process.env.BUILDTYPE !== 'production' &&
process.env.BUILDTYPE !== 'vagovprod'
) {
if (process.env.BUILDTYPE !== ENVIRONMENTS.VAGOVPROD) {
// Ensure introduction page renders.
client
.url(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ const E2eHelpers = require('platform/testing/e2e/helpers');
const Timeouts = require('platform/testing/e2e/timeouts');
const PageHelpers = require('./686-helpers');
const testData = require('./schema/maximal-test.json');
const ENVIRONMENTS = require('../../../../site/constants/environments');
const FormsTestHelpers = require('platform/testing/e2e/form-helpers');

const runTest = E2eHelpers.createE2eTest(client => {
PageHelpers.initApplicationSubmitMock();

if (
process.env.BUILDTYPE !== 'production' &&
process.env.BUILDTYPE !== 'vagovprod'
) {
if (process.env.BUILDTYPE !== ENVIRONMENTS.VAGOVPROD) {
// Ensure introduction page renders.
client
.url(
Expand Down
4 changes: 2 additions & 2 deletions src/applications/facility-locator/api/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import MockApi from './MockLocatorApi';
import LiveApi from './LocatorApi';
import environment from '../../../platform/utilities/environment';

/* global __BUILDTYPE__ */
export default (__BUILDTYPE__ === 'localhost' ? MockApi : LiveApi);
export default (environment.isLocalhost() ? MockApi : LiveApi);
3 changes: 1 addition & 2 deletions src/applications/facility-locator/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable arrow-body-style */
import environment from '../../platform/utilities/environment';
import isProduction from '../../platform/utilities/environment/isProduction';

// Base URL to be used in API requests.
export const api = {
Expand All @@ -21,7 +20,7 @@ export const api = {
* existing Facility Locator App.
*/
export const ccLocatorEnabled = () => {
return !isProduction();
return !environment.isProduction();
};

/* eslint-disable camelcase */
Expand Down
7 changes: 2 additions & 5 deletions src/applications/facility-locator/tests/facility-helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const mock = require('../../../platform/testing/e2e/mock-helpers');
const ENVIRONMENTS = require('../../../site/constants/environments');

const resultsData = {
data: [
Expand Down Expand Up @@ -241,11 +242,7 @@ function initApplicationMock(token) {
* existing Facility Locator App.
*/
function ccLocatorEnabled() {
return (
process.env.BUILDTYPE !== 'production' &&
process.env.BUILDTYPE !== 'preview' &&
process.env.BUILDTYPE !== 'vagovprod'
);
return process.env.BUILDTYPE !== ENVIRONMENTS.VAGOVPROD;
}

module.exports = {
Expand Down
3 changes: 1 addition & 2 deletions src/applications/hca/config/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import applicantDescription from '../../../platform/forms/components/ApplicantDe
import PrefillMessage from '../../../platform/forms/save-in-progress/PrefillMessage';
import MilitaryPrefillMessage from '../../../platform/forms/save-in-progress/MilitaryPrefillMessage';
import preSubmitInfo from '../../../platform/forms/preSubmitInfo';
import isProduction from '../../../platform/utilities/environment/isProduction';

import DowntimeMessage from '../components/DowntimeMessage';

Expand Down Expand Up @@ -417,7 +416,7 @@ const formConfig = {
path: 'military-service/service-information',
title: 'Service periods',
uiSchema: {
'ui:description': !isProduction()
'ui:description': !environment.isProduction()
? MilitaryPrefillMessage
: undefined,
lastServiceBranch: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import PrescriptionCard from '../components/PrescriptionCard';
import isBrandConsolidationEnabled from '../../../../platform/brand-consolidation/feature-flag';
import CallVBACenter from '../../../../platform/brand-consolidation/components/CallVBACenter';
import { mhvBaseUrl } from '../../../../platform/site-wide/cta-widget/helpers';
import isProduction from '../../../../platform/utilities/environment/isProduction';
import environment from '../../../../platform/utilities/environment';

const propertyName = isBrandConsolidationEnabled() ? 'VA.gov' : 'Vets.gov';

Expand Down Expand Up @@ -79,7 +79,7 @@ class PrescriptionsWidget extends React.Component {
{isBrandConsolidationEnabled() ? (
<a
href={`${mhvBaseUrl()}/mhv-portal-web/${
isProduction() ? 'web/myhealthevet/' : ''
environment.isProduction() ? 'web/myhealthevet/' : ''
}refill-prescriptions`}
target="_blank"
>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import dashboardManifest from './manifest';

export default function isPersonalizationEnabled() {
if (__BUILDTYPE__ !== 'production') return true;
return dashboardManifest.production;
return true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Link } from 'react-router';

import CallToActionWidget from '../../../platform/site-wide/cta-widget';
import CallHRC from '../../../platform/brand-consolidation/components/CallHRC';
import isProduction from '../../../platform/utilities/environment/isProduction';
import environment from '../../../platform/utilities/environment';

import EducationWizard from '../components/EducationWizard';
import { wizardConfig } from '../utils/helpers';
Expand Down Expand Up @@ -163,7 +163,7 @@ export default function BrandConsolidationSummary() {
itemScope
itemType="http://schema.org/Answer"
>
{!isProduction() && (
{!environment.isProduction() && (
<div className="intro-wizard" itemProp="text">
There are a few situations where your Post-9/11 GI Bill Statement of
Benefits might not be available. Answer a few questions and we’ll
Expand All @@ -174,7 +174,7 @@ export default function BrandConsolidationSummary() {
/>
</div>
)}
{isProduction() && (
{environment.isProduction() && (
<div itemProp="text">
<p>
Your Post-9/11 GI Bill Statement of Benefits might not be
Expand Down
3 changes: 2 additions & 1 deletion src/platform/forms/save-in-progress/RoutedSavableApp.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import LoadingIndicator from '@department-of-veterans-affairs/formation/LoadingI

import { isInProgress } from '../helpers';
import { getSaveInProgressState } from './selectors';
import environment from '../../utilities/environment';

const Element = Scroll.Element;
const scroller = Scroll.scroller;
Expand Down Expand Up @@ -47,7 +48,7 @@ class RoutedSavableApp extends React.Component {
const trimmedPathname = currentLocation.pathname.replace(/\/$/, '');
const resumeForm = trimmedPathname.endsWith('resume');
const devRedirect =
(__BUILDTYPE__ !== 'localhost' &&
(!environment.isLocalhost() &&
!currentLocation.search.includes('skip')) ||
currentLocation.search.includes('redirect');
const goToStartPage = resumeForm || devRedirect;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,6 @@ describe('Schemaform <RoutedSavableApp>', () => {
};

// Only redirects in production or if ?redirect is in the URL
const buildType = __BUILDTYPE__;
__BUILDTYPE__ = 'production';
const tree = SkinDeep.shallowRender(
<RoutedSavableApp
formConfig={formConfig}
Expand All @@ -249,7 +247,6 @@ describe('Schemaform <RoutedSavableApp>', () => {
tree.getMountedInstance().componentDidMount();

expect(router.replace.calledWith('/introduction')).to.be.true;
__BUILDTYPE__ = buildType;
});
it('should load a saved form when starting in the middle of a form and logged in', () => {
const formConfig = {
Expand All @@ -276,8 +273,6 @@ describe('Schemaform <RoutedSavableApp>', () => {
const fetchInProgressForm = sinon.spy();

// Only redirects in production or if ?redirect is in the URL
const buildType = __BUILDTYPE__;
__BUILDTYPE__ = 'production';
const tree = SkinDeep.shallowRender(
<RoutedSavableApp
formConfig={formConfig}
Expand Down Expand Up @@ -311,7 +306,6 @@ describe('Schemaform <RoutedSavableApp>', () => {
false,
),
).to.be.true;
__BUILDTYPE__ = buildType;
});
it('should load a pre-filled form when starting in the middle of a form and logged in', () => {
const formConfig = {
Expand All @@ -338,8 +332,6 @@ describe('Schemaform <RoutedSavableApp>', () => {
const fetchInProgressForm = sinon.spy();

// Only redirects in production or if ?redirect is in the URL
const buildType = __BUILDTYPE__;
__BUILDTYPE__ = 'production';
const tree = SkinDeep.shallowRender(
<RoutedSavableApp
formConfig={formConfig}
Expand Down Expand Up @@ -373,7 +365,6 @@ describe('Schemaform <RoutedSavableApp>', () => {
true,
),
).to.be.true;
__BUILDTYPE__ = buildType;
});
it('should skip pre-fill when skipPrefill is true', () => {
const formConfig = {
Expand Down Expand Up @@ -401,8 +392,6 @@ describe('Schemaform <RoutedSavableApp>', () => {
const fetchInProgressForm = sinon.spy();

// Only redirects in production or if ?redirect is in the URL
const buildType = __BUILDTYPE__;
__BUILDTYPE__ = 'production';
const tree = SkinDeep.shallowRender(
<RoutedSavableApp
formConfig={formConfig}
Expand Down Expand Up @@ -432,6 +421,5 @@ describe('Schemaform <RoutedSavableApp>', () => {

expect(fetchInProgressForm.called).to.be.false;
expect(router.replace.calledWith('/first-in-form-page')).to.be.true;
__BUILDTYPE__ = buildType;
});
});
4 changes: 1 addition & 3 deletions src/platform/monitoring/frontend-metrics/feature-flag.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@
* @module platform/monitoring/frontend-metrics/feature-flag
*/
export default function isMetricsEnabled() {
const environments = ['vagovdev', 'vagovstaging', 'vagovprod'];

return !!environments.includes(__BUILDTYPE__);
return true;
}
12 changes: 2 additions & 10 deletions src/platform/site-wide/cta-widget/helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import backendServices from '../../user/profile/constants/backendServices';
import environment from '../../utilities/environment';

const frontendApps = {
HEALTH_RECORDS: 'health-records',
Expand Down Expand Up @@ -42,16 +43,7 @@ export const hasRequiredMhvAccount = (appId, accountLevel) => {
export const isHealthTool = appId => HEALTH_TOOLS.includes(appId);

export const mhvBaseUrl = () => {
const lowerEnvironments = [
'localhost',
'vagovdev',
'staging',
'vagovstaging',
];

const mhvSubdomain = lowerEnvironments.includes(__BUILDTYPE__)
? 'mhv-syst'
: 'www';
const mhvSubdomain = !environment.isProduction() ? 'mhv-syst' : 'www';

return `https://${mhvSubdomain}.myhealth.va.gov`;
};
Expand Down
Loading

0 comments on commit 1038000

Please sign in to comment.