From 1ea584096ca7381e660263508ba272d4b6e181b7 Mon Sep 17 00:00:00 2001 From: Roman Krayovskyy Date: Sat, 31 Aug 2019 19:56:04 +0200 Subject: [PATCH] add option to keep .env file after being generated --- src/index.js | 10 ++++++++-- test/index.js | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 1f15ac5..2290f90 100644 --- a/src/index.js +++ b/src/index.js @@ -35,7 +35,13 @@ class ServerlessEnvGeneratorPlugin { commands: { generate: { usage: 'Creates the .env file manually', - lifecycleEvents: ['write'] + lifecycleEvents: ['write'], + options: { + keepEnvFile: { + usage: 'Keep .env file after being manually generated', + shortcut: 'k' + } + } } } } @@ -82,7 +88,7 @@ class ServerlessEnvGeneratorPlugin { this.serverless.cli.log('Creating .env file...') process .on('exit', () => { - if (fs.existsSync(config.dotEnvPath)) { + if (fs.existsSync(config.dotEnvPath && !this.options.keepEnvFile)) { fs.removeSync(config.dotEnvPath); this.serverless.cli.log('Removed .env file') } diff --git a/test/index.js b/test/index.js index 7d6b395..9d8898c 100644 --- a/test/index.js +++ b/test/index.js @@ -249,6 +249,25 @@ describe('index.js', () => { }) }) + it('should write and keep .env file upon manual generation', () => { + initEnvGenerator({ + keepEnvFile: true + }) + sandbox.stub(helper, 'getEnvVars').callsFake(() => { + return Promise.resolve(defaultEnvFiles) + }) + sandbox.stub(fs, 'writeFile').callsFake((file, content) => { + return Promise.resolve() + }) + sandbox.stub(fs, 'remove').callsFake(_ => { + return Promise.resolve() + }) + return envGenerator.hooks['env:generate:write']().then(_ => { + expect(fs.writeFile.callCount).to.equal(1) + expect(fs.remove.callCount).to.equal(0) + }) + }) + it('should work with single kms key', () => { serverless.service.custom.envEncryptionKeyId = 'allthesinglekeys' initEnvGenerator({})