serverless-dotenv-plugin
Advanced tools
Comparing version 2.4.2 to 3.0.0
@@ -5,3 +5,3 @@ const serverless = require('serverless-http') | ||
app.get('/', function(req, res) { | ||
app.get('/', function (req, res) { | ||
res.send(`Hello ${process.env.APP_MESSAGE}!`) | ||
@@ -8,0 +8,0 @@ }) |
@@ -14,7 +14,7 @@ { | ||
"express": "^4.16.4", | ||
"serverless-http": "^1.10.1" | ||
"serverless-http": "^2.5.0" | ||
}, | ||
"devDependencies": { | ||
"serverless-dotenv-plugin": "^1.1.5" | ||
"serverless-dotenv-plugin": "^2.4.2" | ||
} | ||
} |
81
index.js
@@ -15,7 +15,14 @@ 'use strict' | ||
this.serverless.service.custom && this.serverless.service.custom['dotenv'] | ||
this.logging = this.config && typeof this.config.logging !== 'undefined' ? this.config.logging : true; | ||
this.logging = | ||
this.config && typeof this.config.logging !== 'undefined' | ||
? this.config.logging | ||
: true | ||
this.loadEnv(this.getEnvironment(options)) | ||
} | ||
/** | ||
* @param {Object} options | ||
* @returns {string} | ||
*/ | ||
getEnvironment(options) { | ||
@@ -25,24 +32,51 @@ return process.env.NODE_ENV || options.env || options.stage || 'development' | ||
resolveEnvFileName(env) { | ||
/** | ||
* @param {string} env | ||
* @returns {string[]} | ||
*/ | ||
resolveEnvFileNames(env) { | ||
if (this.config && this.config.path) { | ||
return this.config.path | ||
if (Array.isArray(this.config.path)) { | ||
return this.config.path | ||
} | ||
return [this.config.path] | ||
} | ||
let basePath = | ||
// https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use | ||
const dotenvFiles = [ | ||
`.env.${env}.local`, | ||
`.env.${env}`, | ||
// Don't include `.env.local` for `test` environment | ||
// since normally you expect tests to produce the same | ||
// results for everyone | ||
env !== 'test' && `.env.local`, | ||
`.env`, | ||
] | ||
const basePath = | ||
this.config && this.config.basePath ? this.config.basePath : '' | ||
let defaultPath = basePath + '.env' | ||
let path = basePath + '.env.' + env | ||
const filesNames = dotenvFiles.map((file) => basePath + file) | ||
return fs.existsSync(path) ? path : defaultPath | ||
return filesNames.filter((fileName) => fs.existsSync(fileName)) | ||
} | ||
/** | ||
* @param {string} env | ||
*/ | ||
loadEnv(env) { | ||
var envFileName = this.resolveEnvFileName(env) | ||
const envFileNames = this.resolveEnvFileNames(env) | ||
try { | ||
let envVars = dotenvExpand(dotenv.config({ path: envFileName })).parsed | ||
const envVarsArray = envFileNames.map( | ||
(fileName) => dotenvExpand(dotenv.config({ path: fileName })).parsed, | ||
) | ||
var include = false | ||
var exclude = false | ||
const envVars = envVarsArray.reduce( | ||
(acc, curr) => ({ ...acc, ...curr }), | ||
{}, | ||
) | ||
let include = false | ||
let exclude = false | ||
if (this.config && this.config.include) { | ||
@@ -52,3 +86,4 @@ include = this.config.include | ||
if (this.config && this.config.exclude && !include) { // Don't allow both include and exclude to be specified | ||
if (this.config && this.config.exclude && !include) { | ||
// Don't allow both include and exclude to be specified | ||
exclude = this.config.exclude | ||
@@ -60,3 +95,5 @@ } | ||
this.serverless.cli.log( | ||
'DOTENV: Loading environment variables from ' + envFileName + ':' | ||
'DOTENV: Loading environment variables from ' + | ||
envFileNames.reverse().join(', ') + | ||
':', | ||
) | ||
@@ -66,4 +103,4 @@ } | ||
Object.keys(envVars) | ||
.filter(key => !include.includes(key)) | ||
.forEach(key => { | ||
.filter((key) => !include.includes(key)) | ||
.forEach((key) => { | ||
delete envVars[key] | ||
@@ -74,8 +111,8 @@ }) | ||
Object.keys(envVars) | ||
.filter(key => exclude.includes(key)) | ||
.forEach(key => { | ||
.filter((key) => exclude.includes(key)) | ||
.forEach((key) => { | ||
delete envVars[key] | ||
}) | ||
} | ||
Object.keys(envVars).forEach(key => { | ||
} | ||
Object.keys(envVars).forEach((key) => { | ||
if (this.logging) { | ||
@@ -94,4 +131,4 @@ this.serverless.cli.log('\t - ' + key) | ||
chalk.red( | ||
'\n Serverless Plugin Error --------------------------------------\n' | ||
) | ||
'\n Serverless Plugin Error --------------------------------------\n', | ||
), | ||
) | ||
@@ -98,0 +135,0 @@ console.error(chalk.red(' ' + e.message)) |
{ | ||
"name": "serverless-dotenv-plugin", | ||
"version": "2.4.2", | ||
"version": "3.0.0", | ||
"description": "Preload environment variables with dotenv into serverless.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"prettier": "prettier --write \"**/*.js\"" | ||
}, | ||
@@ -25,6 +25,9 @@ "repository": { | ||
"dependencies": { | ||
"chalk": "^3.0.0", | ||
"chalk": "^4.1.0", | ||
"dotenv": "^8.2.0", | ||
"dotenv-expand": "^5.1.0" | ||
}, | ||
"devDependencies": { | ||
"prettier": "^2.0.5" | ||
} | ||
} |
@@ -1,4 +0,4 @@ | ||
# serverless-dotenv-plugin [![npm version](https://img.shields.io/npm/v/serverless-dotenv-plugin.svg?style=flat)](https://www.npmjs.com/package/serverless-dotenv-plugin) | ||
# serverless-dotenv-plugin | ||
Checkout https://colyn.dev/docs for documentation | ||
[![npm version](https://img.shields.io/npm/v/serverless-dotenv-plugin.svg?style=flat)](https://www.npmjs.com/package/serverless-dotenv-plugin) | ||
@@ -11,3 +11,3 @@ Preload environment variables into serverless. Use this plugin if you have variables stored in a `.env` file that you want loaded into your serverless yaml config. This will allow you to reference them as `${env:VAR_NAME}` inside your config _and_ it will load them into your lambdas. | ||
``` | ||
```bash | ||
> npm i -D serverless-dotenv-plugin | ||
@@ -18,3 +18,3 @@ ``` | ||
``` | ||
```bash | ||
service: myService | ||
@@ -28,3 +28,3 @@ plugins: | ||
``` | ||
```bash | ||
DYANMODB_TABLE=myTable | ||
@@ -36,7 +36,7 @@ AWS_REGION=us-west-1 | ||
#### Automatic Env file name resolution | ||
### Automatic ENV File Resolution (as of verson 3.0+ Thanks to [@danilofuchs](https://github.com/danilofuchs)!) | ||
By default, the plugin looks for the file: `.env`. In most use cases this is all that is needed. However, there are times where you want different env files based on environment. For instance: | ||
``` | ||
```bash | ||
.env.development | ||
@@ -46,3 +46,3 @@ .env.production | ||
When you deploy with `NODE_ENV` set: `NODE_ENV=production sls deploy` the plugin will look for a file named `.env.production`. If it doesn't exist it will default to `.env`. If for some reason you can't set NODE_ENV, you could always just pass it in as an option: `sls deploy --env production` or `sls deploy --stage production`. If `NODE_ENV`, `--env` or `--stage` is not set, it will default to `development`. | ||
When you deploy with `NODE_ENV` set: `NODE_ENV=production sls deploy` the plugin will look for files named `.env`, `.env.production`, `.env.production.local`. If for some reason you can't set NODE_ENV, you could always just pass it in as an option: `sls deploy --env production` or `sls deploy --stage production`. If `NODE_ENV`, `--env` or `--stage` is not set, it will default to `development`. | ||
@@ -52,8 +52,14 @@ The precedence between the options is the following: | ||
| Valid .env file names | Description | | ||
| --------------------- | ------------------------------------------------------------------------------------------------------------ | | ||
| .env | Default file name when no other files are specified or found. | | ||
| .env.development | If NODE_ENV or --env or --stage **is not set**, will try to load `.env.development`. If not found, load `.env` | | ||
| .env.{ENV} | If NODE_ENV or --env or --stage **is set**, will try to load `.env.{env}`. If not found, load `.env` | | ||
The env resolution pattern follows the one used by [Rail's dotenv](https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use) and [create-react-app](https://create-react-app.dev/docs/adding-custom-environment-variables/#what-other-env-files-can-be-used) | ||
| Valid .env file names | Description | | ||
| --------------------- | ------------------------------------------------------------------------------------ | | ||
| .env | Default file, always included | | ||
| .env.local | Included in all environments except test | | ||
| .env.development | If NODE_ENV or --env or --stage **is not set**, will try to load `.env.development`. | | ||
| .env.{ENV} | If NODE_ENV or --env or --stage **is set**, will try to load `.env.{env}`. | | ||
| .env.{ENV}.local | Every env set up in `.env.{ENV}.local` **will override** other envs | | ||
> Note: .env, .env.development, and .env.production files should be included in your repository as they define defaults. .env\*.local should be added to .gitignore, as those files are intended to be ignored. .env.local is where secrets can be stored. | ||
### Plugin options | ||
@@ -63,3 +69,3 @@ | ||
The plugin will look for your .env file in the same folder where you run the command using the file resolution rules as described above, but these rules can be overridden by setting the `path` option. | ||
The plugin will look for your .env file in the same folder where you run the command using the file resolution rules as described above, but these rules can be overridden by setting the `path` option. This will **disable** automatic env file resolution | ||
@@ -82,3 +88,3 @@ > basePath: path/to/my/ | ||
``` | ||
```bash | ||
custom: | ||
@@ -98,3 +104,3 @@ dotenv: | ||
``` | ||
```bash | ||
custom: | ||
@@ -114,3 +120,3 @@ dotenv: | ||
``` | ||
```bash | ||
... | ||
@@ -133,2 +139,6 @@ provider: | ||
### Changelog | ||
https://colyn.dev/serverless-dotenv-plugin-changelog/ | ||
### Contributing | ||
@@ -138,4 +148,5 @@ | ||
### Roadmap | ||
## Contributors | ||
See https://colyn.dev/upcoming-changes-to-serverless-dotenv-plugin for upcoming changes. | ||
This project exists thanks to all the people who contribute. | ||
<a href="https://github.com/colynb/serverless-dotenv-plugin/graphs/contributors"><img src="https://opencollective.com/serverless-dotenv-plugin/contributors.svg?width=890&button=false" /></a> |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
31955
13
0
141
1
124
1
+ Addedchalk@4.1.2(transitive)
- Removedchalk@3.0.0(transitive)
Updatedchalk@^4.1.0