├── .eslintignore ├── .eslintrc.yml ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── package.json ├── src └── index.js └── test ├── .eslintrc.yml ├── index.js └── index.tests.js /.eslintignore: -------------------------------------------------------------------------------- 1 | coverage 2 | node_modules 3 | -------------------------------------------------------------------------------- /.eslintrc.yml: -------------------------------------------------------------------------------- 1 | extends: 2 | - eslint:recommended 3 | - plugin:node/recommended 4 | plugins: 5 | - node 6 | - promise 7 | 8 | env: 9 | mocha: true 10 | node: true 11 | 12 | rules: 13 | no-console: 0 14 | strict: 0 15 | node/no-unsupported-features: [ 2, { "version": 4 }] 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '4.4' 4 | - '5.11' 5 | - '6.2' 6 | 7 | script: npm run lint && npm test 8 | deploy: 9 | provider: npm 10 | email: accounts@acloud.guru 11 | api_key: 12 | secure: v4eFNEPAF6DvgAlwubtWVLLiqF5/t753Aa2VaqRKGSCy6+obnrSrFpN1tbVT9pOBRVSpoCqJcliqEdYr19bs103l1NpkhP21N+NQRJrKuYifiTc/ZL8j7z2DWQSijuJo8S7Gz0f7bW/hjbsovpbdxEDmvfwuVJb5RbmRNVGQtEtqTxb4K1MoaqDLDSTJBfmdp5JEZP235lsjjHaKBAKX4PpBhGG2Qg1rCLSEldW859gJD4YjRSrr9tjihEOg2YuL+TsUN/PnHvEqWo6XoEygJ6ymSh+HtkSVp89L9cKP0ilGZqwgKbbV5SFfjKdwmFcPLswCp5P67k3emeqeNKkQ4/EtqUjh4CS7fHbrrdlVFuSh+d5zmaJBtFU/OntZuHZNBa++AtV9Hm7aoD4BnoMsdnU5SWvcnjxdhP0v9oHJ4AlXbNuNG0jStjUtN6OEBjQRC60tVjYVJbDQiY7B4wdz/TJoR6LZnKEdDWAszu2IhwdsYP5efTa0iSZRqKI8WoN7Y99S6yrIKOwnAa1yGNocGyNsibE5oXSleYZZc1QWYg4iIiwozmkmgDY6tth+mC3kMN73Qfapaen5Kd4GjrSfezWbQPbtzGH6oo16CJWH1r8VucCk1PCk2QDe0G6hE9htPaw8CtgyOwgei/cO03J4SakSc+SDILrDpq6B2UOpQKU= 13 | on: 14 | tags: true 15 | repo: ACloudGuru/serverless-plugin-package-dotenv-file 16 | branch: master 17 | notifications: 18 | slack: 19 | secure: JB3mlMMJHR92ZPV82ybysLi6s3IA4JJmkoI2y3z1FdNalB60SUdUZsnobE5TkjWFDw5a6zoYwvhDdXqohZuUewBiNWBDJoSh/OSJRSDGB5AGaIkD25NrqICFmocpkyux1dWZJT9emWXDZLiTfBGKsJwzvcDJ/C0vFk4uWNJaA3D7u1Bho/jdvguRhOYk4dmqsdURKGGgolnCAaPxoI0HJhVr53BDnP2OlKY3sMWktTOWUIM6UoUXB1f5Raf/pfoLnz1Lx/Ppkse49iu7mXHMr3g6LVzn00BA8im03hhgm06BdYkXfKPLG/y9o7wMgYoSdwOs5G/Y9/5exzFiEjsDjlBhoj6x8dvBjApZrxIzbv0QveqsSYwRJt1JUMkvAdLDCiqe4wqhqMSN9tbJpMSLi/ywBXQ3Phy1oBZPc1fpU/0HRORMGXiBHBVvCON2D3zxFRM5eGMB16MrNp/2KNpkwCSBWM531nOgtFcbLQnMBtuo1K653fgFLguAJGhA0r9yn4Wpcs3zdUzoBURfvKPU02tpGzbBJhsj+DFcCv6nYSmePwR/UFLMg+ov+BmErVWG2huFDKGnPtk3aPAv0kXHZFH6YRjmqb6W+V+jk+Htdfp/YAfx+p+5BUAwHj6zGBMfle9sVwngtd/GMt7kJ5BSrBXgn2kxHINCJ959iEyjFeQ= 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 A Cloud Guru 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Serverless Package Env Plugin [![Build Status](https://travis-ci.org/ACloudGuru/serverless-plugin-package-dotenv-file.svg?branch=master)](https://travis-ci.org/ACloudGuru/serverless-plugin-package-dotenv-file) 2 | 3 | A Serverless plugin to copy a .env file into the serverless package 4 | 5 | 6 | ## Installation 7 | `npm i serverless-plugin-package-dotenv-file` 8 | 9 | ## Usage 10 | 11 | ``` 12 | service: your-service 13 | ... 14 | custom: 15 | packageEnv: 16 | fileName: .env-${env:ENVIRONMENT} 17 | 18 | plugins: 19 | - serverless-plugin-package-dotenv-file 20 | ``` 21 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "serverless-plugin-package-dotenv-file", 3 | "version": "0.0.2", 4 | "description": "", 5 | "main": "src/index.js", 6 | "repository": { 7 | "url": "https://github.com/ACloudGuru/serverless-plugin-package-dotenv-file.git", 8 | "type": "git" 9 | }, 10 | "author": "John McKim ", 11 | "license": "MIT", 12 | "scripts": { 13 | "lint": "eslint .", 14 | "test": "istanbul cover _mocha test -- -R spec" 15 | }, 16 | "dependencies": { 17 | "fs-extra": "^0.30.0" 18 | }, 19 | "devDependencies": { 20 | "chai": "^3.5.0", 21 | "coveralls": "^2.11.14", 22 | "eslint": "^3.8.1", 23 | "eslint-config-standard": "^6.2.0", 24 | "eslint-plugin-node": "^2.1.3", 25 | "eslint-plugin-promise": "^3.3.0", 26 | "eslint-plugin-standard": "^2.0.1", 27 | "istanbul": "^0.4.5", 28 | "mocha": "^3.1.2", 29 | "sinon": "^1.17.6" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const path = require('path'); 4 | const fse = require('fs-extra'); 5 | 6 | class Plugin { 7 | constructor(serverless, options) { 8 | this.serverless = serverless; 9 | this.options = options; 10 | 11 | this.copyFileSync = fse.copySync; 12 | this.removeFileSync = fse.unlinkSync; 13 | 14 | this.hooks = { 15 | 'offline:start:init': this.writeEnvironmentFile.bind(this), 16 | 'before:deploy:function:deploy': this.writeEnvironmentFile.bind(this), 17 | 'after:deploy:function:deploy': this.deleteEnvironmentFile.bind(this), 18 | 'before:deploy:createDeploymentArtifacts': this.writeEnvironmentFile.bind(this), 19 | 'after:deploy:createDeploymentArtifacts': this.deleteEnvironmentFile.bind(this), 20 | }; 21 | } 22 | 23 | getPackagedEnvFilePath() { 24 | return this.getEnvFilePath('.env'); 25 | } 26 | 27 | getEnvFilePath(envFile) { 28 | return path.join(this.serverless.config.servicePath, envFile); 29 | } 30 | 31 | getConfig() { 32 | return this.serverless.service.custom.packageEnv; 33 | } 34 | 35 | writeEnvironmentFile() { 36 | const config = this.getConfig(); 37 | 38 | if (config && config.fileName) { 39 | const envFilePath = this.getEnvFilePath(config.fileName); 40 | const packagedEnvPath = this.getPackagedEnvFilePath(); 41 | this.copyFileSync(envFilePath, packagedEnvPath); 42 | 43 | this.serverless.cli.log(`Wrote ${config.fileName} file to .env`); 44 | } else { 45 | this.serverless.cli.log(`No env file was configured`); 46 | } 47 | } 48 | 49 | deleteEnvironmentFile() { 50 | const envPath = this.getPackagedEnvFilePath(); 51 | try { 52 | this.removeFileSync(envPath); 53 | } catch (err) { // eslint-disable-line no-empty 54 | } 55 | } 56 | } 57 | 58 | module.exports = Plugin; 59 | -------------------------------------------------------------------------------- /test/.eslintrc.yml: -------------------------------------------------------------------------------- 1 | rules: 2 | node/no-unpublished-require: 0 3 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('./index.tests'); 4 | -------------------------------------------------------------------------------- /test/index.tests.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const expect = require('chai').expect; 4 | const sinon = require('sinon'); 5 | 6 | const path = require('path'); 7 | 8 | const Plugin = require('../src'); 9 | 10 | describe('#index', function() { 11 | const testServicePath = path.join(__dirname, '.tmp'); 12 | const testEnvFile = '.env-prod'; 13 | const expectedPackageEnvPath = path.join(testServicePath, '.env'); 14 | const expectedTestEnvPath = path.join(testServicePath, testEnvFile); 15 | 16 | let plugin = null; 17 | let serverless = null; 18 | let copyFileSyncStub = null; 19 | let removeFileSyncStub = null; 20 | 21 | beforeEach(() => { 22 | serverless = { 23 | cli: { log: console.log }, 24 | config: { 25 | servicePath: testServicePath 26 | }, 27 | service: { 28 | custom: { 29 | packageEnv: { 30 | fileName: testEnvFile 31 | } 32 | } 33 | } 34 | }; 35 | 36 | plugin = new Plugin(serverless, {}); 37 | 38 | copyFileSyncStub = sinon.stub(plugin, 'copyFileSync'); 39 | removeFileSyncStub = sinon.stub(plugin, 'removeFileSync'); 40 | }); 41 | 42 | it('should get packaged env path', () => { 43 | const packagedEnvPath = plugin.getPackagedEnvFilePath(); 44 | expect(packagedEnvPath).to.equal(expectedPackageEnvPath); 45 | }); 46 | 47 | it('should get env path', () => { 48 | const envFile = '.env-prod'; 49 | const envPath = plugin.getEnvFilePath(envFile); 50 | expect(envPath).to.equal(expectedTestEnvPath); 51 | }); 52 | 53 | it('should get config', () => { 54 | const config = plugin.getConfig(); 55 | expect(config).to.equal(serverless.service.custom.packageEnv); 56 | }); 57 | 58 | it('should write env file', () => { 59 | plugin.writeEnvironmentFile(); 60 | 61 | expect(copyFileSyncStub.calledOnce).to.equal(true); 62 | expect(copyFileSyncStub.args[0][0]).to.equal(expectedTestEnvPath); 63 | expect(copyFileSyncStub.args[0][1]).to.equal(expectedPackageEnvPath); 64 | }); 65 | 66 | it('should remove env file', () => { 67 | plugin.deleteEnvironmentFile(); 68 | 69 | expect(removeFileSyncStub.calledOnce).to.equal(true); 70 | expect(removeFileSyncStub.args[0][0]).to.equal(expectedPackageEnvPath); 71 | }); 72 | }); 73 | --------------------------------------------------------------------------------