Skip to content

Commit

Permalink
feat(Variables): Expand environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
d-asensio committed Jun 16, 2021
1 parent 4ca51cc commit f70f8f8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
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 @@ -40,6 +40,7 @@
"dayjs": "^1.10.5",
"decompress": "^4.2.1",
"dotenv": "^10.0.0",
"dotenv-expand": "^5.1.0",
"essentials": "^1.1.1",
"fastest-levenshtein": "^1.0.12",
"filesize": "^6.3.0",
Expand Down

0 comments on commit f70f8f8

Please sign in to comment.