Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(CLI): Automatically expand loaded env variables #9615

Merged
merged 4 commits into from
Jul 15, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ With `useDotenv: true` set in your `serverless.yml` file, framework automaticall

The framework looks for `.env` and `.env.{stage}` files in service directory and then tries to load them using `dotenv`. If `.env.{stage}` is found, `.env` will not be loaded. If stage is not explicitly defined, it defaults to `dev`.

### Variable expansion
d-asensio marked this conversation as resolved.
Show resolved Hide resolved

It is possible to define environment variables as a combination of existing ones:

```env
BASE_URL=my.api.com
PROTOCOL=https

URL=$PROTOCOL/$BASE_URL
```

> This is supported through [dotenv-expand](https://github.com/motdotla/dotenv-expand)

### Differences against `serverless-dotenv-plugin`

There are a few differences between above functionality and [serverless-dotenv-plugin](https://github.com/colynb/serverless-dotenv-plugin):
Expand Down
11 changes: 9 additions & 2 deletions lib/cli/load-dotenv.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const dotenv = require('dotenv');
const dotenvExpand = require('dotenv-expand');
const path = require('path');
const ServerlessError = require('../serverless-error');

Expand All @@ -11,19 +12,25 @@ const throwDotenvError = (error, filePath) => {
throw new ServerlessError(errorMessage, 'DOTENV_LOAD_ERROR');
};

const loadDotenvFrom = (envFilePath) => {
const config = dotenv.config({ path: envFilePath });
return dotenvExpand(config);
};

module.exports = (stage) => {
const defaultEnvFilePath = path.join(process.cwd(), '.env');
const stageEnvFilePath = path.join(process.cwd(), `.env.${stage}`);

const { error: stageEnvResultError } = dotenv.config({ path: stageEnvFilePath });
const { error: stageEnvResultError } = loadDotenvFrom(stageEnvFilePath);

if (!stageEnvResultError) return;

if (!isMissingFileError(stageEnvResultError)) {
throwDotenvError(stageEnvResultError, stageEnvFilePath);
}

const { error: defaultEnvResultError } = dotenv.config({ path: defaultEnvFilePath });
const { error: defaultEnvResultError } = loadDotenvFrom(defaultEnvFilePath);

if (defaultEnvResultError && !isMissingFileError(defaultEnvResultError)) {
throwDotenvError(defaultEnvResultError, defaultEnvFilePath);
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"dayjs": "^1.10.6",
"decompress": "^4.2.1",
"dotenv": "^10.0.0",
"dotenv-expand": "^5.1.0",
"essentials": "^1.1.1",
"fastest-levenshtein": "^1.0.12",
"filesize": "^7.0.0",
Expand Down
9 changes: 8 additions & 1 deletion test/unit/lib/Serverless.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,16 @@ describe('test/unit/lib/Serverless.test.js', () => {
custom: {
fromDefaultEnv: '${env:DEFAULT_ENV_VARIABLE}',
fromStageEnv: "${env:STAGE_ENV_VARIABLE, 'not-found'}",
fromDefaultExpandedEnv: '${env:DEFAULT_ENV_VARIABLE_EXPANDED}',
},
},
})
).servicePath;

const defaultFileContent = 'DEFAULT_ENV_VARIABLE=valuefromdefault';
const defaultFileContent = `
DEFAULT_ENV_VARIABLE=valuefromdefault
DEFAULT_ENV_VARIABLE_EXPANDED=$DEFAULT_ENV_VARIABLE/expanded
`;
await fs.promises.writeFile(path.join(serviceDir, '.env'), defaultFileContent);
conditionallyLoadDotenv.clear();
});
Expand All @@ -347,6 +351,9 @@ describe('test/unit/lib/Serverless.test.js', () => {
});

expect(result.serverless.service.custom.fromDefaultEnv).to.equal('valuefromdefault');
expect(result.serverless.service.custom.fromDefaultExpandedEnv).to.equal(
'valuefromdefault/expanded'
);
expect(result.serverless.service.custom.fromStageEnv).to.equal('not-found');
});
});
Expand Down