Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions __tests__/config/utils/env.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"author": "Dominik Kundel <dkundel@twilio.com>",
"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",
Expand Down
5 changes: 4 additions & 1 deletion src/config/activate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 & {
Expand Down Expand Up @@ -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);
Expand All @@ -79,5 +81,6 @@ export async function getConfigFromFlags(
sourceEnvironment: flags.sourceEnvironment,
region,
edge,
env
};
}
16 changes: 2 additions & 14 deletions src/config/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
getServiceNameFromFlags,
readLocalEnvFile,
readPackageJsonContent,
filterEnvVariablesForDeploy
} from './utils';
import { mergeFlagsAndConfig } from './utils/mergeFlagsAndConfig';

Expand Down Expand Up @@ -69,6 +70,7 @@ export async function getConfigFromFlags(
externalCliOptions
);
const { localEnv, envPath } = await readLocalEnvFile(flags);
const env = filterEnvVariablesForDeploy(localEnv);

const serviceSid =
flags.serviceSid ||
Expand All @@ -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) {
Expand Down
19 changes: 19 additions & 0 deletions src/config/utils/env.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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;
}