
// Used to assign stage-specific env vars in our CI setup
// to the env var names used in app code.

// All env vars we want to pick up from the CI environment.
const envVars = [
  'NODE_ENV',
  'AWS_REGION',
  // AWS Cognito
  'COGNITO_REGION',
  'COGNITO_IDENTITYPOOLID',
  'COGNITO_USERPOOLID',
  'COGNITO_CLIENTID',
  // Web app
  'PUBLIC_PATH',
  'WEB_HOST',
  'WEB_PORT',
  // GraphQL
  'TABLE_NAME_APPENDIX',
  'GRAPHQL_PORT',
  // Endpoints
  'GRAPHQL_ENDPOINT',
  'DYNAMODB_ENDPOINT',
  'S3_ENDPOINT'
  // Secrets
]

// Expect one argument, the stage name.
const assignEnvVars = function (stageName) {
  // Using the name of the stage, assign the stage-specific
  // value to the environment value name.
  const stageNameUppercase = stageName ? stageName.toUpperCase() : ''
  const stagePrefix = stageNameUppercase ? `${stageNameUppercase}_` : ''
  envVars.forEach((envVar) => {
    let stageEnvVar = `${stagePrefix}${envVar}`
    process.env[envVar] = process.env[stageEnvVar]
  })
}

module.exports = assignEnvVars

@@ -605,12 +605,39 @@
 Name
+, allEnvVarsRequired = true
 ) %7B%0A
-
   //
@@ -928,16 +928,20 @@
 geEnvVar
+Name
  = %60$%7Bst
@@ -969,29 +969,319 @@
     
-process.env%5BenvVar%5D =
+let stageEnvVar = process.env%5BstageEnvVarName%5D%0A%0A    // Optionally throw an error if an env variable is not set.%0A    if (%0A      (typeof stageEnvVar === 'undefined' %7C%7C stageEnvVar === null) &&%0A      allEnvVarsRequired%0A    ) %7B%0A      throw new Error(%60Environment variable $%7BstageEnvVarName%7D must be set.%60)%0A    %7D%0A   
  pro
@@ -1281,32 +1281,42 @@
     process.env%5B
+envVar%5D = 
 stageEnvVar%5D%0A  %7D
@@ -1310,17 +1310,16 @@
 geEnvVar
-%5D
 %0A  %7D)%0A%7D%0A

Throw error if an expected env var is not set.