Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
coverage
node_modules
*.log
tmp
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 17 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,31 @@ class ServerlessDotenvPlugin {
constructor (serverless, options) {
this.serverless = serverless
this.options = options
this.custom = this.serverless.service.custom || {};
this.custom.dotenv = this.custom.dotenv || {};

this.commands = {
dotenv: {
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
}
}
}
}

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)
}

Expand Down Expand Up @@ -55,8 +67,10 @@ 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 || this.custom.dotenv.path ||
path.join(this.serverless.config.servicePath, '.serverless')

const dotEnvFile = path.join(dotEnvPath, '.env')
const dotEnvDocument = transformEnvVarsToString(this.environmentVariables)

mkdirp.sync(dotEnvPath)
Expand Down
22 changes: 21 additions & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -66,6 +66,26 @@ 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 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()
Expand Down