From 0ba0331b0d587c5b3b00b29909b320f8d02c0d1d Mon Sep 17 00:00:00 2001 From: Joe Schmitt Date: Thu, 1 Aug 2019 11:11:45 -0400 Subject: [PATCH 1/5] Add cli flag to pass custom output path for .env file --- .gitignore | 1 + src/index.js | 13 ++++++++++--- test/index.spec.js | 12 +++++++++++- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index b9651a9..5a0c5b0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ coverage node_modules *.log +tmp diff --git a/src/index.js b/src/index.js index 09f889a..48128b5 100644 --- a/src/index.js +++ b/src/index.js @@ -18,7 +18,14 @@ class ServerlessDotenvPlugin { usage: 'Create .env file with serverless environment variables', lifecycleEvents: [ 'dotenvHandler' - ] + ], + options: { + path: { + usage: 'Specify an output path for the .env file. Defaults to .serverless/', + shortcut: 'p', + required: false + } + } } } @@ -55,8 +62,8 @@ class ServerlessDotenvPlugin { } // write .env file - const dotEnvPath = path.join(this.serverless.config.servicePath, '.serverless') - const dotEnvFile = path.join(this.serverless.config.servicePath, '.serverless/.env') + const dotEnvPath = this.options.path || path.join(this.serverless.config.servicePath, '.serverless') + const dotEnvFile = path.join(dotEnvPath, '.env') const dotEnvDocument = transformEnvVarsToString(this.environmentVariables) mkdirp.sync(dotEnvPath) diff --git a/test/index.spec.js b/test/index.spec.js index d8f4e95..fbcbcf3 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -45,7 +45,7 @@ describe('serverless dotenv plugin', () => { expectedEnvVars += 'TEST1_ENV_VAR=value123\r\n' expectedEnvVars += 'TEST2_ENV_VAR=value123\r\n' - pluginInstance = new ServerlessDotenvPlugin(serverless) + pluginInstance = new ServerlessDotenvPlugin(serverless, {}) }) describe('starting plugin', () => { @@ -66,6 +66,16 @@ describe('serverless dotenv plugin', () => { expect(dotEnvDocument.toString('ascii')).toEqual(expectedEnvVars) }) + it('should write .env file to a custom location', () => { + pluginInstance.options.path = 'tmp/custom/path' + pluginInstance.dotenvHandler() + + const dotEnvFile = path.join('tmp/custom/path', '.env') + const dotEnvDocument = fs.readFileSync(dotEnvFile) + + expect(dotEnvDocument.toString('ascii')).toEqual(expectedEnvVars) + }) + it('should append offline variables when hooked', () => { serverless.pluginManager = { run: () => pluginInstance.dotenvHandler() From 6065cbbbaaa4de653c8da39b032412ffc25d57ed Mon Sep 17 00:00:00 2001 From: Joe Schmitt Date: Thu, 1 Aug 2019 11:20:12 -0400 Subject: [PATCH 2/5] Read custom path from custom vars in serverless.yaml --- src/index.js | 6 +++++- test/index.spec.js | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 48128b5..8c30139 100644 --- a/src/index.js +++ b/src/index.js @@ -12,6 +12,8 @@ class ServerlessDotenvPlugin { constructor (serverless, options) { this.serverless = serverless this.options = options + this.custom = this.serverless.service.custom || {}; + this.custom.dotenv = {}; this.commands = { dotenv: { @@ -62,7 +64,9 @@ class ServerlessDotenvPlugin { } // write .env file - const dotEnvPath = this.options.path || path.join(this.serverless.config.servicePath, '.serverless') + const dotEnvPath = this.options.path || this.custom.dotenv.path || + path.join(this.serverless.config.servicePath, '.serverless') + const dotEnvFile = path.join(dotEnvPath, '.env') const dotEnvDocument = transformEnvVarsToString(this.environmentVariables) diff --git a/test/index.spec.js b/test/index.spec.js index fbcbcf3..440adbf 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -76,6 +76,16 @@ describe('serverless dotenv plugin', () => { expect(dotEnvDocument.toString('ascii')).toEqual(expectedEnvVars) }) + it('should write .env file to a custom location set in serverless.yaml custom config', () => { + serverless.service.custom = {dotenv: 'tmp/custom/path'} + pluginInstance.dotenvHandler() + + const dotEnvFile = path.join('tmp/custom/path', '.env') + const dotEnvDocument = fs.readFileSync(dotEnvFile) + + expect(dotEnvDocument.toString('ascii')).toEqual(expectedEnvVars) + }) + it('should append offline variables when hooked', () => { serverless.pluginManager = { run: () => pluginInstance.dotenvHandler() From 0869989c13ae24595bd21daefc248fa54dda9cbd Mon Sep 17 00:00:00 2001 From: Joe Schmitt Date: Thu, 1 Aug 2019 11:28:06 -0400 Subject: [PATCH 3/5] Update README --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index 796a15e..45ae730 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,23 @@ plugins: That's it! You can now type `serverless dotenv` in your terminal to generate the `.env` file based on your serverless configuration. Alternative you can just start `serverless offline` to generate it. +### Setting a custom path + +If you want to customize the output location of the `.env` file, you can set a custom `path` option. + +Via the CLI: +```sh +serverless dotenv --path /some/custom/path +``` + +Or via the `serverless.yaml` file: +```yaml +service: some-service +custom: + dotenv: + path: some/custom/path +``` + ## Contribution Feel free to contribute to this project! Our JavaScript is written based on [standardJS](https://standardjs.com). We recommend to use a `standardJS` [plugin](https://standardjs.com/index.html#are-there-text-editor-plugins) for your Editor, but you can also lint your code with `yarn run lint` - respectively `npm run lint`. Please don't forget to add unit and/or integration tests. Thanks <3 From f68ca76267a23abf172208a4790b1cf8832a4789 Mon Sep 17 00:00:00 2001 From: Joe Schmitt Date: Fri, 16 Aug 2019 13:35:50 -0400 Subject: [PATCH 4/5] Fix overwriting custom dotenv config --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 8c30139..936243b 100644 --- a/src/index.js +++ b/src/index.js @@ -13,7 +13,7 @@ class ServerlessDotenvPlugin { this.serverless = serverless this.options = options this.custom = this.serverless.service.custom || {}; - this.custom.dotenv = {}; + this.custom.dotenv = this.custom.dotenv || {}; this.commands = { dotenv: { From dab8f3deb72423706055e8e03c34a4e9fadb7f59 Mon Sep 17 00:00:00 2001 From: Joe Schmitt Date: Fri, 16 Aug 2019 13:38:11 -0400 Subject: [PATCH 5/5] Run the dotenv handler on some more lifecycle events --- src/index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/index.js b/src/index.js index 936243b..2f15d9a 100644 --- a/src/index.js +++ b/src/index.js @@ -34,6 +34,9 @@ class ServerlessDotenvPlugin { this.hooks = { 'before:offline:start:init': this.initOfflineHook.bind(this), 'before:offline:start': this.initOfflineHook.bind(this), + 'before:invoke:local:invoke': this.dotenvHandler.bind(this), + 'before:package:function:package': this.dotenvHandler.bind(this), + 'before:package:createDeploymentArtifacts': this.dotenvHandler.bind(this), 'dotenv:dotenvHandler': this.dotenvHandler.bind(this) }