serverless-dotenv-plugin
Advanced tools
Comparing version 3.7.2 to 3.8.0
@@ -10,4 +10,10 @@ # Changelog | ||
## Unreleased | ||
## 4.0.x | ||
Breaking changes are introduced when going from version 3.x.x to 4.x.x. | ||
## 3.8.x | ||
* feat: adding an option toggle breaking changes. ([#138](https://github.com/neverendingqs/serverless-dotenv-plugin/pull/138)) | ||
## 3.7.x | ||
@@ -14,0 +20,0 @@ |
{ | ||
"name": "serverless-dotenv-plugin", | ||
"version": "3.7.2", | ||
"version": "3.8.0", | ||
"description": "Preload environment variables with dotenv into serverless.", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -110,4 +110,4 @@ # serverless-dotenv-plugin | ||
# defaults to `false` | ||
logging: true | ||
# defaults to `true` | ||
logging: false | ||
@@ -165,2 +165,5 @@ # default: plugin does not cause an error if any file or env variable is missing | ||
* v4BreakingChanges: true|false (default false) | ||
* Set this to `true` to introduce v3.x.x => v4.x.x breaking changes now | ||
* variableExpansion: true|false (default true) | ||
@@ -196,3 +199,6 @@ * By default, variables can reference other variables | ||
## Changelog | ||
The changelog is available in the `CHANGELOG.md` file in the package or [on GitHub](https://github.com/neverendingqs/serverless-dotenv-plugin/blob/master/CHANGELOG.md). | ||
## FAQ | ||
@@ -199,0 +205,0 @@ |
111
src/index.js
@@ -1,18 +0,18 @@ | ||
'use strict' | ||
'use strict'; | ||
const dotenv = require('dotenv') | ||
const dotenvExpand = require('dotenv-expand') | ||
const chalk = require('chalk') | ||
const fs = require('fs') | ||
const path = require('path') | ||
const dotenv = require('dotenv'); | ||
const dotenvExpand = require('dotenv-expand'); | ||
const chalk = require('chalk'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const errorTypes = { | ||
HALT: 'HALT', | ||
} | ||
}; | ||
class ServerlessPlugin { | ||
constructor(serverless, options) { | ||
this.serverless = serverless | ||
this.serverless = serverless; | ||
this.serverless.service.provider.environment = | ||
this.serverless.service.provider.environment || {} | ||
this.serverless.service.provider.environment || {}; | ||
@@ -24,2 +24,3 @@ this.config = Object.assign( | ||
variableExpansion: true, | ||
v4BreakingChanges: false, | ||
}, | ||
@@ -29,3 +30,3 @@ (this.serverless.service.custom && | ||
{}, | ||
) | ||
); | ||
@@ -36,6 +37,6 @@ if (this.config.dotenvParser) { | ||
this.config.dotenvParser, | ||
) | ||
); | ||
} | ||
this.loadEnv(this.getEnvironment(options)) | ||
this.loadEnv(this.getEnvironment(options)); | ||
} | ||
@@ -45,3 +46,3 @@ | ||
if (this.config.logging) { | ||
this.serverless.cli.log(...args) | ||
this.serverless.cli.log(...args); | ||
} | ||
@@ -55,3 +56,5 @@ } | ||
getEnvironment(options) { | ||
return process.env.NODE_ENV || options.env || options.stage || 'development' | ||
return ( | ||
process.env.NODE_ENV || options.env || options.stage || 'development' | ||
); | ||
} | ||
@@ -64,13 +67,13 @@ | ||
resolveEnvFileNames(env) { | ||
const basePath = (this.config && this.config.basePath) || '' | ||
const basePath = (this.config && this.config.basePath) || ''; | ||
if (this.config && this.config.path) { | ||
if (basePath) { | ||
this.log('DOTENV (WARNING): if "path" is set, "basePath" is ignored.') | ||
this.log('DOTENV (WARNING): if "path" is set, "basePath" is ignored.'); | ||
} | ||
if (Array.isArray(this.config.path)) { | ||
return this.config.path | ||
return this.config.path; | ||
} | ||
return [this.config.path] | ||
return [this.config.path]; | ||
} | ||
@@ -87,7 +90,7 @@ | ||
`.env`, | ||
] | ||
]; | ||
const filesNames = dotenvFiles.map((file) => basePath + file) | ||
const filesNames = dotenvFiles.map((file) => basePath + file); | ||
return filesNames.filter((fileName) => fs.existsSync(fileName)) | ||
return filesNames.filter((fileName) => fs.existsSync(fileName)); | ||
} | ||
@@ -104,5 +107,5 @@ | ||
paths: envFileNames, | ||
}) | ||
}); | ||
} catch (err) { | ||
throw Object.assign(err, { type: errorTypes.HALT }) | ||
throw Object.assign(err, { type: errorTypes.HALT }); | ||
} | ||
@@ -117,9 +120,9 @@ } | ||
const envVarsArray = envFileNames.map((fileName) => { | ||
const parsed = dotenv.config({ path: fileName }) | ||
const parsed = dotenv.config({ path: fileName }); | ||
return this.config.variableExpansion | ||
? dotenvExpand(parsed).parsed | ||
: parsed.parsed | ||
}) | ||
: parsed.parsed; | ||
}); | ||
return envVarsArray.reduce((acc, curr) => ({ ...acc, ...curr }), {}) | ||
return envVarsArray.reduce((acc, curr) => ({ ...acc, ...curr }), {}); | ||
} | ||
@@ -131,8 +134,10 @@ | ||
setProviderEnv(envVars) { | ||
const include = (this.config && this.config.include) || [] | ||
const exclude = (this.config && this.config.exclude) || [] | ||
const include = (this.config && this.config.include) || []; | ||
const exclude = (this.config && this.config.exclude) || []; | ||
if (include.length > 0) { | ||
if (exclude.length > 0) { | ||
this.log('DOTENV (WARNING): if "include" is set, "exclude" is ignored.') | ||
this.log( | ||
'DOTENV (WARNING): if "include" is set, "exclude" is ignored.', | ||
); | ||
} | ||
@@ -143,4 +148,4 @@ | ||
.forEach((key) => { | ||
delete envVars[key] | ||
}) | ||
delete envVars[key]; | ||
}); | ||
} else if (exclude.length > 0) { | ||
@@ -150,10 +155,10 @@ Object.keys(envVars) | ||
.forEach((key) => { | ||
delete envVars[key] | ||
}) | ||
delete envVars[key]; | ||
}); | ||
} | ||
Object.keys(envVars).forEach((key) => { | ||
this.log('\t - ' + key) | ||
this.serverless.service.provider.environment[key] = envVars[key] | ||
}) | ||
this.log('\t - ' + key); | ||
this.serverless.service.provider.environment[key] = envVars[key]; | ||
}); | ||
} | ||
@@ -170,9 +175,9 @@ | ||
':', | ||
) | ||
); | ||
} else { | ||
const errorMsg = 'DOTENV: Could not find .env file.' | ||
this.log(errorMsg) | ||
const errorMsg = 'DOTENV: Could not find .env file.'; | ||
this.log(errorMsg); | ||
if (this.config.required.file === true) { | ||
throw Object.assign(new Error(errorMsg), { type: errorTypes.HALT }) | ||
throw Object.assign(new Error(errorMsg), { type: errorTypes.HALT }); | ||
} | ||
@@ -192,3 +197,3 @@ } | ||
{ type: errorTypes.HALT }, | ||
) | ||
); | ||
} | ||
@@ -198,3 +203,3 @@ | ||
(envVarName) => !envVars[envVarName] && !process.env[envVarName], | ||
) | ||
); | ||
@@ -209,3 +214,3 @@ if (missingRequiredEnvVars.length > 0) { | ||
{ type: errorTypes.HALT }, | ||
) | ||
); | ||
} | ||
@@ -218,15 +223,15 @@ } | ||
loadEnv(env) { | ||
const envFileNames = this.resolveEnvFileNames(env) | ||
const envFileNames = this.resolveEnvFileNames(env); | ||
try { | ||
this.validateEnvFileNames(envFileNames) | ||
this.validateEnvFileNames(envFileNames); | ||
const envVars = this.config.dotenvParserPath | ||
? this.callDotenvParser(envFileNames) | ||
: this.parseEnvFiles(envFileNames) | ||
: this.parseEnvFiles(envFileNames); | ||
this.validateEnvVars(envVars) | ||
this.setProviderEnv(envVars) | ||
this.validateEnvVars(envVars); | ||
this.setProviderEnv(envVars); | ||
} catch (e) { | ||
if (e.type === errorTypes.HALT) { | ||
throw e | ||
throw e; | ||
} | ||
@@ -238,4 +243,4 @@ | ||
), | ||
) | ||
console.error(chalk.red(' ' + e.message)) | ||
); | ||
console.error(chalk.red(' ' + e.message)); | ||
} | ||
@@ -245,2 +250,2 @@ } | ||
module.exports = ServerlessPlugin | ||
module.exports = ServerlessPlugin; |
21862
201
235