Skip to content

Commit 808fe47

Browse files
authored
fix(activate): prepare and pass env vars to serverless-api activateBuild (#142)
* fix(activate): prepare and pass env vars to serverless-api activateBuild * chore: rename prepareEnvForDeploy to filterEnvVariablesForDeploy * chore: adds test for filterEnvVariablesForDeploy * chore(deps): updates @twilio-labs/serverless-api to 3.1.0 Fixes: #109
1 parent 46fffa0 commit 808fe47

File tree

5 files changed

+53
-16
lines changed

5 files changed

+53
-16
lines changed

__tests__/config/utils/env.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { filterEnvVariablesForDeploy } from '../../../src/config/utils/env';
2+
import { EnvironmentVariablesWithAuth } from '../../../src/types/generic';
3+
4+
describe('filterEnvVariablesForDeploy', () => {
5+
const testVars: EnvironmentVariablesWithAuth = {
6+
ACCOUNT_SID: 'ACCOUNT_SID',
7+
AUTH_TOKEN: 'AUTH_TOKEN',
8+
empty: '',
9+
hello: 'world',
10+
};
11+
12+
it('deletes ACCOUNT_SID and AUTH_TOKEN', () => {
13+
const deployVars = filterEnvVariablesForDeploy(testVars);
14+
expect(deployVars['ACCOUNT_SID']).toBeUndefined();
15+
expect(deployVars['AUTH_TOKEN']).toBeUndefined();
16+
});
17+
18+
it('deletes empty env vars', () => {
19+
const deployVars = filterEnvVariablesForDeploy(testVars);
20+
expect(deployVars['empty']).toBeUndefined();
21+
});
22+
23+
it('leaves other variables as they were', () => {
24+
const deployVars = filterEnvVariablesForDeploy(testVars);
25+
expect(deployVars['hello']).toEqual('world');
26+
});
27+
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"author": "Dominik Kundel <dkundel@twilio.com>",
3939
"license": "MIT",
4040
"dependencies": {
41-
"@twilio-labs/serverless-api": "^3.0.0",
41+
"@twilio-labs/serverless-api": "^3.1.0",
4242
"@twilio-labs/serverless-runtime-types": "^1.1.7",
4343
"@types/express": "^4.17.0",
4444
"@types/inquirer": "^6.0.3",

src/config/activate.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from '../commands/shared';
1010
import { getFullCommand } from '../commands/utils';
1111
import { readSpecializedConfig } from './global';
12-
import { getCredentialsFromFlags } from './utils';
12+
import { getCredentialsFromFlags, readLocalEnvFile, filterEnvVariablesForDeploy } from './utils';
1313
import { mergeFlagsAndConfig } from './utils/mergeFlagsAndConfig';
1414

1515
type ActivateConfig = ApiActivateConfig & {
@@ -61,6 +61,8 @@ export async function getConfigFromFlags(
6161
flags,
6262
externalCliOptions
6363
);
64+
const { localEnv } = await readLocalEnvFile(flags);
65+
const env = filterEnvVariablesForDeploy(localEnv);
6466

6567
const command = getFullCommand(flags);
6668
const serviceSid = checkForValidServiceSid(command, flags.serviceSid);
@@ -79,5 +81,6 @@ export async function getConfigFromFlags(
7981
sourceEnvironment: flags.sourceEnvironment,
8082
region,
8183
edge,
84+
env
8285
};
8386
}

src/config/deploy.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
getServiceNameFromFlags,
1515
readLocalEnvFile,
1616
readPackageJsonContent,
17+
filterEnvVariablesForDeploy
1718
} from './utils';
1819
import { mergeFlagsAndConfig } from './utils/mergeFlagsAndConfig';
1920

@@ -69,6 +70,7 @@ export async function getConfigFromFlags(
6970
externalCliOptions
7071
);
7172
const { localEnv, envPath } = await readLocalEnvFile(flags);
73+
const env = filterEnvVariablesForDeploy(localEnv);
7274

7375
const serviceSid =
7476
flags.serviceSid ||
@@ -83,20 +85,6 @@ export async function getConfigFromFlags(
8385

8486
const pkgJson = await readPackageJsonContent(flags);
8587

86-
const env = {
87-
...localEnv,
88-
};
89-
90-
for (let key of Object.keys(env)) {
91-
const val = env[key];
92-
if (typeof val === 'string' && val.length === 0) {
93-
delete env[key];
94-
}
95-
}
96-
97-
delete env.ACCOUNT_SID;
98-
delete env.AUTH_TOKEN;
99-
10088
let serviceName: string | undefined = await getServiceNameFromFlags(flags);
10189

10290
if (!serviceName) {

src/config/utils/env.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import dotenv from 'dotenv';
22
import path from 'path';
33
import { EnvironmentVariablesWithAuth } from '../../types/generic';
4+
import { EnvironmentVariables } from '@twilio-labs/serverless-api';
45
import { fileExists, readFile } from '../../utils/fs';
56

67
export async function readLocalEnvFile(flags: {
@@ -25,3 +26,21 @@ export async function readLocalEnvFile(flags: {
2526
}
2627
return { envPath: '', localEnv: {} };
2728
}
29+
30+
export function filterEnvVariablesForDeploy(localEnv: EnvironmentVariablesWithAuth): EnvironmentVariables {
31+
const env = {
32+
...localEnv,
33+
};
34+
35+
for (let key of Object.keys(env)) {
36+
const val = env[key];
37+
if (typeof val === 'string' && val.length === 0) {
38+
delete env[key];
39+
}
40+
}
41+
42+
delete env.ACCOUNT_SID;
43+
delete env.AUTH_TOKEN;
44+
45+
return env;
46+
}

0 commit comments

Comments
 (0)