diff --git a/__tests__/config/utils/env.test.ts b/__tests__/config/utils/env.test.ts new file mode 100644 index 00000000..f0366dd1 --- /dev/null +++ b/__tests__/config/utils/env.test.ts @@ -0,0 +1,27 @@ +import { filterEnvVariablesForDeploy } from '../../../src/config/utils/env'; +import { EnvironmentVariablesWithAuth } from '../../../src/types/generic'; + +describe('filterEnvVariablesForDeploy', () => { + const testVars: EnvironmentVariablesWithAuth = { + ACCOUNT_SID: 'ACCOUNT_SID', + AUTH_TOKEN: 'AUTH_TOKEN', + empty: '', + hello: 'world', + }; + + it('deletes ACCOUNT_SID and AUTH_TOKEN', () => { + const deployVars = filterEnvVariablesForDeploy(testVars); + expect(deployVars['ACCOUNT_SID']).toBeUndefined(); + expect(deployVars['AUTH_TOKEN']).toBeUndefined(); + }); + + it('deletes empty env vars', () => { + const deployVars = filterEnvVariablesForDeploy(testVars); + expect(deployVars['empty']).toBeUndefined(); + }); + + it('leaves other variables as they were', () => { + const deployVars = filterEnvVariablesForDeploy(testVars); + expect(deployVars['hello']).toEqual('world'); + }); +}); diff --git a/package.json b/package.json index 1550160c..f4e694a8 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "author": "Dominik Kundel ", "license": "MIT", "dependencies": { - "@twilio-labs/serverless-api": "^3.0.0", + "@twilio-labs/serverless-api": "^3.1.0", "@twilio-labs/serverless-runtime-types": "^1.1.7", "@types/express": "^4.17.0", "@types/inquirer": "^6.0.3", diff --git a/src/config/activate.ts b/src/config/activate.ts index 643be10d..ee36c356 100644 --- a/src/config/activate.ts +++ b/src/config/activate.ts @@ -9,7 +9,7 @@ import { } from '../commands/shared'; import { getFullCommand } from '../commands/utils'; import { readSpecializedConfig } from './global'; -import { getCredentialsFromFlags } from './utils'; +import { getCredentialsFromFlags, readLocalEnvFile, filterEnvVariablesForDeploy } from './utils'; import { mergeFlagsAndConfig } from './utils/mergeFlagsAndConfig'; type ActivateConfig = ApiActivateConfig & { @@ -61,6 +61,8 @@ export async function getConfigFromFlags( flags, externalCliOptions ); + const { localEnv } = await readLocalEnvFile(flags); + const env = filterEnvVariablesForDeploy(localEnv); const command = getFullCommand(flags); const serviceSid = checkForValidServiceSid(command, flags.serviceSid); @@ -79,5 +81,6 @@ export async function getConfigFromFlags( sourceEnvironment: flags.sourceEnvironment, region, edge, + env }; } diff --git a/src/config/deploy.ts b/src/config/deploy.ts index 58843e48..65631bc3 100644 --- a/src/config/deploy.ts +++ b/src/config/deploy.ts @@ -14,6 +14,7 @@ import { getServiceNameFromFlags, readLocalEnvFile, readPackageJsonContent, + filterEnvVariablesForDeploy } from './utils'; import { mergeFlagsAndConfig } from './utils/mergeFlagsAndConfig'; @@ -69,6 +70,7 @@ export async function getConfigFromFlags( externalCliOptions ); const { localEnv, envPath } = await readLocalEnvFile(flags); + const env = filterEnvVariablesForDeploy(localEnv); const serviceSid = flags.serviceSid || @@ -83,20 +85,6 @@ export async function getConfigFromFlags( const pkgJson = await readPackageJsonContent(flags); - const env = { - ...localEnv, - }; - - for (let key of Object.keys(env)) { - const val = env[key]; - if (typeof val === 'string' && val.length === 0) { - delete env[key]; - } - } - - delete env.ACCOUNT_SID; - delete env.AUTH_TOKEN; - let serviceName: string | undefined = await getServiceNameFromFlags(flags); if (!serviceName) { diff --git a/src/config/utils/env.ts b/src/config/utils/env.ts index 5f3655cc..46a75aba 100644 --- a/src/config/utils/env.ts +++ b/src/config/utils/env.ts @@ -1,6 +1,7 @@ import dotenv from 'dotenv'; import path from 'path'; import { EnvironmentVariablesWithAuth } from '../../types/generic'; +import { EnvironmentVariables } from '@twilio-labs/serverless-api'; import { fileExists, readFile } from '../../utils/fs'; export async function readLocalEnvFile(flags: { @@ -25,3 +26,21 @@ export async function readLocalEnvFile(flags: { } return { envPath: '', localEnv: {} }; } + +export function filterEnvVariablesForDeploy(localEnv: EnvironmentVariablesWithAuth): EnvironmentVariables { + const env = { + ...localEnv, + }; + + for (let key of Object.keys(env)) { + const val = env[key]; + if (typeof val === 'string' && val.length === 0) { + delete env[key]; + } + } + + delete env.ACCOUNT_SID; + delete env.AUTH_TOKEN; + + return env; +} \ No newline at end of file